blob: 5e0bc27a3c645b719df654ddcffb89b475931bee [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 Jansen388fbf32002-06-09 22:08:52 +000018import getopt
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
Jack Jansen388fbf32002-06-09 22:08:52 +000057 SHORTOPTS = "o:r:ne:v?"
58 LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help")
59 try:
60 options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS)
61 except getopt.error:
62 usage()
63 if options and len(args) > 1:
64 sys.stderr.write("Cannot use options when specifying multiple input files")
65 sys.exit(1)
66 dstfilename = None
67 rsrcfilename = None
68 raw = 0
69 extras = []
70 verbose = None
71 for opt, arg in options:
72 if opt in ('-o', '--output'):
73 dstfilename = arg
74 elif opt in ('-r', '--resource'):
75 rsrcfilename = arg
76 elif opt in ('-n', '--noargv'):
77 raw = 1
78 elif opt in ('-e', '--extra'):
79 extras.append(arg)
80 elif opt in ('-v', '--verbose'):
81 verbose = Verbose()
82 elif opt in ('-?', '--help'):
83 usage()
Jack Jansen5fd94582002-08-22 23:36:11 +000084 # On OS9 always be verbose
85 if sys.platform == 'mac' and not verbose:
86 verbose = 'default'
Jack Jansen7571f301995-07-29 13:48:41 +000087 # Loop over all files to be processed
Jack Jansen388fbf32002-06-09 22:08:52 +000088 for filename in args:
Jack Jansen8554e301998-02-20 16:06:56 +000089 cr, tp = MacOS.GetCreatorAndType(filename)
90 if tp == 'APPL':
Jack Jansen388fbf32002-06-09 22:08:52 +000091 buildtools.update(template, filename, dstfilename)
Jack Jansen8554e301998-02-20 16:06:56 +000092 else:
Jack Jansen388fbf32002-06-09 22:08:52 +000093 buildtools.process(template, filename, dstfilename, 1,
94 rsrcname=rsrcfilename, others=extras, raw=raw, progress=verbose)
Jack Jansen7571f301995-07-29 13:48:41 +000095
Jack Jansen388fbf32002-06-09 22:08:52 +000096def usage():
97 print "BuildApplet creates an application from a Python source file"
98 print "Usage:"
99 print " BuildApplet interactive, single file, no options"
100 print " BuildApplet src1.py src2.py ... non-interactive multiple file"
101 print " BuildApplet [options] src.py non-interactive single file"
102 print "Options:"
103 print " --output o Output file; default based on source filename, short -o"
104 print " --resource r Resource file; default based on source filename, short -r"
105 print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
106 print " --extra f Extra file to put in .app bundle, short -e, OSX only"
107 print " --verbose Verbose, short -v"
108 print " --help This message, short -?"
109 sys.exit(1)
110
111class Verbose:
112 """This class mimics EasyDialogs.ProgressBar but prints to stderr"""
113 def __init__(self, *args):
114 if args and args[0]:
115 self.label(args[0])
116
117 def set(self, *args):
118 pass
119
120 def inc(self, *args):
121 pass
122
123 def label(self, str):
124 sys.stderr.write(str+'\n')
Jack Jansen7571f301995-07-29 13:48:41 +0000125
126if __name__ == '__main__':
127 main()