summaryrefslogtreecommitdiffstats
path: root/Mac/scripts/BuildApplication.py
blob: a7c8f407a5055a67cdcc37b7726945bfa15679f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Create a standalone application from a Python script.

This puts up a dialog asking for a Python source file ('TEXT').
The output is a file with the same name but its ".py" suffix dropped.
It is created by copying an applet template, all used shared libs and 
then adding 'PYC ' resources containing compiled versions of all used 
modules written in Python and the main script itself, as __main__.
"""


import sys

import string
import os
import macfs
import MacOS
from Carbon import Res
from Carbon import Dlg
import EasyDialogs
import buildtools
import macresource

# Hmmm...
MACFREEZEPATH = os.path.join(sys.prefix, ":Mac:Tools:macfreeze")
if MACFREEZEPATH not in sys.path:
	sys.path.append(MACFREEZEPATH)

import macgen_bin

# dialog, items
DLG_ID = 400
OK_BUTTON = 1
CANCEL_BUTTON = 2
GENFAT_BUTTON = 4
GENPPC_BUTTON = 5
GEN68K_BUTTON = 6

# Define this if we cannot generate 68/fat binaries (Python 1.6)
PPC_ONLY=1


macresource.need('DITL', DLG_ID, "BuildApplication.rsrc")

def main():
	try:
		buildapplication()
	except buildtools.BuildError, detail:
		EasyDialogs.Message(detail)


def buildapplication(debug = 0):
	buildtools.DEBUG = debug
	
	# Ask for source text if not specified in sys.argv[1:]
	
	if not sys.argv[1:]:
		srcfss, ok = macfs.PromptGetFile('Select Python source:', 'TEXT')
		if not ok:
			return
		filename = srcfss.as_pathname()
	else:
		if sys.argv[2:]:
			raise buildtools.BuildError, "please select one file at a time"
		filename = sys.argv[1]
	tp, tf = os.path.split(filename)
	
	# interact with user
	architecture, ok = interact(tf)
	if not ok:
		return
	if tf[-3:] == '.py':
		tf = tf[:-3]
	else:
		tf = tf + '.app'
	
	dstfss, ok = macfs.StandardPutFile('Save application as:', tf)
	if not ok:
		return
	dstfilename = dstfss.as_pathname()
	
	macgen_bin.generate(filename, dstfilename, None, architecture, 1)


class radio:
	
	def __init__(self, dlg, *items):
		self.items = {}
		for item in items:
			ctl = dlg.GetDialogItemAsControl(item)
			self.items[item] = ctl
	
	def set(self, setitem):
		for item, ctl in self.items.items():
			if item == setitem:
				ctl.SetControlValue(1)
			else:
				ctl.SetControlValue(0)
	
	def get(self):
		for item, ctl in self.items.items():
			if ctl.GetControlValue():
				return item
	
	def hasitem(self, item):
		return self.items.has_key(item)


def interact(scriptname):
	if PPC_ONLY:
		return 'pwpc', 1
	d = Dlg.GetNewDialog(DLG_ID, -1)
	if not d:
		raise "Can't get DLOG resource with id =", DLG_ID
	d.SetDialogDefaultItem(OK_BUTTON)
	d.SetDialogCancelItem(CANCEL_BUTTON)
	Dlg.ParamText(scriptname, "", "", "")
	
	radiogroup = radio(d, GENFAT_BUTTON, GENPPC_BUTTON, GEN68K_BUTTON)
	radiogroup.set(GENFAT_BUTTON)
	
	gentype = 'fat'
	while 1:
		n = Dlg.ModalDialog(None)
		if n == OK_BUTTON or n == CANCEL_BUTTON:
			break
		elif radiogroup.hasitem(n):
			radiogroup.set(n)
	genitem = radiogroup.get()
	del radiogroup
	del d
	if genitem == GENFAT_BUTTON:
		gentype = 'fat'
	elif genitem == GENPPC_BUTTON:
		gentype = 'pwpc'
	elif genitem == GEN68K_BUTTON:
		gentype = 'm68k'
	return gentype, n == OK_BUTTON


if __name__ == '__main__':
	main()