blob: b57c425b110cc27e50ee789f563321f42606bfa7 [file] [log] [blame]
Jack Jansen7571f301995-07-29 13:48:41 +00001"""Create an applet from a Python script.
2
3This puts up a dialog asking for a Python source file ('TEXT').
4The output is a file with the same name but its ".py" suffix dropped.
5It is created by copying an applet template and then adding a 'PYC '
6resource named __main__ containing the compiled, marshalled script.
7"""
8
Jack Jansen0f452fa1995-09-01 11:54:11 +00009
Jack Jansen7571f301995-07-29 13:48:41 +000010import sys
11sys.stdout = sys.stderr
12
Jack Jansen7571f301995-07-29 13:48:41 +000013import os
Jack Jansen7571f301995-07-29 13:48:41 +000014import macfs
15import MacOS
Jack Jansenaf647dd1997-05-13 15:42:26 +000016import EasyDialogs
Jack Jansen015b70e1998-07-31 09:44:23 +000017import buildtools
Jack Jansen7571f301995-07-29 13:48:41 +000018
Jack Jansen0f452fa1995-09-01 11:54:11 +000019
20def main():
Jack Jansen015b70e1998-07-31 09:44:23 +000021 try:
22 buildapplet()
23 except buildtools.BuildError, detail:
24 EasyDialogs.Message(detail)
25
26
27def buildapplet():
28 buildtools.DEBUG=1
Jack Jansen0f452fa1995-09-01 11:54:11 +000029
30 # Find the template
31 # (there's no point in proceeding if we can't find it)
32
Jack Jansen015b70e1998-07-31 09:44:23 +000033 template = buildtools.findtemplate()
34
Jack Jansen7571f301995-07-29 13:48:41 +000035 # Ask for source text if not specified in sys.argv[1:]
36
37 if not sys.argv[1:]:
Jack Jansen8554e301998-02-20 16:06:56 +000038 srcfss, ok = macfs.PromptGetFile('Select Python source or applet:', 'TEXT', 'APPL')
Jack Jansen7571f301995-07-29 13:48:41 +000039 if not ok:
40 return
41 filename = srcfss.as_pathname()
42 tp, tf = os.path.split(filename)
43 if tf[-3:] == '.py':
44 tf = tf[:-3]
45 else:
Jack Jansen015b70e1998-07-31 09:44:23 +000046 tf = tf + '.applet'
Jack Jansen7571f301995-07-29 13:48:41 +000047 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
48 if not ok: return
Jack Jansen8554e301998-02-20 16:06:56 +000049 dstfilename = dstfss.as_pathname()
50 cr, tp = MacOS.GetCreatorAndType(filename)
51 if tp == 'APPL':
Jack Jansen015b70e1998-07-31 09:44:23 +000052 buildtools.update(template, filename, dstfilename)
Jack Jansen8554e301998-02-20 16:06:56 +000053 else:
Jack Jansen015b70e1998-07-31 09:44:23 +000054 buildtools.process(template, filename, dstfilename, 1)
Jack Jansen7571f301995-07-29 13:48:41 +000055 else:
56
57 # Loop over all files to be processed
58 for filename in sys.argv[1:]:
Jack Jansen8554e301998-02-20 16:06:56 +000059 cr, tp = MacOS.GetCreatorAndType(filename)
60 if tp == 'APPL':
Jack Jansen015b70e1998-07-31 09:44:23 +000061 buildtools.update(template, filename, '')
Jack Jansen8554e301998-02-20 16:06:56 +000062 else:
Jack Jansen015b70e1998-07-31 09:44:23 +000063 buildtools.process(template, filename, '', 1)
Jack Jansen7571f301995-07-29 13:48:41 +000064
65
66if __name__ == '__main__':
67 main()