annotate Mac/scripts/BuildApplet.py @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents 7190348d466d
children 3ee9519eaa4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 """Create an applet from a Python script.
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 This puts up a dialog asking for a Python source file ('TEXT').
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 The output is a file with the same name but its ".py" suffix dropped.
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 It is created by copying an applet template and then adding a 'PYC '
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6 resource named __main__ containing the compiled, marshalled script.
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7 """
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8
3062
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
9
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10 import sys
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 sys.stdout = sys.stderr
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 import os
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
14 import macfs
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
15 import MacOS
5489
2d133280129f Use ProgressBar to show progress in stead of print
Jack Jansen <jack.jansen@cwi.nl>
parents: 4311
diff changeset
16 import EasyDialogs
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
17 import buildtools
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
18 import getopt
3062
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
19
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
20 def main():
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
21 try:
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
22 buildapplet()
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
23 except buildtools.BuildError, detail:
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
24 EasyDialogs.Message(detail)
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
25
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
26
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
27 def buildapplet():
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
28 buildtools.DEBUG=1
3062
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
29
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
30 # Find the template
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
31 # (there's no point in proceeding if we can't find it)
8d5179277858 Slightly reorganized so it is useable from another program, and
Jack Jansen <jack.jansen@cwi.nl>
parents: 3045
diff changeset
32
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
33 template = buildtools.findtemplate()
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
34
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 # Ask for source text if not specified in sys.argv[1:]
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 if not sys.argv[1:]:
7441
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
38 srcfss, ok = macfs.PromptGetFile('Select Python source or applet:', 'TEXT', 'APPL')
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
39 if not ok:
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
40 return
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41 filename = srcfss.as_pathname()
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 tp, tf = os.path.split(filename)
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43 if tf[-3:] == '.py':
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44 tf = tf[:-3]
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45 else:
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
46 tf = tf + '.applet'
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
47 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
48 if not ok: return
7441
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
49 dstfilename = dstfss.as_pathname()
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
50 cr, tp = MacOS.GetCreatorAndType(filename)
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
51 if tp == 'APPL':
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
52 buildtools.update(template, filename, dstfilename)
7441
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
53 else:
8798
ca1e5bc5209d Common code used to the buildtools module (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents: 8106
diff changeset
54 buildtools.process(template, filename, dstfilename, 1)
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
55 else:
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
56
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
57 SHORTOPTS = "o:r:ne:v?"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
58 LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help")
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
59 try:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
60 options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS)
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
61 except getopt.error:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
62 usage()
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
63 if options and len(args) > 1:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
64 sys.stderr.write("Cannot use options when specifying multiple input files")
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
65 sys.exit(1)
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
66 dstfilename = None
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
67 rsrcfilename = None
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
68 raw = 0
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
69 extras = []
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
70 verbose = None
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
71 for opt, arg in options:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
72 if opt in ('-o', '--output'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
73 dstfilename = arg
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
74 elif opt in ('-r', '--resource'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
75 rsrcfilename = arg
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
76 elif opt in ('-n', '--noargv'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
77 raw = 1
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
78 elif opt in ('-e', '--extra'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
79 extras.append(arg)
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
80 elif opt in ('-v', '--verbose'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
81 verbose = Verbose()
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
82 elif opt in ('-?', '--help'):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
83 usage()
24974
7190348d466d For MacPython-OS9 verbose is the default.
Jack Jansen <jack.jansen@cwi.nl>
parents: 23748
diff changeset
84 # On OS9 always be verbose
7190348d466d For MacPython-OS9 verbose is the default.
Jack Jansen <jack.jansen@cwi.nl>
parents: 23748
diff changeset
85 if sys.platform == 'mac' and not verbose:
7190348d466d For MacPython-OS9 verbose is the default.
Jack Jansen <jack.jansen@cwi.nl>
parents: 23748
diff changeset
86 verbose = 'default'
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
87 # Loop over all files to be processed
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
88 for filename in args:
7441
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
89 cr, tp = MacOS.GetCreatorAndType(filename)
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
90 if tp == 'APPL':
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
91 buildtools.update(template, filename, dstfilename)
7441
a8059b96bc35 BuildApplet will now also update old applets
Jack Jansen <jack.jansen@cwi.nl>
parents: 5489
diff changeset
92 else:
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
93 buildtools.process(template, filename, dstfilename, 1,
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
94 rsrcname=rsrcfilename, others=extras, raw=raw, progress=verbose)
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
95
23748
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
96 def usage():
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
97 print "BuildApplet creates an application from a Python source file"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
98 print "Usage:"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
99 print " BuildApplet interactive, single file, no options"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
100 print " BuildApplet src1.py src2.py ... non-interactive multiple file"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
101 print " BuildApplet [options] src.py non-interactive single file"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
102 print "Options:"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
103 print " --output o Output file; default based on source filename, short -o"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
104 print " --resource r Resource file; default based on source filename, short -r"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
105 print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
106 print " --extra f Extra file to put in .app bundle, short -e, OSX only"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
107 print " --verbose Verbose, short -v"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
108 print " --help This message, short -?"
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
109 sys.exit(1)
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
110
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
111 class Verbose:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
112 """This class mimics EasyDialogs.ProgressBar but prints to stderr"""
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
113 def __init__(self, *args):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
114 if args and args[0]:
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
115 self.label(args[0])
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
116
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
117 def set(self, *args):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
118 pass
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
119
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
120 def inc(self, *args):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
121 pass
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
122
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
123 def label(self, str):
2d67a0ba9020 - Better commandline interface to BuildApplet, complete with options,
Jack Jansen <jack.jansen@cwi.nl>
parents: 8798
diff changeset
124 sys.stderr.write(str+'\n')
2873
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
125
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
126 if __name__ == '__main__':
315760ba0041 A new home for scripts/applets
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
127 main()