blob: a7c8f407a5055a67cdcc37b7726945bfa15679f3 [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
15import macfs
16import MacOS
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000017from Carbon import Res
18from Carbon import Dlg
Jack Jansenb44f1cc1998-07-31 09:44:58 +000019import EasyDialogs
20import buildtools
Jack Jansen3c06b9a2001-08-27 21:41:23 +000021import macresource
Jack Jansenb44f1cc1998-07-31 09:44:58 +000022
23# Hmmm...
24MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
25if MACFREEZEPATH not in sys.path:
26 sys.path.append(MACFREEZEPATH)
27
Jack Jansenb44f1cc1998-07-31 09:44:58 +000028import 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
Jack Jansen919d3262000-07-24 19:44:17 +000038# Define this if we cannot generate 68/fat binaries (Python 1.6)
39PPC_ONLY=1
40
Jack Jansenb44f1cc1998-07-31 09:44:58 +000041
Jack Jansen3c06b9a2001-08-27 21:41:23 +000042macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")
Jack Jansenb44f1cc1998-07-31 09:44:58 +000043
44def main():
45 try:
46 buildapplication()
47 except buildtools.BuildError, detail:
48 EasyDialogs.Message(detail)
49
50
51def buildapplication(debug = 0):
52 buildtools.DEBUG = debug
53
54 # Ask for source text if not specified in sys.argv[1:]
55
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)
66
67 # interact with user
68 architecture, ok = interact(tf)
69 if not ok:
70 return
71 if tf[-3:] == '.py':
72 tf = tf[:-3]
Jack Jansenb44f1cc1998-07-31 09:44:58 +000073 else:
74 tf = tf + '.app'
Jack Jansenb44f1cc1998-07-31 09:44:58 +000075
76 dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
77 if not ok:
78 return
79 dstfilename = dstfss.as_pathname()
80
81 macgen_bin.generate(filename, dstfilename, None, architecture, 1)
82
83
84class radio:
85
86 def __init__(self, dlg, *items):
87 self.items = {}
88 for item in items:
Jack Jansend2bf68f1999-12-23 14:33:20 +000089 ctl = dlg.GetDialogItemAsControl(item)
90 self.items[item] = ctl
Jack Jansenb44f1cc1998-07-31 09:44:58 +000091
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)
98
99 def get(self):
100 for item, ctl in self.items.items():
101 if ctl.GetControlValue():
102 return item
103
104 def hasitem(self, item):
105 return self.items.has_key(item)
106
107
108def interact(scriptname):
Jack Jansen919d3262000-07-24 19:44:17 +0000109 if PPC_ONLY:
Jack Jansenf0d75082000-12-14 23:34:15 +0000110 return 'pwpc', 1
Jack Jansenb44f1cc1998-07-31 09:44:58 +0000111 d = Dlg.GetNewDialog(DLG_ID, -1)
112 if not d:
Jack Jansenf0d75082000-12-14 23:34:15 +0000113 raise "Can't get DLOG resource with id =", DLG_ID
Jack Jansenb44f1cc1998-07-31 09:44:58 +0000114 d.SetDialogDefaultItem(OK_BUTTON)
115 d.SetDialogCancelItem(CANCEL_BUTTON)
116 Dlg.ParamText(scriptname, "", "", "")
117
118 radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
119 radiogroup.set(GENFAT_BUTTON)
120
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
138
139
140if __name__ == '__main__':
141 main()