annotate Mac/scripts/BuildApplication.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 546ef318ba5d
children 412e66e3530e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
1 """Create a standalone application from a Python script.
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
2
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 This puts up a dialog asking for a Python source file ('TEXT').
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 The output is a file with the same name but its ".py" suffix dropped.
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 It is created by copying an applet template, all used shared libs and
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6 then adding 'PYC ' resources containing compiled versions of all used
11437
32a480d0823c Typo fixed in docstring; removed unneccesary import. (jvr)
Just van Rossum <just@lettererror.com>
parents: 10739
diff changeset
7 modules written in Python and the main script itself, as __main__.
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 """
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 import sys
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 import string
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
14 import os
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
15 import macfs
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
16 import MacOS
19488
42b2f1936254 Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents: 15571
diff changeset
17 from Carbon import Res
42b2f1936254 Import the MacOS toolbox modules from the Carbon package.
Jack Jansen <jack.jansen@cwi.nl>
parents: 15571
diff changeset
18 from Carbon import Dlg
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
19 import EasyDialogs
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20 import buildtools
19515
546ef318ba5d Use the new macresource module to open the accompanying resource file (if needed).
Jack Jansen <jack.jansen@cwi.nl>
parents: 19488
diff changeset
21 import macresource
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
23 # Hmmm...
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24 MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25 if MACFREEZEPATH not in sys.path:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 sys.path.append(MACFREEZEPATH)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
28 import macgen_bin
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
29
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
30 # dialog, items
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
31 DLG_ID = 400
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
32 OK_BUTTON = 1
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33 CANCEL_BUTTON = 2
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 GENFAT_BUTTON = 4
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 GENPPC_BUTTON = 5
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36 GEN68K_BUTTON = 6
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37
13698
cb29d4a2c17b Added a "compile-time" flag PPC_ONLY, if set we make ppc-only applications without posting the dialog. This is the normal situation since 1.6alfa (no more 68k support).
Jack Jansen <jack.jansen@cwi.nl>
parents: 12972
diff changeset
38 # Define this if we cannot generate 68/fat binaries (Python 1.6)
cb29d4a2c17b Added a "compile-time" flag PPC_ONLY, if set we make ppc-only applications without posting the dialog. This is the normal situation since 1.6alfa (no more 68k support).
Jack Jansen <jack.jansen@cwi.nl>
parents: 12972
diff changeset
39 PPC_ONLY=1
cb29d4a2c17b Added a "compile-time" flag PPC_ONLY, if set we make ppc-only applications without posting the dialog. This is the normal situation since 1.6alfa (no more 68k support).
Jack Jansen <jack.jansen@cwi.nl>
parents: 12972
diff changeset
40
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41
19515
546ef318ba5d Use the new macresource module to open the accompanying resource file (if needed).
Jack Jansen <jack.jansen@cwi.nl>
parents: 19488
diff changeset
42 macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
43
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44 def main():
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45 try:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
46 buildapplication()
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
47 except buildtools.BuildError, detail:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
48 EasyDialogs.Message(detail)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
49
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
50
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
51 def buildapplication(debug = 0):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
52 buildtools.DEBUG = debug
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
53
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
54 # Ask for source text if not specified in sys.argv[1:]
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
55
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
56 if not sys.argv[1:]:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
57 srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT')
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
58 if not ok:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
59 return
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
60 filename = srcfss.as_pathname()
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
61 else:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
62 if sys.argv[2:]:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
63 raise buildtools.BuildError, "please select one file at a time"
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
64 filename = sys.argv[1]
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
65 tp, tf = os.path.split(filename)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
66
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
67 # interact with user
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
68 architecture, ok = interact(tf)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
69 if not ok:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
70 return
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
71 if tf[-3:] == '.py':
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
72 tf = tf[:-3]
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
73 else:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
74 tf = tf + '.app'
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
75
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
76 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
77 if not ok:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 return
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
79 dstfilename = dstfss.as_pathname()
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
80
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
81 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
82
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
83
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
84 class radio:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
85
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
86 def __init__(self, dlg, *items):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
87 self.items = {}
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
88 for item in items:
11623
6f2119cd99d2 Converted to Appearance.
Jack Jansen <jack.jansen@cwi.nl>
parents: 11437
diff changeset
89 ctl = dlg.GetDialogItemAsControl(item)
6f2119cd99d2 Converted to Appearance.
Jack Jansen <jack.jansen@cwi.nl>
parents: 11437
diff changeset
90 self.items[item] = ctl
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
91
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
92 def set(self, setitem):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
93 for item, ctl in self.items.items():
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
94 if item == setitem:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
95 ctl.SetControlValue(1)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
96 else:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
97 ctl.SetControlValue(0)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
98
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
99 def get(self):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
100 for item, ctl in self.items.items():
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
101 if ctl.GetControlValue():
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
102 return item
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
103
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
104 def hasitem(self, item):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
105 return self.items.has_key(item)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
106
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
107
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
108 def interact(scriptname):
13698
cb29d4a2c17b Added a "compile-time" flag PPC_ONLY, if set we make ppc-only applications without posting the dialog. This is the normal situation since 1.6alfa (no more 68k support).
Jack Jansen <jack.jansen@cwi.nl>
parents: 12972
diff changeset
109 if PPC_ONLY:
15571
027ee9a870d2 Oops, interaction() returned a single value in stead of a tuple (I thought that bug was loooong fixed??!?).
Jack Jansen <jack.jansen@cwi.nl>
parents: 13698
diff changeset
110 return 'pwpc', 1
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
111 d = Dlg.GetNewDialog(DLG_ID, -1)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
112 if not d:
15571
027ee9a870d2 Oops, interaction() returned a single value in stead of a tuple (I thought that bug was loooong fixed??!?).
Jack Jansen <jack.jansen@cwi.nl>
parents: 13698
diff changeset
113 raise "Can't get DLOG resource with id =", DLG_ID
8799
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
114 d.SetDialogDefaultItem(OK_BUTTON)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
115 d.SetDialogCancelItem(CANCEL_BUTTON)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
116 Dlg.ParamText(scriptname, "", "", "")
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
117
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
118 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
119 radiogroup.set(GENFAT_BUTTON)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
120
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
121 gentype = 'fat'
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
122 while 1:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
123 n = Dlg.ModalDialog(None)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
124 if n == OK_BUTTON or n == CANCEL_BUTTON:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
125 break
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
126 elif radiogroup.hasitem(n):
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
127 radiogroup.set(n)
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
128 genitem = radiogroup.get()
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
129 del radiogroup
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
130 del d
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
131 if genitem == GENFAT_BUTTON:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
132 gentype = 'fat'
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
133 elif genitem == GENPPC_BUTTON:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
134 gentype = 'pwpc'
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
135 elif genitem == GEN68K_BUTTON:
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
136 gentype = 'm68k'
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
137 return gentype, n == OK_BUTTON
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
138
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
139
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
140 if __name__ == '__main__':
44726b058700 Build a full standalone application from a python script (Just)
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
141 main()