blob: c919b3f748b5c5bef5c75ce14135ef3dfade4971 [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
Just van Rossum29e51841999-11-04 10:30:13 +00007modules written in Python and the main script itself, as __main__.
Jack Jansenb44f1cc1998-07-31 09:44:58 +00008"""
9
10
11import sys
12
13import string
14import os
Jack Jansenb44f1cc1998-07-31 09:44:58 +000015import MacOS
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000016from Carbon import Res
17from Carbon import Dlg
Jack Jansenb44f1cc1998-07-31 09:44:58 +000018import EasyDialogs
19import buildtools
Jack Jansen3c06b9a2001-08-27 21:41:23 +000020import macresource
Jack Jansenb44f1cc1998-07-31 09:44:58 +000021
22# Hmmm...
23MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
24if MACFREEZEPATH not in sys.path:
25 sys.path.append(MACFREEZEPATH)
26
Jack Jansenb44f1cc1998-07-31 09:44:58 +000027import macgen_bin
28
29# dialog, items
30DLG_ID = 400
31OK_BUTTON = 1
32CANCEL_BUTTON = 2
33GENFAT_BUTTON = 4
34GENPPC_BUTTON = 5
35GEN68K_BUTTON = 6
36
Jack Jansen919d3262000-07-24 19:44:17 +000037# Define this if we cannot generate 68/fat binaries (Python 1.6)
38PPC_ONLY=1
39
Jack Jansenb44f1cc1998-07-31 09:44:58 +000040
Jack Jansen3c06b9a2001-08-27 21:41:23 +000041macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")
Jack Jansenb44f1cc1998-07-31 09:44:58 +000042
43def main():
44 try:
45 buildapplication()
46 except buildtools.BuildError, detail:
47 EasyDialogs.Message(detail)
48
49
50def buildapplication(debug = 0):
51 buildtools.DEBUG = debug
52
53 # Ask for source text if not specified in sys.argv[1:]
54
55 if not sys.argv[1:]:
Jack Jansend44a3ec2003-01-22 14:03:12 +000056 filename = EasyDialogs.AskFileForOpen(message='Select Python source:',
57 fileTypes=('TEXT',))
58 if not filename:
Jack Jansenb44f1cc1998-07-31 09:44:58 +000059 return
Jack Jansenb44f1cc1998-07-31 09:44:58 +000060 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)
65
66 # interact with user
67 architecture, ok = interact(tf)
68 if not ok:
69 return
70 if tf[-3:] == '.py':
71 tf = tf[:-3]
Jack Jansenb44f1cc1998-07-31 09:44:58 +000072 else:
73 tf = tf + '.app'
Jack Jansenb44f1cc1998-07-31 09:44:58 +000074
Jack Jansend44a3ec2003-01-22 14:03:12 +000075 dstfilename = EasyDialogs.AskFileForSate(message='Save application as:',
76 savedFileName=tf)
Jack Jansenb44f1cc1998-07-31 09:44:58 +000077 if not ok:
78 return
Jack Jansenb44f1cc1998-07-31 09:44:58 +000079
80 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
81
82
83class radio:
84
85 def __init__(self, dlg, *items):
86 self.items = {}
87 for item in items:
Jack Jansend2bf68f1999-12-23 14:33:20 +000088 ctl = dlg.GetDialogItemAsControl(item)
89 self.items[item] = ctl
Jack Jansenb44f1cc1998-07-31 09:44:58 +000090
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)
97
98 def get(self):
99 for item, ctl in self.items.items():
100 if ctl.GetControlValue():
101 return item
102
103 def hasitem(self, item):
104 return self.items.has_key(item)
105
106
107def interact(scriptname):
Jack Jansen919d3262000-07-24 19:44:17 +0000108 if PPC_ONLY:
Jack Jansenf0d75082000-12-14 23:34:15 +0000109 return 'pwpc', 1
Jack Jansenb44f1cc1998-07-31 09:44:58 +0000110 d = Dlg.GetNewDialog(DLG_ID, -1)
111 if not d:
Jack Jansenf0d75082000-12-14 23:34:15 +0000112 raise "Can't get DLOG resource with id =", DLG_ID
Jack Jansenb44f1cc1998-07-31 09:44:58 +0000113 d.SetDialogDefaultItem(OK_BUTTON)
114 d.SetDialogCancelItem(CANCEL_BUTTON)
115 Dlg.ParamText(scriptname, "", "", "")
116
117 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
118 radiogroup.set(GENFAT_BUTTON)
119
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
137
138
139if __name__ == '__main__':
140 main()