Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / scripts / BuildApplication.py
blobc919b3f748b5c5bef5c75ce14135ef3dfade4971
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 MacOS
16 from Carbon import Res
17 from Carbon import Dlg
18 import EasyDialogs
19 import buildtools
20 import macresource
22 # Hmmm...
23 MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
24 if MACFREEZEPATH not in sys.path:
25 sys.path.append(MACFREEZEPATH)
27 import macgen_bin
29 # dialog, items
30 DLG_ID = 400
31 OK_BUTTON = 1
32 CANCEL_BUTTON = 2
33 GENFAT_BUTTON = 4
34 GENPPC_BUTTON = 5
35 GEN68K_BUTTON = 6
37 # Define this if we cannot generate 68/fat binaries (Python 1.6)
38 PPC_ONLY=1
41 macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")
43 def main():
44 try:
45 buildapplication()
46 except buildtools.BuildError, detail:
47 EasyDialogs.Message(detail)
50 def buildapplication(debug = 0):
51 buildtools.DEBUG = debug
53 # Ask for source text if not specified in sys.argv[1:]
55 if not sys.argv[1:]:
56 filename = EasyDialogs.AskFileForOpen(message='Select Python source:',
57 fileTypes=('TEXT',))
58 if not filename:
59 return
60 else:
61 if sys.argv[2:]:
62 raise buildtools.BuildError, "please select one file at a time"
63 filename = sys.argv[1]
64 tp, tf = os.path.split(filename)
66 # interact with user
67 architecture, ok = interact(tf)
68 if not ok:
69 return
70 if tf[-3:] == '.py':
71 tf = tf[:-3]
72 else:
73 tf = tf + '.app'
75 dstfilename = EasyDialogs.AskFileForSate(message='Save application as:',
76 savedFileName=tf)
77 if not ok:
78 return
80 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
83 class radio:
85 def __init__(self, dlg, *items):
86 self.items = {}
87 for item in items:
88 ctl = dlg.GetDialogItemAsControl(item)
89 self.items[item] = ctl
91 def set(self, setitem):
92 for item, ctl in self.items.items():
93 if item == setitem:
94 ctl.SetControlValue(1)
95 else:
96 ctl.SetControlValue(0)
98 def get(self):
99 for item, ctl in self.items.items():
100 if ctl.GetControlValue():
101 return item
103 def hasitem(self, item):
104 return self.items.has_key(item)
107 def interact(scriptname):
108 if PPC_ONLY:
109 return 'pwpc', 1
110 d = Dlg.GetNewDialog(DLG_ID, -1)
111 if not d:
112 raise "Can't get DLOG resource with id =", DLG_ID
113 d.SetDialogDefaultItem(OK_BUTTON)
114 d.SetDialogCancelItem(CANCEL_BUTTON)
115 Dlg.ParamText(scriptname, "", "", "")
117 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
118 radiogroup.set(GENFAT_BUTTON)
120 gentype = 'fat'
121 while 1:
122 n = Dlg.ModalDialog(None)
123 if n == OK_BUTTON or n == CANCEL_BUTTON:
124 break
125 elif radiogroup.hasitem(n):
126 radiogroup.set(n)
127 genitem = radiogroup.get()
128 del radiogroup
129 del d
130 if genitem == GENFAT_BUTTON:
131 gentype = 'fat'
132 elif genitem == GENPPC_BUTTON:
133 gentype = 'pwpc'
134 elif genitem == GEN68K_BUTTON:
135 gentype = 'm68k'
136 return gentype, n == OK_BUTTON
139 if __name__ == '__main__':
140 main()