Bump to 2.3.1 to pick up the missing file.
[python/dscho.git] / Mac / scripts / BuildApplication.py
bloba7c8f407a5055a67cdcc37b7726945bfa15679f3
1 """Create a standalone application from a Python script.
3 This puts up a dialog asking for a Python source file ('TEXT').
4 The output is a file with the same name but its ".py" suffix dropped.
5 It is created by copying an applet template, all used shared libs and
6 then adding 'PYC ' resources containing compiled versions of all used
7 modules written in Python and the main script itself, as __main__.
8 """
11 import sys
13 import string
14 import os
15 import macfs
16 import MacOS
17 from Carbon import Res
18 from Carbon import Dlg
19 import EasyDialogs
20 import buildtools
21 import macresource
23 # Hmmm...
24 MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
25 if MACFREEZEPATH not in sys.path:
26 sys.path.append(MACFREEZEPATH)
28 import macgen_bin
30 # dialog, items
31 DLG_ID = 400
32 OK_BUTTON = 1
33 CANCEL_BUTTON = 2
34 GENFAT_BUTTON = 4
35 GENPPC_BUTTON = 5
36 GEN68K_BUTTON = 6
38 # Define this if we cannot generate 68/fat binaries (Python 1.6)
39 PPC_ONLY=1
42 macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")
44 def main():
45 try:
46 buildapplication()
47 except buildtools.BuildError, detail:
48 EasyDialogs.Message(detail)
51 def buildapplication(debug = 0):
52 buildtools.DEBUG = debug
54 # Ask for source text if not specified in sys.argv[1:]
56 if not sys.argv[1:]:
57 srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT')
58 if not ok:
59 return
60 filename = srcfss.as_pathname()
61 else:
62 if sys.argv[2:]:
63 raise buildtools.BuildError, "please select one file at a time"
64 filename = sys.argv[1]
65 tp, tf = os.path.split(filename)
67 # interact with user
68 architecture, ok = interact(tf)
69 if not ok:
70 return
71 if tf[-3:] == '.py':
72 tf = tf[:-3]
73 else:
74 tf = tf + '.app'
76 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
77 if not ok:
78 return
79 dstfilename = dstfss.as_pathname()
81 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
84 class radio:
86 def __init__(self, dlg, *items):
87 self.items = {}
88 for item in items:
89 ctl = dlg.GetDialogItemAsControl(item)
90 self.items[item] = ctl
92 def set(self, setitem):
93 for item, ctl in self.items.items():
94 if item == setitem:
95 ctl.SetControlValue(1)
96 else:
97 ctl.SetControlValue(0)
99 def get(self):
100 for item, ctl in self.items.items():
101 if ctl.GetControlValue():
102 return item
104 def hasitem(self, item):
105 return self.items.has_key(item)
108 def interact(scriptname):
109 if PPC_ONLY:
110 return 'pwpc', 1
111 d = Dlg.GetNewDialog(DLG_ID, -1)
112 if not d:
113 raise "Can't get DLOG resource with id =", DLG_ID
114 d.SetDialogDefaultItem(OK_BUTTON)
115 d.SetDialogCancelItem(CANCEL_BUTTON)
116 Dlg.ParamText(scriptname, "", "", "")
118 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
119 radiogroup.set(GENFAT_BUTTON)
121 gentype = 'fat'
122 while 1:
123 n = Dlg.ModalDialog(None)
124 if n == OK_BUTTON or n == CANCEL_BUTTON:
125 break
126 elif radiogroup.hasitem(n):
127 radiogroup.set(n)
128 genitem = radiogroup.get()
129 del radiogroup
130 del d
131 if genitem == GENFAT_BUTTON:
132 gentype = 'fat'
133 elif genitem == GENPPC_BUTTON:
134 gentype = 'pwpc'
135 elif genitem == GEN68K_BUTTON:
136 gentype = 'm68k'
137 return gentype, n == OK_BUTTON
140 if __name__ == '__main__':
141 main()