ยปCore Development>Code coverage>Mac/scripts/BuildApplet.py

Python code coverage for Mac/scripts/BuildApplet.py

#countcontent
1n/a"""Create an applet from a Python script.
2n/a
3n/aThis puts up a dialog asking for a Python source file ('TEXT').
4n/aThe output is a file with the same name but its ".py" suffix dropped.
5n/aIt is created by copying an applet template and then adding a 'PYC '
6n/aresource named __main__ containing the compiled, marshalled script.
7n/a"""
8n/a
9n/a
10n/aimport sys
11n/asys.stdout = sys.stderr
12n/a
13n/aimport os
14n/aimport MacOS
15n/atry:
16n/a import EasyDialogs
17n/aexcept ImportError:
18n/a EasyDialogs = None
19n/aimport buildtools
20n/aimport getopt
21n/a
22n/aif not sys.executable.startswith(sys.exec_prefix):
23n/a # Oh, the joys of using a python script to bootstrap applicatin bundles
24n/a # sys.executable points inside the current application bundle. Because this
25n/a # path contains blanks (two of them actually) this path isn't usable on
26n/a # #! lines. Reset sys.executable to point to the embedded python interpreter
27n/a sys.executable = os.path.join(sys.prefix,
28n/a 'Resources/Python.app/Contents/MacOS/Python')
29n/a
30n/a # Just in case we're not in a framework:
31n/a if not os.path.exists(sys.executable):
32n/a sys.executable = os.path.join(sys.exec_prefix, 'bin/python')
33n/a
34n/adef main():
35n/a try:
36n/a buildapplet()
37n/a except buildtools.BuildError, detail:
38n/a if EasyDialogs is None:
39n/a print detail
40n/a else:
41n/a EasyDialogs.Message(detail)
42n/a
43n/a
44n/adef buildapplet():
45n/a buildtools.DEBUG=1
46n/a
47n/a # Find the template
48n/a # (there's no point in proceeding if we can't find it)
49n/a
50n/a template = buildtools.findtemplate()
51n/a
52n/a # Ask for source text if not specified in sys.argv[1:]
53n/a
54n/a if not sys.argv[1:]:
55n/a if EasyDialogs is None:
56n/a usage()
57n/a sys.exit(1)
58n/a
59n/a filename = EasyDialogs.AskFileForOpen(message='Select Python source or applet:',
60n/a typeList=('TEXT', 'APPL'))
61n/a if not filename:
62n/a return
63n/a tp, tf = os.path.split(filename)
64n/a if tf[-3:] == '.py':
65n/a tf = tf[:-3]
66n/a else:
67n/a tf = tf + '.applet'
68n/a dstfilename = EasyDialogs.AskFileForSave(message='Save application as:',
69n/a savedFileName=tf)
70n/a if not dstfilename: return
71n/a cr, tp = MacOS.GetCreatorAndType(filename)
72n/a if tp == 'APPL':
73n/a buildtools.update(template, filename, dstfilename)
74n/a else:
75n/a buildtools.process(template, filename, dstfilename, 1)
76n/a else:
77n/a
78n/a SHORTOPTS = "o:r:ne:v?PR"
79n/a LONGOPTS=("output=", "resource=", "noargv", "extra=", "verbose", "help", "python=", "destroot=")
80n/a try:
81n/a options, args = getopt.getopt(sys.argv[1:], SHORTOPTS, LONGOPTS)
82n/a except getopt.error:
83n/a usage()
84n/a if options and len(args) > 1:
85n/a sys.stderr.write("Cannot use options when specifying multiple input files")
86n/a sys.exit(1)
87n/a dstfilename = None
88n/a rsrcfilename = None
89n/a raw = 0
90n/a extras = []
91n/a verbose = None
92n/a destroot = ''
93n/a for opt, arg in options:
94n/a if opt in ('-o', '--output'):
95n/a dstfilename = arg
96n/a elif opt in ('-r', '--resource'):
97n/a rsrcfilename = arg
98n/a elif opt in ('-n', '--noargv'):
99n/a raw = 1
100n/a elif opt in ('-e', '--extra'):
101n/a if ':' in arg:
102n/a arg = arg.split(':')
103n/a extras.append(arg)
104n/a elif opt in ('-P', '--python'):
105n/a # This is a very dirty trick. We set sys.executable
106n/a # so that bundlebuilder will use this in the #! line
107n/a # for the applet bootstrap.
108n/a sys.executable = arg
109n/a elif opt in ('-v', '--verbose'):
110n/a verbose = Verbose()
111n/a elif opt in ('-?', '--help'):
112n/a usage()
113n/a elif opt in ('-d', '--destroot'):
114n/a destroot = arg
115n/a # Loop over all files to be processed
116n/a for filename in args:
117n/a cr, tp = MacOS.GetCreatorAndType(filename)
118n/a if tp == 'APPL':
119n/a buildtools.update(template, filename, dstfilename)
120n/a else:
121n/a buildtools.process(template, filename, dstfilename, 1,
122n/a rsrcname=rsrcfilename, others=extras, raw=raw,
123n/a progress=verbose, destroot=destroot)
124n/a
125n/adef usage():
126n/a print "BuildApplet creates an application from a Python source file"
127n/a print "Usage:"
128n/a print " BuildApplet interactive, single file, no options"
129n/a print " BuildApplet src1.py src2.py ... non-interactive multiple file"
130n/a print " BuildApplet [options] src.py non-interactive single file"
131n/a print "Options:"
132n/a print " --output o Output file; default based on source filename, short -o"
133n/a print " --resource r Resource file; default based on source filename, short -r"
134n/a print " --noargv Build applet without drag-and-drop sys.argv emulation, short -n, OSX only"
135n/a print " --extra src[:dst] Extra file to put in .app bundle, short -e, OSX only"
136n/a print " --verbose Verbose, short -v"
137n/a print " --help This message, short -?"
138n/a sys.exit(1)
139n/a
140n/aclass Verbose:
141n/a """This class mimics EasyDialogs.ProgressBar but prints to stderr"""
142n/a def __init__(self, *args):
143n/a if args and args[0]:
144n/a self.label(args[0])
145n/a
146n/a def set(self, *args):
147n/a pass
148n/a
149n/a def inc(self, *args):
150n/a pass
151n/a
152n/a def label(self, str):
153n/a sys.stderr.write(str+'\n')
154n/a
155n/aif __name__ == '__main__':
156n/a main()