Results of a rewrite pass
[python/dscho.git] / Lib / plat-mac / buildtools.py
blob01b5392e6c3ef884448d12ec0d9228f52983bcc1
1 """tools for BuildApplet and BuildApplication"""
3 import sys
4 import os
5 import string
6 import imp
7 import marshal
8 import macfs
9 from Carbon import Res
10 import MACFS
11 import MacOS
12 import macostools
13 import macresource
14 import EasyDialogs
15 import shutil
18 BuildError = "BuildError"
20 # .pyc file (and 'PYC ' resource magic number)
21 MAGIC = imp.get_magic()
23 # Template file (searched on sys.path)
24 TEMPLATE = "PythonInterpreter"
26 # Specification of our resource
27 RESTYPE = 'PYC '
28 RESNAME = '__main__'
30 # A resource with this name sets the "owner" (creator) of the destination
31 # It should also have ID=0. Either of these alone is not enough.
32 OWNERNAME = "owner resource"
34 # Default applet creator code
35 DEFAULT_APPLET_CREATOR="Pyta"
37 # OpenResFile mode parameters
38 READ = 1
39 WRITE = 2
42 def findtemplate(template=None):
43 """Locate the applet template along sys.path"""
44 if MacOS.runtimemodel == 'macho':
45 if template:
46 return template
47 return findtemplate_macho()
48 if not template:
49 template=TEMPLATE
50 for p in sys.path:
51 file = os.path.join(p, template)
52 try:
53 file, d1, d2 = macfs.ResolveAliasFile(file)
54 break
55 except (macfs.error, ValueError):
56 continue
57 else:
58 raise BuildError, "Template %s not found on sys.path" % `template`
59 file = file.as_pathname()
60 return file
62 def 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])
70 def process(template, filename, destname, copy_codefragment,
71 rsrcname=None, others=[], raw=0, progress="default"):
73 if progress == "default":
74 progress = EasyDialogs.ProgressBar("Processing %s..."%os.path.split(filename)[1], 120)
75 progress.label("Compiling...")
76 progress.inc(0)
77 # check for the script name being longer than 32 chars. This may trigger a bug
78 # on OSX that can destroy your sourcefile.
79 if '#' in os.path.split(filename)[1]:
80 raise BuildError, "BuildApplet could destroy your sourcefile on OSX, please rename: %s" % filename
81 # Read the source and compile it
82 # (there's no point overwriting the destination if it has a syntax error)
84 fp = open(filename, 'rU')
85 text = fp.read()
86 fp.close()
87 try:
88 code = compile(text, filename, "exec")
89 except SyntaxError, arg:
90 raise BuildError, "Syntax error in script %s: %s" % (filename, arg)
91 except EOFError:
92 raise BuildError, "End-of-file in script %s" % (filename,)
94 # Set the destination file name. Note that basename
95 # does contain the whole filepath, only a .py is stripped.
97 if string.lower(filename[-3:]) == ".py":
98 basename = filename[:-3]
99 if MacOS.runtimemodel != 'macho' and not destname:
100 destname = basename
101 else:
102 basename = filename
104 if not destname:
105 if MacOS.runtimemodel == 'macho':
106 destname = basename + '.app'
107 else:
108 destname = basename + '.applet'
109 if not rsrcname:
110 rsrcname = basename + '.rsrc'
112 # Try removing the output file. This fails in MachO, but it should
113 # do any harm.
114 try:
115 os.remove(destname)
116 except os.error:
117 pass
118 process_common(template, progress, code, rsrcname, destname, 0,
119 copy_codefragment, raw, others)
122 def update(template, filename, output):
123 if MacOS.runtimemodel == 'macho':
124 raise BuildError, "No updating yet for MachO applets"
125 if progress:
126 progress = EasyDialogs.ProgressBar("Updating %s..."%os.path.split(filename)[1], 120)
127 else:
128 progress = None
129 if not output:
130 output = filename + ' (updated)'
132 # Try removing the output file
133 try:
134 os.remove(output)
135 except os.error:
136 pass
137 process_common(template, progress, None, filename, output, 1, 1)
140 def process_common(template, progress, code, rsrcname, destname, is_update,
141 copy_codefragment, raw=0, others=[]):
142 if MacOS.runtimemodel == 'macho':
143 return process_common_macho(template, progress, code, rsrcname, destname,
144 is_update, raw, others)
145 if others:
146 raise BuildError, "Extra files only allowed for MachoPython applets"
147 # Create FSSpecs for the various files
148 template_fss = macfs.FSSpec(template)
149 template_fss, d1, d2 = macfs.ResolveAliasFile(template_fss)
150 dest_fss = macfs.FSSpec(destname)
152 # Copy data (not resources, yet) from the template
153 if progress:
154 progress.label("Copy data fork...")
155 progress.set(10)
157 if copy_codefragment:
158 tmpl = open(template, "rb")
159 dest = open(destname, "wb")
160 data = tmpl.read()
161 if data:
162 dest.write(data)
163 dest.close()
164 tmpl.close()
165 del dest
166 del tmpl
168 # Open the output resource fork
170 if progress:
171 progress.label("Copy resources...")
172 progress.set(20)
173 try:
174 output = Res.FSpOpenResFile(dest_fss, WRITE)
175 except MacOS.Error:
176 Res.FSpCreateResFile(destname, '????', 'APPL', MACFS.smAllScripts)
177 output = Res.FSpOpenResFile(dest_fss, WRITE)
179 # Copy the resources from the target specific resource template, if any
180 typesfound, ownertype = [], None
181 try:
182 input = Res.FSpOpenResFile(rsrcname, READ)
183 except (MacOS.Error, ValueError):
184 pass
185 if progress:
186 progress.inc(50)
187 else:
188 if is_update:
189 skip_oldfile = ['cfrg']
190 else:
191 skip_oldfile = []
192 typesfound, ownertype = copyres(input, output, skip_oldfile, 0, progress)
193 Res.CloseResFile(input)
195 # Check which resource-types we should not copy from the template
196 skiptypes = []
197 if 'vers' in typesfound: skiptypes.append('vers')
198 if 'SIZE' in typesfound: skiptypes.append('SIZE')
199 if 'BNDL' in typesfound: skiptypes = skiptypes + ['BNDL', 'FREF', 'icl4',
200 'icl8', 'ics4', 'ics8', 'ICN#', 'ics#']
201 if not copy_codefragment:
202 skiptypes.append('cfrg')
203 ## skipowner = (ownertype <> None)
205 # Copy the resources from the template
207 input = Res.FSpOpenResFile(template_fss, READ)
208 dummy, tmplowner = copyres(input, output, skiptypes, 1, progress)
210 Res.CloseResFile(input)
211 ## if ownertype == None:
212 ## raise BuildError, "No owner resource found in either resource file or template"
213 # Make sure we're manipulating the output resource file now
215 Res.UseResFile(output)
217 if ownertype == None:
218 # No owner resource in the template. We have skipped the
219 # Python owner resource, so we have to add our own. The relevant
220 # bundle stuff is already included in the interpret/applet template.
221 newres = Res.Resource('\0')
222 newres.AddResource(DEFAULT_APPLET_CREATOR, 0, "Owner resource")
223 ownertype = DEFAULT_APPLET_CREATOR
225 if code:
226 # Delete any existing 'PYC ' resource named __main__
228 try:
229 res = Res.Get1NamedResource(RESTYPE, RESNAME)
230 res.RemoveResource()
231 except Res.Error:
232 pass
234 # Create the raw data for the resource from the code object
235 if progress:
236 progress.label("Write PYC resource...")
237 progress.set(120)
239 data = marshal.dumps(code)
240 del code
241 data = (MAGIC + '\0\0\0\0') + data
243 # Create the resource and write it
245 id = 0
246 while id < 128:
247 id = Res.Unique1ID(RESTYPE)
248 res = Res.Resource(data)
249 res.AddResource(RESTYPE, id, RESNAME)
250 attrs = res.GetResAttrs()
251 attrs = attrs | 0x04 # set preload
252 res.SetResAttrs(attrs)
253 res.WriteResource()
254 res.ReleaseResource()
256 # Close the output file
258 Res.CloseResFile(output)
260 # Now set the creator, type and bundle bit of the destination
261 dest_finfo = dest_fss.GetFInfo()
262 dest_finfo.Creator = ownertype
263 dest_finfo.Type = 'APPL'
264 dest_finfo.Flags = dest_finfo.Flags | MACFS.kHasBundle | MACFS.kIsShared
265 dest_finfo.Flags = dest_finfo.Flags & ~MACFS.kHasBeenInited
266 dest_fss.SetFInfo(dest_finfo)
268 macostools.touched(dest_fss)
269 if progress:
270 progress.label("Done.")
271 progress.inc(0)
273 def process_common_macho(template, progress, code, rsrcname, destname, is_update, raw=0, others=[]):
274 # First make sure the name ends in ".app"
275 if destname[-4:] != '.app':
276 destname = destname + '.app'
277 # Now deduce the short name
278 shortname = os.path.split(destname)[1]
279 if shortname[-4:] == '.app':
280 # Strip the .app suffix
281 shortname = shortname[:-4]
282 # And deduce the .plist and .icns names
283 plistname = None
284 icnsname = None
285 if rsrcname and rsrcname[-5:] == '.rsrc':
286 tmp = rsrcname[:-5]
287 plistname = tmp + '.plist'
288 if os.path.exists(plistname):
289 icnsname = tmp + '.icns'
290 if not os.path.exists(icnsname):
291 icnsname = None
292 else:
293 plistname = None
294 # Start with copying the .app framework
295 if not is_update:
296 exceptlist = ["Contents/Info.plist",
297 "Contents/Resources/English.lproj/InfoPlist.strings",
298 "Contents/Resources/English.lproj/Documentation",
299 "Contents/Resources/python.rsrc",
301 copyapptree(template, destname, exceptlist, progress)
302 # SERIOUS HACK. If we've just copied a symlink as the
303 # executable we assume we're running from the MacPython addon
304 # to 10.2 python. We remove the symlink again and install
305 # the appletrunner script.
306 executable = os.path.join(destname, "Contents/MacOS/python")
307 if os.path.islink(executable):
308 os.remove(executable)
309 dummyfp, appletrunner, d2 = imp.find_module('appletrunner')
310 del dummyfp
311 shutil.copy2(appletrunner, executable)
312 os.chmod(executable, 0775)
313 # Now either use the .plist file or the default
314 if progress:
315 progress.label('Create info.plist')
316 progress.inc(0)
317 if plistname:
318 shutil.copy2(plistname, os.path.join(destname, 'Contents', 'Info.plist'))
319 if icnsname:
320 icnsdest = os.path.split(icnsname)[1]
321 icnsdest = os.path.join(destname,
322 os.path.join('Contents', 'Resources', icnsdest))
323 shutil.copy2(icnsname, icnsdest)
324 # XXXX Wrong. This should be parsed from plist file. Also a big hack:-)
325 if shortname == 'PythonIDE':
326 ownertype = 'Pide'
327 else:
328 ownertype = 'PytA'
329 # XXXX Should copy .icns file
330 else:
331 cocoainfo = ''
332 for o in others:
333 if o[-4:] == '.nib':
334 nibname = os.path.split(o)[1][:-4]
335 cocoainfo = """
336 <key>NSMainNibFile</key>
337 <string>%s</string>
338 <key>NSPrincipalClass</key>
339 <string>NSApplication</string>""" % nibname
340 elif o[-6:] == '.lproj':
341 files = os.listdir(o)
342 for f in files:
343 if f[-4:] == '.nib':
344 nibname = os.path.split(f)[1][:-4]
345 cocoainfo = """
346 <key>NSMainNibFile</key>
347 <string>%s</string>
348 <key>NSPrincipalClass</key>
349 <string>NSApplication</string>""" % nibname
351 plistname = os.path.join(template, 'Contents', 'Resources', 'Applet-Info.plist')
352 plistdata = open(plistname).read()
353 plistdata = plistdata % {'appletname':shortname, 'cocoainfo':cocoainfo}
354 ofp = open(os.path.join(destname, 'Contents', 'Info.plist'), 'w')
355 ofp.write(plistdata)
356 ofp.close()
357 ownertype = 'PytA'
358 # Create the PkgInfo file
359 if progress:
360 progress.label('Create PkgInfo')
361 progress.inc(0)
362 ofp = open(os.path.join(destname, 'Contents', 'PkgInfo'), 'wb')
363 ofp.write('APPL' + ownertype)
364 ofp.close()
367 # Copy the resources from the target specific resource template, if any
368 typesfound, ownertype = [], None
369 try:
370 input = macresource.open_pathname(rsrcname)
371 except (MacOS.Error, ValueError):
372 if progress:
373 progress.inc(50)
374 else:
375 if progress:
376 progress.label("Copy resources...")
377 progress.set(20)
378 resfilename = 'python.rsrc' # XXXX later: '%s.rsrc' % shortname
379 try:
380 output = Res.FSOpenResourceFile(
381 os.path.join(destname, 'Contents', 'Resources', resfilename),
382 u'', WRITE)
383 except MacOS.Error:
384 fsr, dummy = Res.FSCreateResourceFile(
385 os.path.join(destname, 'Contents', 'Resources'),
386 unicode(resfilename), '')
387 output = Res.FSOpenResourceFile(fsr, u'', WRITE)
389 typesfound, ownertype = copyres(input, output, [], 0, progress)
390 Res.CloseResFile(input)
391 Res.CloseResFile(output)
393 if code:
394 if raw:
395 pycname = '__rawmain__.pyc'
396 else:
397 pycname = '__main__.pyc'
398 # And we also create __rawmain__.pyc
399 outputfilename = os.path.join(destname, 'Contents', 'Resources', '__rawmain__.pyc')
400 if progress:
401 progress.label('Creating __rawmain__.pyc')
402 progress.inc(0)
403 rawsourcefp, rawsourcefile, d2 = imp.find_module('appletrawmain')
404 rawsource = rawsourcefp.read()
405 rawcode = compile(rawsource, rawsourcefile, 'exec')
406 writepycfile(rawcode, outputfilename)
408 outputfilename = os.path.join(destname, 'Contents', 'Resources', pycname)
409 if progress:
410 progress.label('Creating '+pycname)
411 progress.inc(0)
412 writepycfile(code, outputfilename)
413 # Copy other files the user asked for
414 for osrc in others:
415 oname = os.path.split(osrc)[1]
416 odst = os.path.join(destname, 'Contents', 'Resources', oname)
417 if progress:
418 progress.label('Copy ' + oname)
419 progress.inc(0)
420 if os.path.isdir(osrc):
421 copyapptree(osrc, odst)
422 else:
423 shutil.copy2(osrc, odst)
424 if progress:
425 progress.label('Done.')
426 progress.inc(0)
428 ## macostools.touched(dest_fss)
430 # Copy resources between two resource file descriptors.
431 # skip a resource named '__main__' or (if skipowner is set) with ID zero.
432 # Also skip resources with a type listed in skiptypes.
434 def copyres(input, output, skiptypes, skipowner, progress=None):
435 ctor = None
436 alltypes = []
437 Res.UseResFile(input)
438 ntypes = Res.Count1Types()
439 progress_type_inc = 50/ntypes
440 for itype in range(1, 1+ntypes):
441 type = Res.Get1IndType(itype)
442 if type in skiptypes:
443 continue
444 alltypes.append(type)
445 nresources = Res.Count1Resources(type)
446 progress_cur_inc = progress_type_inc/nresources
447 for ires in range(1, 1+nresources):
448 res = Res.Get1IndResource(type, ires)
449 id, type, name = res.GetResInfo()
450 lcname = string.lower(name)
452 if lcname == OWNERNAME and id == 0:
453 if skipowner:
454 continue # Skip this one
455 else:
456 ctor = type
457 size = res.size
458 attrs = res.GetResAttrs()
459 if progress:
460 progress.label("Copy %s %d %s"%(type, id, name))
461 progress.inc(progress_cur_inc)
462 res.LoadResource()
463 res.DetachResource()
464 Res.UseResFile(output)
465 try:
466 res2 = Res.Get1Resource(type, id)
467 except MacOS.Error:
468 res2 = None
469 if res2:
470 if progress:
471 progress.label("Overwrite %s %d %s"%(type, id, name))
472 progress.inc(0)
473 res2.RemoveResource()
474 res.AddResource(type, id, name)
475 res.WriteResource()
476 attrs = attrs | res.GetResAttrs()
477 res.SetResAttrs(attrs)
478 Res.UseResFile(input)
479 return alltypes, ctor
481 def copyapptree(srctree, dsttree, exceptlist=[], progress=None):
482 names = []
483 if os.path.exists(dsttree):
484 shutil.rmtree(dsttree)
485 os.mkdir(dsttree)
486 todo = os.listdir(srctree)
487 while todo:
488 this, todo = todo[0], todo[1:]
489 if this in exceptlist:
490 continue
491 thispath = os.path.join(srctree, this)
492 if os.path.isdir(thispath):
493 thiscontent = os.listdir(thispath)
494 for t in thiscontent:
495 todo.append(os.path.join(this, t))
496 names.append(this)
497 for this in names:
498 srcpath = os.path.join(srctree, this)
499 dstpath = os.path.join(dsttree, this)
500 if os.path.isdir(srcpath):
501 os.mkdir(dstpath)
502 elif os.path.islink(srcpath):
503 endpoint = os.readlink(srcpath)
504 os.symlink(endpoint, dstpath)
505 else:
506 if progress:
507 progress.label('Copy '+this)
508 progress.inc(0)
509 shutil.copy2(srcpath, dstpath)
511 def writepycfile(codeobject, cfile):
512 import marshal
513 fc = open(cfile, 'wb')
514 fc.write('\0\0\0\0') # MAGIC placeholder, written later
515 fc.write('\0\0\0\0') # Timestap placeholder, not needed
516 marshal.dump(codeobject, fc)
517 fc.flush()
518 fc.seek(0, 0)
519 fc.write(MAGIC)
520 fc.close()