blob: 756218f1ff3933e031cc82402e3676d789b08563 [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():
Tim Peters182b5ac2004-07-18 06:16:08 +000020 try:
21 buildapplet()
22 except buildtools.BuildError, detail:
23 EasyDialogs.Message(detail)
Jack Jansen015b70e1998-07-31 09:44:23 +000024
25
26def buildapplet():
Tim Peters182b5ac2004-07-18 06:16:08 +000027 buildtools.DEBUG=1
28
29 # Find the template
30 # (there's no point in proceeding if we can't find it)
31
32 template = buildtools.findtemplate()
33
34 # Ask for source text if not specified in sys.argv[1:]
35
36 if not sys.argv[1:]:
37 filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:',
38 typeList=('TEXT', 'APPL'))
39 if not filename:
40 return
41 tp, tf = os.path.split(filename)
42 if tf[-3:] == '.py':
43 tf = tf[:-3]
44 else:
45 tf = tf + '.applet'
46 dstfilename = EasyDialogs.AskFileForSave(message='Save application as:',
47 savedFileName=tf)
48 if not dstfilename: return
49 cr, tp = MacOS.GetCreatorAndType(filename)
50 if tp == 'APPL':
51 buildtools.update(template, filename, dstfilename)
52 else:
53 buildtools.process(template, filename, dstfilename, 1)
54 else:
55
Jack Jansenc77f6df2004-12-27 15:51:03 +000056 SHORTOPTS = "o:r:ne:v?PR"
57 LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help", "python=", "destroot=")
Tim Peters182b5ac2004-07-18 06:16:08 +000058 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
Jack Jansenc77f6df2004-12-27 15:51:03 +000070 destroot = ''
Tim Peters182b5ac2004-07-18 06:16:08 +000071 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 if ':' in arg:
80 arg = arg.split(':')
81 extras.append(arg)
82 elif opt in ('-P', '--python'):
83 # This is a very dirty trick. We set sys.executable
84 # so that bundlebuilder will use this in the #! line
85 # for the applet bootstrap.
86 sys.executable = arg
87 elif opt in ('-v', '--verbose'):
88 verbose = Verbose()
89 elif opt in ('-?', '--help'):
90 usage()
Jack Jansenc77f6df2004-12-27 15:51:03 +000091 elif opt in ('-d', '--destroot'):
92 destroot = arg
Tim Peters182b5ac2004-07-18 06:16:08 +000093 # On OS9 always be verbose
94 if sys.platform == 'mac' and not verbose:
95 verbose = 'default'
96 # Loop over all files to be processed
97 for filename in args:
98 cr, tp = MacOS.GetCreatorAndType(filename)
99 if tp == 'APPL':
100 buildtools.update(template, filename, dstfilename)
101 else:
102 buildtools.process(template, filename, dstfilename, 1,
Jack Jansenc77f6df2004-12-27 15:51:03 +0000103 rsrcname=rsrcfilename, others=extras, raw=raw,
104 progress=verbose, destroot=destroot)
Jack Jansen7571f301995-07-29 13:48:41 +0000105
Jack Jansen388fbf32002-06-09 22:08:52 +0000106def usage():
Tim Peters182b5ac2004-07-18 06:16:08 +0000107 print "BuildApplet creates an application from a Python source file"
108 print "Usage:"
109 print " BuildApplet interactive, single file, no options"
110 print " BuildApplet src1.py src2.py ... non-interactive multiple file"
111 print " BuildApplet [options] src.py non-interactive single file"
112 print "Options:"
113 print " --output o Output file; default based on source filename, short -o"
114 print " --resource r Resource file; default based on source filename, short -r"
115 print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
116 print " --extra src[:dst] Extra file to put in .app bundle, short -e, OSX only"
117 print " --verbose Verbose, short -v"
118 print " --help This message, short -?"
119 sys.exit(1)
Jack Jansen388fbf32002-06-09 22:08:52 +0000120
121class Verbose:
Tim Peters182b5ac2004-07-18 06:16:08 +0000122 """This class mimics EasyDialogs.ProgressBar but prints to stderr"""
123 def __init__(self, *args):
124 if args and args[0]:
125 self.label(args[0])
126
127 def set(self, *args):
128 pass
129
130 def inc(self, *args):
131 pass
132
133 def label(self, str):
134 sys.stderr.write(str+'\n')
Jack Jansen7571f301995-07-29 13:48:41 +0000135
136if __name__ == '__main__':
Tim Peters182b5ac2004-07-18 06:16:08 +0000137 main()