blob: 58499f6bb1f9d0a96efc1d34b0e0a72baa41e030 [file] [log] [blame]
Jack Jansenb44f1cc1998-07-31 09:44:58 +00001"""Create a standalone application from a Python script.
2
3This puts up a dialog asking for a Python source file ('TEXT').
4The output is a file with the same name but its ".py" suffix dropped.
5It is created by copying an applet template, all used shared libs and
6then adding 'PYC ' resources containing compiled versions of all used
7modules wirrten in Python and the main script itself, as __main__.
8"""
9
10
11import sys
12
13import string
14import os
15import macfs
16import MacOS
17import Res
18import Dlg
19import EasyDialogs
20import buildtools
21
22# Hmmm...
23MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
24if MACFREEZEPATH not in sys.path:
25 sys.path.append(MACFREEZEPATH)
26
27import macmodulefinder
28import macgen_bin
29
30# dialog, items
31DLG_ID = 400
32OK_BUTTON = 1
33CANCEL_BUTTON = 2
34GENFAT_BUTTON = 4
35GENPPC_BUTTON = 5
36GEN68K_BUTTON = 6
37
38
39try:
40 Res.GetResource('DITL', DLG_ID)
41except Res.Error:
42 Res.OpenResFile("BuildApplication.rsrc")
43else:
44 pass # we're an applet
45
46
47def main():
48 try:
49 buildapplication()
50 except buildtools.BuildError, detail:
51 EasyDialogs.Message(detail)
52
53
54def buildapplication(debug = 0):
55 buildtools.DEBUG = debug
56
57 # Ask for source text if not specified in sys.argv[1:]
58
59 if not sys.argv[1:]:
60 srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT')
61 if not ok:
62 return
63 filename = srcfss.as_pathname()
64 else:
65 if sys.argv[2:]:
66 raise buildtools.BuildError, "please select one file at a time"
67 filename = sys.argv[1]
68 tp, tf = os.path.split(filename)
69
70 # interact with user
71 architecture, ok = interact(tf)
72 if not ok:
73 return
74 if tf[-3:] == '.py':
75 tf = tf[:-3]
Jack Jansenb44f1cc1998-07-31 09:44:58 +000076 else:
77 tf = tf + '.app'
Jack Jansenb44f1cc1998-07-31 09:44:58 +000078
79 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
80 if not ok:
81 return
82 dstfilename = dstfss.as_pathname()
83
84 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
85
86
87class radio:
88
89 def __init__(self, dlg, *items):
90 self.items = {}
91 for item in items:
92 tp, h, rect = dlg.GetDialogItem(item)
93 self.items[item] = h.as_Control()
94
95 def set(self, setitem):
96 for item, ctl in self.items.items():
97 if item == setitem:
98 ctl.SetControlValue(1)
99 else:
100 ctl.SetControlValue(0)
101
102 def get(self):
103 for item, ctl in self.items.items():
104 if ctl.GetControlValue():
105 return item
106
107 def hasitem(self, item):
108 return self.items.has_key(item)
109
110
111def interact(scriptname):
112 d = Dlg.GetNewDialog(DLG_ID, -1)
113 if not d:
114 print "Can't get DLOG resource with id =", DLG_ID
115 return
116 d.SetDialogDefaultItem(OK_BUTTON)
117 d.SetDialogCancelItem(CANCEL_BUTTON)
118 Dlg.ParamText(scriptname, "", "", "")
119
120 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
121 radiogroup.set(GENFAT_BUTTON)
122
123 gentype = 'fat'
124 while 1:
125 n = Dlg.ModalDialog(None)
126 if n == OK_BUTTON or n == CANCEL_BUTTON:
127 break
128 elif radiogroup.hasitem(n):
129 radiogroup.set(n)
130 genitem = radiogroup.get()
131 del radiogroup
132 del d
133 if genitem == GENFAT_BUTTON:
134 gentype = 'fat'
135 elif genitem == GENPPC_BUTTON:
136 gentype = 'pwpc'
137 elif genitem == GEN68K_BUTTON:
138 gentype = 'm68k'
139 return gentype, n == OK_BUTTON
140
141
142if __name__ == '__main__':
143 main()