blob: ca9b252b39ebce8a60f70d7a19f5f2ce67e5ad38 [file] [log] [blame]
Jack Jansen813c9971998-07-31 09:42:35 +00001"""tools for BuildApplet and BuildApplication"""
2
3import sys
4import os
5import string
6import imp
7import marshal
8import macfs
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00009from Carbon import Res
Jack Jansen813c9971998-07-31 09:42:35 +000010import MACFS
11import MacOS
12import macostools
Jack Jansenb2e33fe2002-03-29 21:21:28 +000013import macresource
Jack Jansen813c9971998-07-31 09:42:35 +000014import EasyDialogs
Jack Jansenb2e33fe2002-03-29 21:21:28 +000015import shutil
Jack Jansen813c9971998-07-31 09:42:35 +000016
17
18BuildError = "BuildError"
19
Jack Jansen813c9971998-07-31 09:42:35 +000020# .pyc file (and 'PYC ' resource magic number)
21MAGIC = imp.get_magic()
22
23# Template file (searched on sys.path)
Jack Jansen3b805261999-02-14 23:12:06 +000024TEMPLATE = "PythonInterpreter"
Jack Jansen813c9971998-07-31 09:42:35 +000025
26# Specification of our resource
27RESTYPE = 'PYC '
28RESNAME = '__main__'
29
30# A resource with this name sets the "owner" (creator) of the destination
Jack Jansen81da9f11999-03-17 22:57:55 +000031# It should also have ID=0. Either of these alone is not enough.
Jack Jansen813c9971998-07-31 09:42:35 +000032OWNERNAME = "owner resource"
33
Jack Jansen81da9f11999-03-17 22:57:55 +000034# Default applet creator code
35DEFAULT_APPLET_CREATOR="Pyta"
36
Jack Jansen813c9971998-07-31 09:42:35 +000037# OpenResFile mode parameters
38READ = 1
39WRITE = 2
40
41
Jack Jansena4f8e582001-02-17 23:30:19 +000042def findtemplate(template=None):
Jack Jansen813c9971998-07-31 09:42:35 +000043 """Locate the applet template along sys.path"""
Jack Jansenb2e33fe2002-03-29 21:21:28 +000044 if MacOS.runtimemodel == 'macho':
45 if template:
46 return template
47 return findtemplate_macho()
Jack Jansena4f8e582001-02-17 23:30:19 +000048 if not template:
49 template=TEMPLATE
Jack Jansen813c9971998-07-31 09:42:35 +000050 for p in sys.path:
Jack Jansena4f8e582001-02-17 23:30:19 +000051 file = os.path.join(p, template)
Jack Jansen813c9971998-07-31 09:42:35 +000052 try:
Jack Jansena4f8e582001-02-17 23:30:19 +000053 file, d1, d2 = macfs.ResolveAliasFile(file)
Jack Jansen813c9971998-07-31 09:42:35 +000054 break
55 except (macfs.error, ValueError):
56 continue
57 else:
Jack Jansena4f8e582001-02-17 23:30:19 +000058 raise BuildError, "Template %s not found on sys.path" % `template`
59 file = file.as_pathname()
60 return file
Jack Jansenb2e33fe2002-03-29 21:21:28 +000061
62def findtemplate_macho():
63 execpath = sys.executable.split('/')
64 if not 'Contents' in execpath:
65 raise BuildError, "Not running from a .app bundle: %s" % sys.executable
66 i = execpath.index('Contents')
67 return '/'.join(execpath[:i])
Jack Jansen813c9971998-07-31 09:42:35 +000068
69
Jack Jansen388fbf32002-06-09 22:08:52 +000070def process(template, filename, destname, copy_codefragment,
71 rsrcname=None, others=[], raw=0, progress="default"):
Jack Jansen813c9971998-07-31 09:42:35 +000072
Jack Jansen388fbf32002-06-09 22:08:52 +000073 if progress == "default":
Jack Jansen813c9971998-07-31 09:42:35 +000074 progress = EasyDialogs.ProgressBar("Processing %s..."%os.path.split(filename)[1], 120)
75 progress.label("Compiling...")
Jack Jansen388fbf32002-06-09 22:08:52 +000076 progress.inc(0)
Jack Jansen813c9971998-07-31 09:42:35 +000077
78 # Read the source and compile it
79 # (there's no point overwriting the destination if it has a syntax error)
80
Jack Jansen5d099042002-06-20 20:42:07 +000081 fp = open(filename, 'rU')
Jack Jansen813c9971998-07-31 09:42:35 +000082 text = fp.read()
83 fp.close()
84 try:
85 code = compile(text, filename, "exec")
Jack Jansen2eb4b182002-08-02 14:04:15 +000086 except SyntaxError, arg:
87 raise BuildError, "Syntax error in script %s: %s" % (filename, arg)
88 except EOFError:
89 raise BuildError, "End-of-file in script %s" % (filename,)
Jack Jansen813c9971998-07-31 09:42:35 +000090
Jack Jansen388fbf32002-06-09 22:08:52 +000091 # Set the destination file name. Note that basename
92 # does contain the whole filepath, only a .py is stripped.
Jack Jansen813c9971998-07-31 09:42:35 +000093
94 if string.lower(filename[-3:]) == ".py":
Jack Jansen388fbf32002-06-09 22:08:52 +000095 basename = filename[:-3]
96 if MacOS.runtimemodel != 'macho' and not destname:
97 destname = basename
Jack Jansen813c9971998-07-31 09:42:35 +000098 else:
Jack Jansen388fbf32002-06-09 22:08:52 +000099 basename = filename
100
101 if not destname:
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000102 if MacOS.runtimemodel == 'macho':
Jack Jansen388fbf32002-06-09 22:08:52 +0000103 destname = basename + '.app'
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000104 else:
Jack Jansen388fbf32002-06-09 22:08:52 +0000105 destname = basename + '.applet'
106 if not rsrcname:
107 rsrcname = basename + '.rsrc'
108
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000109 # Try removing the output file. This fails in MachO, but it should
110 # do any harm.
Jack Jansen813c9971998-07-31 09:42:35 +0000111 try:
112 os.remove(destname)
113 except os.error:
114 pass
Jack Jansen388fbf32002-06-09 22:08:52 +0000115 process_common(template, progress, code, rsrcname, destname, 0,
116 copy_codefragment, raw, others)
Jack Jansen813c9971998-07-31 09:42:35 +0000117
118
119def update(template, filename, output):
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000120 if MacOS.runtimemodel == 'macho':
121 raise BuildError, "No updating yet for MachO applets"
Jack Jansen388fbf32002-06-09 22:08:52 +0000122 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000123 progress = EasyDialogs.ProgressBar("Updating %s..."%os.path.split(filename)[1], 120)
124 else:
125 progress = None
126 if not output:
127 output = filename + ' (updated)'
128
129 # Try removing the output file
130 try:
131 os.remove(output)
132 except os.error:
133 pass
134 process_common(template, progress, None, filename, output, 1, 1)
135
136
Jack Jansen388fbf32002-06-09 22:08:52 +0000137def process_common(template, progress, code, rsrcname, destname, is_update,
138 copy_codefragment, raw=0, others=[]):
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000139 if MacOS.runtimemodel == 'macho':
Jack Jansen388fbf32002-06-09 22:08:52 +0000140 return process_common_macho(template, progress, code, rsrcname, destname,
141 is_update, raw, others)
142 if others:
143 raise BuildError, "Extra files only allowed for MachoPython applets"
Jack Jansen813c9971998-07-31 09:42:35 +0000144 # Create FSSpecs for the various files
145 template_fss = macfs.FSSpec(template)
146 template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
147 dest_fss = macfs.FSSpec(destname)
148
149 # Copy data (not resources, yet) from the template
Jack Jansen388fbf32002-06-09 22:08:52 +0000150 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000151 progress.label("Copy data fork...")
152 progress.set(10)
153
154 if copy_codefragment:
155 tmpl = open(template, "rb")
156 dest = open(destname, "wb")
157 data = tmpl.read()
158 if data:
159 dest.write(data)
160 dest.close()
161 tmpl.close()
162 del dest
163 del tmpl
164
165 # Open the output resource fork
166
Jack Jansen388fbf32002-06-09 22:08:52 +0000167 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000168 progress.label("Copy resources...")
169 progress.set(20)
170 try:
171 output = Res.FSpOpenResFile(dest_fss, WRITE)
172 except MacOS.Error:
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000173 Res.FSpCreateResFile(destname, '????', 'APPL', MACFS.smAllScripts)
Jack Jansen813c9971998-07-31 09:42:35 +0000174 output = Res.FSpOpenResFile(dest_fss, WRITE)
175
176 # Copy the resources from the target specific resource template, if any
177 typesfound, ownertype = [], None
178 try:
179 input = Res.FSpOpenResFile(rsrcname, READ)
180 except (MacOS.Error, ValueError):
181 pass
Jack Jansen388fbf32002-06-09 22:08:52 +0000182 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000183 progress.inc(50)
184 else:
185 if is_update:
186 skip_oldfile = ['cfrg']
187 else:
188 skip_oldfile = []
189 typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress)
190 Res.CloseResFile(input)
191
192 # Check which resource-types we should not copy from the template
Jack Jansen81da9f11999-03-17 22:57:55 +0000193 skiptypes = []
194 if 'vers' in typesfound: skiptypes.append('vers')
Jack Jansen813c9971998-07-31 09:42:35 +0000195 if 'SIZE' in typesfound: skiptypes.append('SIZE')
196 if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
197 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
198 if not copy_codefragment:
199 skiptypes.append('cfrg')
Jack Jansen81da9f11999-03-17 22:57:55 +0000200## skipowner = (ownertype <> None)
Jack Jansen813c9971998-07-31 09:42:35 +0000201
202 # Copy the resources from the template
203
204 input = Res.FSpOpenResFile(template_fss, READ)
Jack Jansen81da9f11999-03-17 22:57:55 +0000205 dummy, tmplowner = copyres(input, output, skiptypes, 1, progress)
206
Jack Jansen813c9971998-07-31 09:42:35 +0000207 Res.CloseResFile(input)
Jack Jansen81da9f11999-03-17 22:57:55 +0000208## if ownertype == None:
209## raise BuildError, "No owner resource found in either resource file or template"
Jack Jansen813c9971998-07-31 09:42:35 +0000210 # Make sure we're manipulating the output resource file now
211
212 Res.UseResFile(output)
Jack Jansen81da9f11999-03-17 22:57:55 +0000213
214 if ownertype == None:
215 # No owner resource in the template. We have skipped the
216 # Python owner resource, so we have to add our own. The relevant
217 # bundle stuff is already included in the interpret/applet template.
218 newres = Res.Resource('\0')
219 newres.AddResource(DEFAULT_APPLET_CREATOR, 0, "Owner resource")
220 ownertype = DEFAULT_APPLET_CREATOR
Jack Jansen813c9971998-07-31 09:42:35 +0000221
222 if code:
223 # Delete any existing 'PYC ' resource named __main__
224
225 try:
226 res = Res.Get1NamedResource(RESTYPE, RESNAME)
227 res.RemoveResource()
228 except Res.Error:
229 pass
230
231 # Create the raw data for the resource from the code object
Jack Jansen388fbf32002-06-09 22:08:52 +0000232 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000233 progress.label("Write PYC resource...")
234 progress.set(120)
235
236 data = marshal.dumps(code)
237 del code
238 data = (MAGIC + '\0\0\0\0') + data
239
240 # Create the resource and write it
241
242 id = 0
243 while id < 128:
244 id = Res.Unique1ID(RESTYPE)
245 res = Res.Resource(data)
246 res.AddResource(RESTYPE, id, RESNAME)
Just van Rossum874f87b1999-01-30 22:31:26 +0000247 attrs = res.GetResAttrs()
248 attrs = attrs | 0x04 # set preload
249 res.SetResAttrs(attrs)
Jack Jansen813c9971998-07-31 09:42:35 +0000250 res.WriteResource()
251 res.ReleaseResource()
252
253 # Close the output file
254
255 Res.CloseResFile(output)
256
257 # Now set the creator, type and bundle bit of the destination
258 dest_finfo = dest_fss.GetFInfo()
259 dest_finfo.Creator = ownertype
260 dest_finfo.Type = 'APPL'
Jack Jansenb70699b1999-12-03 23:38:05 +0000261 dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle | MACFS.kIsShared
Jack Jansen813c9971998-07-31 09:42:35 +0000262 dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
263 dest_fss.SetFInfo(dest_finfo)
264
265 macostools.touched(dest_fss)
Jack Jansen388fbf32002-06-09 22:08:52 +0000266 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000267 progress.label("Done.")
Jack Jansen388fbf32002-06-09 22:08:52 +0000268 progress.inc(0)
Jack Jansen813c9971998-07-31 09:42:35 +0000269
Jack Jansen388fbf32002-06-09 22:08:52 +0000270def process_common_macho(template, progress, code, rsrcname, destname, is_update, raw=0, others=[]):
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000271 # First make sure the name ends in ".app"
272 if destname[-4:] != '.app':
273 destname = destname + '.app'
274 # Now deduce the short name
275 shortname = os.path.split(destname)[1]
276 if shortname[-4:] == '.app':
277 # Strip the .app suffix
278 shortname = shortname[:-4]
Jack Jansen9aa8fd02002-03-29 23:44:37 +0000279 # And deduce the .plist and .icns names
280 plistname = None
281 icnsname = None
282 if rsrcname and rsrcname[-5:] == '.rsrc':
283 tmp = rsrcname[:-5]
284 plistname = tmp + '.plist'
285 if os.path.exists(plistname):
286 icnsname = tmp + '.icns'
287 if not os.path.exists(icnsname):
288 icnsname = None
289 else:
290 plistname = None
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000291 # Start with copying the .app framework
292 if not is_update:
293 exceptlist = ["Contents/Info.plist",
294 "Contents/Resources/English.lproj/InfoPlist.strings",
295 "Contents/Resources/python.rsrc",
296 ]
Jack Jansen388fbf32002-06-09 22:08:52 +0000297 copyapptree(template, destname, exceptlist, progress)
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000298 # Now either use the .plist file or the default
Jack Jansen388fbf32002-06-09 22:08:52 +0000299 if progress:
300 progress.label('Create info.plist')
301 progress.inc(0)
Jack Jansen9aa8fd02002-03-29 23:44:37 +0000302 if plistname:
Jack Jansen388fbf32002-06-09 22:08:52 +0000303 shutil.copy2(plistname, os.path.join(destname, 'Contents', 'Info.plist'))
Jack Jansen9aa8fd02002-03-29 23:44:37 +0000304 if icnsname:
305 icnsdest = os.path.split(icnsname)[1]
306 icnsdest = os.path.join(destname,
Jack Jansen388fbf32002-06-09 22:08:52 +0000307 os.path.join('Contents', 'Resources', icnsdest))
Jack Jansen9aa8fd02002-03-29 23:44:37 +0000308 shutil.copy2(icnsname, icnsdest)
309 # XXXX Wrong. This should be parsed from plist file. Also a big hack:-)
310 if shortname == 'PythonIDE':
311 ownertype = 'Pide'
312 else:
313 ownertype = 'PytA'
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000314 # XXXX Should copy .icns file
315 else:
Jack Jansen388fbf32002-06-09 22:08:52 +0000316 cocoainfo = ''
317 for o in others:
318 if o[-4:] == '.nib':
319 nibname = os.path.split(o)[1][:-4]
320 cocoainfo = """
321 <key>NSMainNibFile</key>
322 <string>%s</string>
323 <key>NSPrincipalClass</key>
324 <string>NSApplication</string>""" % nibname
Jack Jansen94caa782002-08-05 22:06:29 +0000325 elif o[-6:] == '.lproj':
326 files = os.listdir(o)
327 for f in files:
328 if f[-4:] == '.nib':
329 nibname = os.path.split(f)[1][:-4]
330 cocoainfo = """
331 <key>NSMainNibFile</key>
332 <string>%s</string>
333 <key>NSPrincipalClass</key>
334 <string>NSApplication</string>""" % nibname
Jack Jansen388fbf32002-06-09 22:08:52 +0000335
336 plistname = os.path.join(template, 'Contents', 'Resources', 'Applet-Info.plist')
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000337 plistdata = open(plistname).read()
Jack Jansen388fbf32002-06-09 22:08:52 +0000338 plistdata = plistdata % {'appletname':shortname, 'cocoainfo':cocoainfo}
339 ofp = open(os.path.join(destname, 'Contents', 'Info.plist'), 'w')
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000340 ofp.write(plistdata)
341 ofp.close()
342 ownertype = 'PytA'
343 # Create the PkgInfo file
Jack Jansen388fbf32002-06-09 22:08:52 +0000344 if progress:
345 progress.label('Create PkgInfo')
346 progress.inc(0)
347 ofp = open(os.path.join(destname, 'Contents', 'PkgInfo'), 'wb')
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000348 ofp.write('APPL' + ownertype)
349 ofp.close()
350
351
Jack Jansen388fbf32002-06-09 22:08:52 +0000352 if progress:
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000353 progress.label("Copy resources...")
354 progress.set(20)
Jack Jansen2eb4b182002-08-02 14:04:15 +0000355 resfilename = 'python.rsrc' # XXXX later: '%s.rsrc' % shortname
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000356 try:
357 output = Res.FSOpenResourceFile(
Jack Jansen388fbf32002-06-09 22:08:52 +0000358 os.path.join(destname, 'Contents', 'Resources', resfilename),
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000359 u'', WRITE)
360 except MacOS.Error:
361 fsr, dummy = Res.FSCreateResourceFile(
Jack Jansen388fbf32002-06-09 22:08:52 +0000362 os.path.join(destname, 'Contents', 'Resources'),
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000363 unicode(resfilename), '')
364 output = Res.FSOpenResourceFile(fsr, u'', WRITE)
365
366 # Copy the resources from the target specific resource template, if any
367 typesfound, ownertype = [], None
368 try:
369 input = macresource.open_pathname(rsrcname)
370 except (MacOS.Error, ValueError):
371 pass
Jack Jansen388fbf32002-06-09 22:08:52 +0000372 if progress:
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000373 progress.inc(50)
374 else:
375 typesfound, ownertype = copyres(input, output, [], 0, progress)
376 Res.CloseResFile(input)
377
378 # Check which resource-types we should not copy from the template
379 skiptypes = []
380## if 'vers' in typesfound: skiptypes.append('vers')
381## if 'SIZE' in typesfound: skiptypes.append('SIZE')
382## if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
383## 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
384## if not copy_codefragment:
385## skiptypes.append('cfrg')
386## skipowner = (ownertype <> None)
387
388 # Copy the resources from the template
389
390 input = Res.FSOpenResourceFile(
Jack Jansen388fbf32002-06-09 22:08:52 +0000391 os.path.join(template, 'Contents', 'Resources', 'python.rsrc'), u'', READ)
392 if progress:
393 progress.label("Copy standard resources...")
394 progress.inc(0)
395## dummy, tmplowner = copyres(input, output, skiptypes, 1, progress)
396 dummy, tmplowner = copyres(input, output, skiptypes, 1, None)
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000397
398 Res.CloseResFile(input)
399## if ownertype == None:
400## raise BuildError, "No owner resource found in either resource file or template"
401 # Make sure we're manipulating the output resource file now
402
403 Res.CloseResFile(output)
404
405 if code:
Jack Jansen388fbf32002-06-09 22:08:52 +0000406 if raw:
407 pycname = '__rawmain__.pyc'
408 else:
409 pycname = '__main__.pyc'
Jack Jansenba1c13d2002-08-02 14:57:43 +0000410 # And we also create __rawmain__.pyc
411 outputfilename = os.path.join(destname, 'Contents', 'Resources', '__rawmain__.pyc')
412 if progress:
413 progress.label('Creating __rawmain__.pyc')
414 progress.inc(0)
415 rawsourcefile = os.path.join(sys.prefix, 'Mac', 'Lib', 'appletrawmain.py')
416 rawsource = open(rawsourcefile, 'rU').read()
417 rawcode = compile(rawsource, rawsourcefile, 'exec')
418 writepycfile(rawcode, outputfilename)
419
Jack Jansen388fbf32002-06-09 22:08:52 +0000420 outputfilename = os.path.join(destname, 'Contents', 'Resources', pycname)
421 if progress:
422 progress.label('Creating '+pycname)
423 progress.inc(0)
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000424 writepycfile(code, outputfilename)
Jack Jansen388fbf32002-06-09 22:08:52 +0000425 # Copy other files the user asked for
426 for osrc in others:
427 oname = os.path.split(osrc)[1]
428 odst = os.path.join(destname, 'Contents', 'Resources', oname)
429 if progress:
430 progress.label('Copy ' + oname)
431 progress.inc(0)
432 if os.path.isdir(osrc):
433 copyapptree(osrc, odst)
434 else:
435 shutil.copy2(osrc, odst)
436 if progress:
437 progress.label('Done.')
438 progress.inc(0)
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000439
440## macostools.touched(dest_fss)
Jack Jansen813c9971998-07-31 09:42:35 +0000441
442# Copy resources between two resource file descriptors.
Jack Jansen81da9f11999-03-17 22:57:55 +0000443# skip a resource named '__main__' or (if skipowner is set) with ID zero.
Jack Jansen813c9971998-07-31 09:42:35 +0000444# Also skip resources with a type listed in skiptypes.
445#
446def copyres(input, output, skiptypes, skipowner, progress=None):
447 ctor = None
448 alltypes = []
449 Res.UseResFile(input)
450 ntypes = Res.Count1Types()
451 progress_type_inc = 50/ntypes
452 for itype in range(1, 1+ntypes):
453 type = Res.Get1IndType(itype)
454 if type in skiptypes:
455 continue
456 alltypes.append(type)
457 nresources = Res.Count1Resources(type)
458 progress_cur_inc = progress_type_inc/nresources
459 for ires in range(1, 1+nresources):
460 res = Res.Get1IndResource(type, ires)
461 id, type, name = res.GetResInfo()
462 lcname = string.lower(name)
Jack Jansen81da9f11999-03-17 22:57:55 +0000463
464 if lcname == OWNERNAME and id == 0:
Jack Jansen813c9971998-07-31 09:42:35 +0000465 if skipowner:
466 continue # Skip this one
467 else:
468 ctor = type
469 size = res.size
470 attrs = res.GetResAttrs()
Jack Jansen388fbf32002-06-09 22:08:52 +0000471 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000472 progress.label("Copy %s %d %s"%(type, id, name))
473 progress.inc(progress_cur_inc)
474 res.LoadResource()
475 res.DetachResource()
476 Res.UseResFile(output)
477 try:
478 res2 = Res.Get1Resource(type, id)
479 except MacOS.Error:
480 res2 = None
481 if res2:
Jack Jansen388fbf32002-06-09 22:08:52 +0000482 if progress:
Jack Jansen813c9971998-07-31 09:42:35 +0000483 progress.label("Overwrite %s %d %s"%(type, id, name))
Jack Jansen388fbf32002-06-09 22:08:52 +0000484 progress.inc(0)
Jack Jansen813c9971998-07-31 09:42:35 +0000485 res2.RemoveResource()
486 res.AddResource(type, id, name)
487 res.WriteResource()
488 attrs = attrs | res.GetResAttrs()
489 res.SetResAttrs(attrs)
490 Res.UseResFile(input)
491 return alltypes, ctor
492
Jack Jansen388fbf32002-06-09 22:08:52 +0000493def copyapptree(srctree, dsttree, exceptlist=[], progress=None):
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000494 names = []
495 if os.path.exists(dsttree):
496 shutil.rmtree(dsttree)
497 os.mkdir(dsttree)
498 todo = os.listdir(srctree)
499 while todo:
500 this, todo = todo[0], todo[1:]
501 if this in exceptlist:
502 continue
503 thispath = os.path.join(srctree, this)
504 if os.path.isdir(thispath):
505 thiscontent = os.listdir(thispath)
506 for t in thiscontent:
507 todo.append(os.path.join(this, t))
508 names.append(this)
509 for this in names:
510 srcpath = os.path.join(srctree, this)
511 dstpath = os.path.join(dsttree, this)
512 if os.path.isdir(srcpath):
513 os.mkdir(dstpath)
514 else:
Jack Jansen388fbf32002-06-09 22:08:52 +0000515 if progress:
516 progress.label('Copy '+this)
517 progress.inc(0)
Jack Jansenb2e33fe2002-03-29 21:21:28 +0000518 shutil.copy2(srcpath, dstpath)
519
520def writepycfile(codeobject, cfile):
521 import marshal
522 fc = open(cfile, 'wb')
523 fc.write('\0\0\0\0') # MAGIC placeholder, written later
524 fc.write('\0\0\0\0') # Timestap placeholder, not needed
525 marshal.dump(codeobject, fc)
526 fc.flush()
527 fc.seek(0, 0)
528 fc.write(MAGIC)
529 fc.close()
Jack Jansen813c9971998-07-31 09:42:35 +0000530