blob: 64a3d5eb95b8428e73f53c77fe4b4340b28f8383 [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 MacOS
Jack Jansenaf647dd1997-05-13 15:42:26 +000015import EasyDialogs
Jack Jansen015b70e1998-07-31 09:44:23 +000016import buildtools
Jack Jansen388fbf32002-06-09 22:08:52 +000017import getopt
Jack Jansen0f452fa1995-09-01 11:54:11 +000018
19def main():
Jack Jansen015b70e1998-07-31 09:44:23 +000020 try:
21 buildapplet()
22 except buildtools.BuildError, detail:
23 EasyDialogs.Message(detail)
24
25
26def buildapplet():
27 buildtools.DEBUG=1
Jack Jansen0f452fa1995-09-01 11:54:11 +000028
29 # Find the template
30 # (there's no point in proceeding if we can't find it)
31
Jack Jansen015b70e1998-07-31 09:44:23 +000032 template = buildtools.findtemplate()
33
Jack Jansen7571f301995-07-29 13:48:41 +000034 # Ask for source text if not specified in sys.argv[1:]
35
36 if not sys.argv[1:]:
Jack Jansend44a3ec2003-01-22 14:03:12 +000037 filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:',
Jack Jansendf976ca2003-01-26 20:35:47 +000038 typeList=('TEXT', 'APPL'))
Jack Jansend44a3ec2003-01-22 14:03:12 +000039 if not filename:
Jack Jansen7571f301995-07-29 13:48:41 +000040 return
Jack Jansen7571f301995-07-29 13:48:41 +000041 tp, tf = os.path.split(filename)
42 if tf[-3:] == '.py':
43 tf = tf[:-3]
44 else:
Jack Jansen015b70e1998-07-31 09:44:23 +000045 tf = tf + '.applet'
Jack Jansend44a3ec2003-01-22 14:03:12 +000046 dstfilename = EasyDialogs.AskFileForSave(message='Save application as:',
47 savedFileName=tf)
48 if not dstfilename: return
Jack Jansen8554e301998-02-20 16:06:56 +000049 cr, tp = MacOS.GetCreatorAndType(filename)
50 if tp == 'APPL':
Jack Jansen015b70e1998-07-31 09:44:23 +000051 buildtools.update(template, filename, dstfilename)
Jack Jansen8554e301998-02-20 16:06:56 +000052 else:
Jack Jansen015b70e1998-07-31 09:44:23 +000053 buildtools.process(template, filename, dstfilename, 1)
Jack Jansen7571f301995-07-29 13:48:41 +000054 else:
55
Jack Jansen388fbf32002-06-09 22:08:52 +000056 SHORTOPTS = "o:r:ne:v?"
57 LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help")
58 try:
59 options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS)
60 except getopt.error:
61 usage()
62 if options and len(args) > 1:
63 sys.stderr.write("Cannot use options when specifying multiple input files")
64 sys.exit(1)
65 dstfilename = None
66 rsrcfilename = None
67 raw = 0
68 extras = []
69 verbose = None
70 for opt, arg in options:
71 if opt in ('-o', '--output'):
72 dstfilename = arg
73 elif opt in ('-r', '--resource'):
74 rsrcfilename = arg
75 elif opt in ('-n', '--noargv'):
76 raw = 1
77 elif opt in ('-e', '--extra'):
Jack Jansenaf1d4222003-02-24 16:28:37 +000078 if ':' in arg:
79 arg = arg.split(':')
Jack Jansen388fbf32002-06-09 22:08:52 +000080 extras.append(arg)
81 elif opt in ('-v', '--verbose'):
82 verbose = Verbose()
83 elif opt in ('-?', '--help'):
84 usage()
Jack Jansen5fd94582002-08-22 23:36:11 +000085 # On OS9 always be verbose
86 if sys.platform == 'mac' and not verbose:
87 verbose = 'default'
Jack Jansen7571f301995-07-29 13:48:41 +000088 # Loop over all files to be processed
Jack Jansen388fbf32002-06-09 22:08:52 +000089 for filename in args:
Jack Jansendf976ca2003-01-26 20:35:47 +000090 cr, tp = MacOS.GetCreatorAndType(filename)
Jack Jansen8554e301998-02-20 16:06:56 +000091 if tp == 'APPL':
Jack Jansen388fbf32002-06-09 22:08:52 +000092 buildtools.update(template, filename, dstfilename)
Jack Jansen8554e301998-02-20 16:06:56 +000093 else:
Jack Jansen388fbf32002-06-09 22:08:52 +000094 buildtools.process(template, filename, dstfilename, 1,
95 rsrcname=rsrcfilename, others=extras, raw=raw, progress=verbose)
Jack Jansen7571f301995-07-29 13:48:41 +000096
Jack Jansen388fbf32002-06-09 22:08:52 +000097def usage():
98 print "BuildApplet creates an application from a Python source file"
99 print "Usage:"
100 print " BuildApplet interactive, single file, no options"
101 print " BuildApplet src1.py src2.py ... non-interactive multiple file"
102 print " BuildApplet [options] src.py non-interactive single file"
103 print "Options:"
Jack Jansenaf1d4222003-02-24 16:28:37 +0000104 print " --output o Output file; default based on source filename, short -o"
105 print " --resource r Resource file; default based on source filename, short -r"
106 print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
107 print " --extra src[:dst] Extra file to put in .app bundle, short -e, OSX only"
108 print " --verbose Verbose, short -v"
109 print " --help This message, short -?"
Jack Jansen388fbf32002-06-09 22:08:52 +0000110 sys.exit(1)
111
112class Verbose:
113 """This class mimics EasyDialogs.ProgressBar but prints to stderr"""
114 def __init__(self, *args):
115 if args and args[0]:
116 self.label(args[0])
117
118 def set(self, *args):
119 pass
120
121 def inc(self, *args):
122 pass
123
124 def label(self, str):
125 sys.stderr.write(str+'\n')
Jack Jansen7571f301995-07-29 13:48:41 +0000126
127if __name__ == '__main__':
128 main()