blob: 3d4f3c9c14b7d65e6c4b9aa4bca25e442d637c3f [file] [log] [blame]
Just van Rossumad33d722002-11-21 10:23:04 +00001#! /usr/bin/env python
2
3"""\
4bundlebuilder.py -- Tools to assemble MacOS X (application) bundles.
5
Just van Rossumceeb9622002-11-21 23:19:37 +00006This module contains two classes to build so called "bundles" for
Just van Rossumad33d722002-11-21 10:23:04 +00007MacOS X. BundleBuilder is a general tool, AppBuilder is a subclass
Just van Rossumceeb9622002-11-21 23:19:37 +00008specialized in building application bundles.
Just van Rossumad33d722002-11-21 10:23:04 +00009
Just van Rossumceeb9622002-11-21 23:19:37 +000010[Bundle|App]Builder objects are instantiated with a bunch of keyword
11arguments, and have a build() method that will do all the work. See
12the class doc strings for a description of the constructor arguments.
13
14The module contains a main program that can be used in two ways:
15
16 % python bundlebuilder.py [options] build
17 % python buildapp.py [options] build
18
19Where "buildapp.py" is a user-supplied setup.py-like script following
20this model:
21
22 from bundlebuilder import buildapp
23 buildapp(<lots-of-keyword-args>)
Just van Rossumad33d722002-11-21 10:23:04 +000024
25"""
26
Just van Rossumad33d722002-11-21 10:23:04 +000027
Just van Rossumcef32882002-11-26 00:34:52 +000028__all__ = ["BundleBuilder", "BundleBuilderError", "AppBuilder", "buildapp"]
Just van Rossumad33d722002-11-21 10:23:04 +000029
30
31import sys
32import os, errno, shutil
Just van Rossumcef32882002-11-26 00:34:52 +000033import imp, marshal
34import re
Just van Rossumda302da2002-11-23 22:26:44 +000035from copy import deepcopy
Just van Rossumceeb9622002-11-21 23:19:37 +000036import getopt
Just van Rossumad33d722002-11-21 10:23:04 +000037from plistlib import Plist
Just van Rossumda302da2002-11-23 22:26:44 +000038from types import FunctionType as function
Jack Jansen946c1942003-02-17 16:47:12 +000039import macresource
Just van Rossumad33d722002-11-21 10:23:04 +000040
41
Just van Rossumcef32882002-11-26 00:34:52 +000042class BundleBuilderError(Exception): pass
43
44
Just van Rossumda302da2002-11-23 22:26:44 +000045class Defaults:
46
47 """Class attributes that don't start with an underscore and are
48 not functions or classmethods are (deep)copied to self.__dict__.
49 This allows for mutable default values.
50 """
51
52 def __init__(self, **kwargs):
53 defaults = self._getDefaults()
54 defaults.update(kwargs)
55 self.__dict__.update(defaults)
56
57 def _getDefaults(cls):
58 defaults = {}
59 for name, value in cls.__dict__.items():
60 if name[0] != "_" and not isinstance(value,
61 (function, classmethod)):
62 defaults[name] = deepcopy(value)
63 for base in cls.__bases__:
64 if hasattr(base, "_getDefaults"):
65 defaults.update(base._getDefaults())
66 return defaults
67 _getDefaults = classmethod(_getDefaults)
Just van Rossumad33d722002-11-21 10:23:04 +000068
69
Just van Rossumda302da2002-11-23 22:26:44 +000070class BundleBuilder(Defaults):
Just van Rossumad33d722002-11-21 10:23:04 +000071
72 """BundleBuilder is a barebones class for assembling bundles. It
73 knows nothing about executables or icons, it only copies files
74 and creates the PkgInfo and Info.plist files.
Just van Rossumad33d722002-11-21 10:23:04 +000075 """
76
Just van Rossumda302da2002-11-23 22:26:44 +000077 # (Note that Defaults.__init__ (deep)copies these values to
78 # instance variables. Mutable defaults are therefore safe.)
79
80 # Name of the bundle, with or without extension.
81 name = None
82
83 # The property list ("plist")
84 plist = Plist(CFBundleDevelopmentRegion = "English",
85 CFBundleInfoDictionaryVersion = "6.0")
86
87 # The type of the bundle.
88 type = "APPL"
89 # The creator code of the bundle.
Just van Rossume6b49022002-11-24 01:23:45 +000090 creator = None
Just van Rossumda302da2002-11-23 22:26:44 +000091
92 # List of files that have to be copied to <bundle>/Contents/Resources.
93 resources = []
94
95 # List of (src, dest) tuples; dest should be a path relative to the bundle
96 # (eg. "Contents/Resources/MyStuff/SomeFile.ext).
97 files = []
98
99 # Directory where the bundle will be assembled.
100 builddir = "build"
101
102 # platform, name of the subfolder of Contents that contains the executable.
103 platform = "MacOS"
104
105 # Make symlinks instead copying files. This is handy during debugging, but
106 # makes the bundle non-distributable.
107 symlink = 0
108
109 # Verbosity level.
110 verbosity = 1
Just van Rossumad33d722002-11-21 10:23:04 +0000111
Just van Rossumceeb9622002-11-21 23:19:37 +0000112 def setup(self):
Just van Rossumda302da2002-11-23 22:26:44 +0000113 # XXX rethink self.name munging, this is brittle.
Just van Rossumceeb9622002-11-21 23:19:37 +0000114 self.name, ext = os.path.splitext(self.name)
115 if not ext:
116 ext = ".bundle"
Just van Rossumda302da2002-11-23 22:26:44 +0000117 bundleextension = ext
Just van Rossumceeb9622002-11-21 23:19:37 +0000118 # misc (derived) attributes
Just van Rossumda302da2002-11-23 22:26:44 +0000119 self.bundlepath = pathjoin(self.builddir, self.name + bundleextension)
Just van Rossumceeb9622002-11-21 23:19:37 +0000120 self.execdir = pathjoin("Contents", self.platform)
121
Just van Rossumda302da2002-11-23 22:26:44 +0000122 plist = self.plist
Just van Rossumceeb9622002-11-21 23:19:37 +0000123 plist.CFBundleName = self.name
124 plist.CFBundlePackageType = self.type
Just van Rossume6b49022002-11-24 01:23:45 +0000125 if self.creator is None:
126 if hasattr(plist, "CFBundleSignature"):
127 self.creator = plist.CFBundleSignature
128 else:
129 self.creator = "????"
Just van Rossumceeb9622002-11-21 23:19:37 +0000130 plist.CFBundleSignature = self.creator
Just van Rossum9896ea22003-01-13 23:30:04 +0000131 if not hasattr(plist, "CFBundleIdentifier"):
132 plist.CFBundleIdentifier = self.name
Just van Rossumceeb9622002-11-21 23:19:37 +0000133
Just van Rossumad33d722002-11-21 10:23:04 +0000134 def build(self):
135 """Build the bundle."""
136 builddir = self.builddir
137 if builddir and not os.path.exists(builddir):
138 os.mkdir(builddir)
139 self.message("Building %s" % repr(self.bundlepath), 1)
140 if os.path.exists(self.bundlepath):
141 shutil.rmtree(self.bundlepath)
142 os.mkdir(self.bundlepath)
143 self.preProcess()
144 self._copyFiles()
145 self._addMetaFiles()
146 self.postProcess()
Just van Rossum535ffa22002-11-29 20:06:52 +0000147 self.message("Done.", 1)
Just van Rossumad33d722002-11-21 10:23:04 +0000148
149 def preProcess(self):
150 """Hook for subclasses."""
151 pass
152 def postProcess(self):
153 """Hook for subclasses."""
154 pass
155
156 def _addMetaFiles(self):
157 contents = pathjoin(self.bundlepath, "Contents")
158 makedirs(contents)
159 #
160 # Write Contents/PkgInfo
161 assert len(self.type) == len(self.creator) == 4, \
162 "type and creator must be 4-byte strings."
163 pkginfo = pathjoin(contents, "PkgInfo")
164 f = open(pkginfo, "wb")
165 f.write(self.type + self.creator)
166 f.close()
167 #
168 # Write Contents/Info.plist
Just van Rossumad33d722002-11-21 10:23:04 +0000169 infoplist = pathjoin(contents, "Info.plist")
Just van Rossumceeb9622002-11-21 23:19:37 +0000170 self.plist.write(infoplist)
Just van Rossumad33d722002-11-21 10:23:04 +0000171
172 def _copyFiles(self):
173 files = self.files[:]
174 for path in self.resources:
175 files.append((path, pathjoin("Contents", "Resources",
176 os.path.basename(path))))
177 if self.symlink:
178 self.message("Making symbolic links", 1)
179 msg = "Making symlink from"
180 else:
181 self.message("Copying files", 1)
182 msg = "Copying"
Just van Rossumcef32882002-11-26 00:34:52 +0000183 files.sort()
Just van Rossumad33d722002-11-21 10:23:04 +0000184 for src, dst in files:
Just van Rossumceeb9622002-11-21 23:19:37 +0000185 if os.path.isdir(src):
186 self.message("%s %s/ to %s/" % (msg, src, dst), 2)
187 else:
188 self.message("%s %s to %s" % (msg, src, dst), 2)
Just van Rossumad33d722002-11-21 10:23:04 +0000189 dst = pathjoin(self.bundlepath, dst)
190 if self.symlink:
191 symlink(src, dst, mkdirs=1)
Jack Jansen946c1942003-02-17 16:47:12 +0000192 elif os.path.splitext(src)[1] == '.rsrc':
193 macresource.install(src, dst, mkdirs=1)
Just van Rossumad33d722002-11-21 10:23:04 +0000194 else:
195 copy(src, dst, mkdirs=1)
196
197 def message(self, msg, level=0):
198 if level <= self.verbosity:
Just van Rossumceeb9622002-11-21 23:19:37 +0000199 indent = ""
200 if level > 1:
201 indent = (level - 1) * " "
202 sys.stderr.write(indent + msg + "\n")
203
204 def report(self):
205 # XXX something decent
Just van Rossum74bdca82002-11-28 11:30:56 +0000206 pass
Just van Rossumad33d722002-11-21 10:23:04 +0000207
208
Just van Rossumcef32882002-11-26 00:34:52 +0000209if __debug__:
210 PYC_EXT = ".pyc"
211else:
212 PYC_EXT = ".pyo"
213
214MAGIC = imp.get_magic()
Just van Rossum109ecbf2003-01-02 13:13:01 +0000215USE_ZIPIMPORT = "zipimport" in sys.builtin_module_names
Just van Rossumcef32882002-11-26 00:34:52 +0000216
217# For standalone apps, we have our own minimal site.py. We don't need
218# all the cruft of the real site.py.
219SITE_PY = """\
220import sys
221del sys.path[1:] # sys.path[0] is Contents/Resources/
222"""
223
Just van Rossum109ecbf2003-01-02 13:13:01 +0000224if USE_ZIPIMPORT:
225 ZIP_ARCHIVE = "Modules.zip"
226 SITE_PY += "sys.path.append(sys.path[0] + '/%s')\n" % ZIP_ARCHIVE
227 def getPycData(fullname, code, ispkg):
228 if ispkg:
229 fullname += ".__init__"
230 path = fullname.replace(".", os.sep) + PYC_EXT
231 return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
Just van Rossumcef32882002-11-26 00:34:52 +0000232
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000233#
234# The following snippet gets added as sitecustomize.py[co] to
235# non-standalone apps and appended to our custom site.py for
236# standalone apps. The bootstrap scripts calls os.execve() with
237# an argv[0] that's different from the actual executable: argv[0]
238# is the bootstrap script itself and matches the CFBundleExecutable
239# value in the Info.plist. This is needed to keep the Finder happy
240# and have the app work from the command line as well. However,
241# this causes sys.executable to also be that value, so we correct
242# that from the PYTHONEXECUTABLE environment variable that the
243# bootstrap script sets.
244#
Just van Rossum7322b1a2003-02-25 20:15:40 +0000245SITECUSTOMIZE_PY = """\
246import sys, os
247executable = os.getenv("PYTHONEXECUTABLE")
248if executable is not None:
249 sys.executable = executable
250"""
251
252SITE_PY += SITECUSTOMIZE_PY
Just van Rossum74bdca82002-11-28 11:30:56 +0000253SITE_CO = compile(SITE_PY, "<-bundlebuilder.py->", "exec")
Just van Rossum7322b1a2003-02-25 20:15:40 +0000254SITECUSTOMIZE_CO = compile(SITECUSTOMIZE_PY, "<-bundlebuilder.py->", "exec")
255
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000256#
257# Extension modules can't be in the modules zip archive, so a placeholder
258# is added instead, that loads the extension from a specified location.
259#
Just van Rossum535ffa22002-11-29 20:06:52 +0000260EXT_LOADER = """\
Just van Rossum109ecbf2003-01-02 13:13:01 +0000261def __load():
262 import imp, sys, os
263 for p in sys.path:
264 path = os.path.join(p, "%(filename)s")
265 if os.path.exists(path):
266 break
267 else:
268 assert 0, "file not found: %(filename)s"
269 mod = imp.load_dynamic("%(name)s", path)
270
271__load()
272del __load
Just van Rossum535ffa22002-11-29 20:06:52 +0000273"""
274
Just van Rossumcef32882002-11-26 00:34:52 +0000275MAYMISS_MODULES = ['mac', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
276 'win32api', 'ce', '_winreg', 'nturl2path', 'sitecustomize',
277 'org.python.core', 'riscos', 'riscosenviron', 'riscospath'
278]
279
280STRIP_EXEC = "/usr/bin/strip"
281
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000282#
283# We're using a stock interpreter to run the app, yet we need
284# a way to pass the Python main program to the interpreter. The
285# bootstrapping script fires up the interpreter with the right
286# arguments. os.execve() is used as OSX doesn't like us to
287# start a real new process. Also, the executable name must match
288# the CFBundleExecutable value in the Info.plist, so we lie
289# deliberately with argv[0]. The actual Python executable is
290# passed in an environment variable so we can "repair"
291# sys.executable later.
292#
Just van Rossum74bdca82002-11-28 11:30:56 +0000293BOOTSTRAP_SCRIPT = """\
Just van Rossum7322b1a2003-02-25 20:15:40 +0000294#!/usr/bin/env python
Just van Rossumad33d722002-11-21 10:23:04 +0000295
Just van Rossum7322b1a2003-02-25 20:15:40 +0000296import sys, os
297execdir = os.path.dirname(sys.argv[0])
298executable = os.path.join(execdir, "%(executable)s")
299resdir = os.path.join(os.path.dirname(execdir), "Resources")
300mainprogram = os.path.join(resdir, "%(mainprogram)s")
301
302sys.argv.insert(1, mainprogram)
303os.environ["PYTHONPATH"] = resdir
304os.environ["PYTHONEXECUTABLE"] = executable
305os.execve(executable, sys.argv, os.environ)
Just van Rossumad33d722002-11-21 10:23:04 +0000306"""
307
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000308
309#
310# Optional wrapper that converts "dropped files" into sys.argv values.
311#
312ARGV_EMULATOR = """\
Jack Jansena03adde2003-02-18 23:29:46 +0000313import argvemulator, os
314
315argvemulator.ArgvCollector().mainloop()
316execfile(os.path.join(os.path.split(__file__)[0], "%(realmainprogram)s"))
317"""
Just van Rossumcef32882002-11-26 00:34:52 +0000318
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000319
Just van Rossumad33d722002-11-21 10:23:04 +0000320class AppBuilder(BundleBuilder):
321
Just van Rossumda302da2002-11-23 22:26:44 +0000322 # A Python main program. If this argument is given, the main
323 # executable in the bundle will be a small wrapper that invokes
324 # the main program. (XXX Discuss why.)
325 mainprogram = None
Just van Rossumceeb9622002-11-21 23:19:37 +0000326
Just van Rossumda302da2002-11-23 22:26:44 +0000327 # The main executable. If a Python main program is specified
328 # the executable will be copied to Resources and be invoked
329 # by the wrapper program mentioned above. Otherwise it will
330 # simply be used as the main executable.
331 executable = None
Just van Rossumceeb9622002-11-21 23:19:37 +0000332
Just van Rossumda302da2002-11-23 22:26:44 +0000333 # The name of the main nib, for Cocoa apps. *Must* be specified
334 # when building a Cocoa app.
335 nibname = None
Just van Rossumad33d722002-11-21 10:23:04 +0000336
Just van Rossum2aa09562003-02-01 08:34:46 +0000337 # The name of the icon file to be copied to Resources and used for
338 # the Finder icon.
339 iconfile = None
340
Just van Rossumda302da2002-11-23 22:26:44 +0000341 # Symlink the executable instead of copying it.
342 symlink_exec = 0
Just van Rossumad33d722002-11-21 10:23:04 +0000343
Just van Rossumcef32882002-11-26 00:34:52 +0000344 # If True, build standalone app.
345 standalone = 0
Jack Jansena03adde2003-02-18 23:29:46 +0000346
347 # If True, add a real main program that emulates sys.argv before calling
348 # mainprogram
349 argv_emulation = 0
Just van Rossumcef32882002-11-26 00:34:52 +0000350
351 # The following attributes are only used when building a standalone app.
352
353 # Exclude these modules.
354 excludeModules = []
355
356 # Include these modules.
357 includeModules = []
358
359 # Include these packages.
360 includePackages = []
361
362 # Strip binaries.
363 strip = 0
364
Just van Rossumcef32882002-11-26 00:34:52 +0000365 # Found Python modules: [(name, codeobject, ispkg), ...]
366 pymodules = []
367
368 # Modules that modulefinder couldn't find:
369 missingModules = []
Just van Rossum74bdca82002-11-28 11:30:56 +0000370 maybeMissingModules = []
Just van Rossumcef32882002-11-26 00:34:52 +0000371
372 # List of all binaries (executables or shared libs), for stripping purposes
373 binaries = []
374
Just van Rossumceeb9622002-11-21 23:19:37 +0000375 def setup(self):
Just van Rossumcef32882002-11-26 00:34:52 +0000376 if self.standalone and self.mainprogram is None:
377 raise BundleBuilderError, ("must specify 'mainprogram' when "
378 "building a standalone application.")
Just van Rossumceeb9622002-11-21 23:19:37 +0000379 if self.mainprogram is None and self.executable is None:
Just van Rossumcef32882002-11-26 00:34:52 +0000380 raise BundleBuilderError, ("must specify either or both of "
Just van Rossumceeb9622002-11-21 23:19:37 +0000381 "'executable' and 'mainprogram'")
382
383 if self.name is not None:
384 pass
385 elif self.mainprogram is not None:
386 self.name = os.path.splitext(os.path.basename(self.mainprogram))[0]
387 elif executable is not None:
388 self.name = os.path.splitext(os.path.basename(self.executable))[0]
389 if self.name[-4:] != ".app":
390 self.name += ".app"
Just van Rossumceeb9622002-11-21 23:19:37 +0000391
Just van Rossum74bdca82002-11-28 11:30:56 +0000392 if self.executable is None:
393 if not self.standalone:
394 self.symlink_exec = 1
395 self.executable = sys.executable
396
Just van Rossumceeb9622002-11-21 23:19:37 +0000397 if self.nibname:
398 self.plist.NSMainNibFile = self.nibname
399 if not hasattr(self.plist, "NSPrincipalClass"):
400 self.plist.NSPrincipalClass = "NSApplication"
401
402 BundleBuilder.setup(self)
403
Just van Rossum7fd69ad2002-11-22 00:08:47 +0000404 self.plist.CFBundleExecutable = self.name
Just van Rossumf7aba232002-11-22 00:31:50 +0000405
Just van Rossumcef32882002-11-26 00:34:52 +0000406 if self.standalone:
Just van Rossumcef32882002-11-26 00:34:52 +0000407 self.findDependencies()
408
Just van Rossumf7aba232002-11-22 00:31:50 +0000409 def preProcess(self):
Just van Rossumcef32882002-11-26 00:34:52 +0000410 resdir = "Contents/Resources"
Just van Rossumad33d722002-11-21 10:23:04 +0000411 if self.executable is not None:
412 if self.mainprogram is None:
Just van Rossum74bdca82002-11-28 11:30:56 +0000413 execname = self.name
Just van Rossumad33d722002-11-21 10:23:04 +0000414 else:
Just van Rossum74bdca82002-11-28 11:30:56 +0000415 execname = os.path.basename(self.executable)
416 execpath = pathjoin(self.execdir, execname)
Just van Rossum16aebf72002-11-22 11:43:10 +0000417 if not self.symlink_exec:
418 self.files.append((self.executable, execpath))
Just van Rossumcef32882002-11-26 00:34:52 +0000419 self.binaries.append(execpath)
Just van Rossumda302da2002-11-23 22:26:44 +0000420 self.execpath = execpath
Just van Rossumad33d722002-11-21 10:23:04 +0000421
422 if self.mainprogram is not None:
Just van Rossum24884f72002-11-29 21:22:33 +0000423 mainprogram = os.path.basename(self.mainprogram)
424 self.files.append((self.mainprogram, pathjoin(resdir, mainprogram)))
Jack Jansena03adde2003-02-18 23:29:46 +0000425 if self.argv_emulation:
426 # Change the main program, and create the helper main program (which
427 # does argv collection and then calls the real main).
428 # Also update the included modules (if we're creating a standalone
429 # program) and the plist
430 realmainprogram = mainprogram
431 mainprogram = '__argvemulator_' + mainprogram
432 resdirpath = pathjoin(self.bundlepath, resdir)
433 mainprogrampath = pathjoin(resdirpath, mainprogram)
434 makedirs(resdirpath)
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000435 open(mainprogrampath, "w").write(ARGV_EMULATOR % locals())
Jack Jansena03adde2003-02-18 23:29:46 +0000436 if self.standalone:
437 self.includeModules.append("argvemulator")
438 self.includeModules.append("os")
439 if not self.plist.has_key("CFBundleDocumentTypes"):
440 self.plist["CFBundleDocumentTypes"] = [
441 { "CFBundleTypeOSTypes" : [
442 "****",
443 "fold",
444 "disk"],
445 "CFBundleTypeRole": "Viewer"}]
Just van Rossum24884f72002-11-29 21:22:33 +0000446 # Write bootstrap script
Just van Rossum74bdca82002-11-28 11:30:56 +0000447 executable = os.path.basename(self.executable)
Just van Rossumad33d722002-11-21 10:23:04 +0000448 execdir = pathjoin(self.bundlepath, self.execdir)
Just van Rossum24884f72002-11-29 21:22:33 +0000449 bootstrappath = pathjoin(execdir, self.name)
Just van Rossumad33d722002-11-21 10:23:04 +0000450 makedirs(execdir)
Just van Rossum24884f72002-11-29 21:22:33 +0000451 open(bootstrappath, "w").write(BOOTSTRAP_SCRIPT % locals())
452 os.chmod(bootstrappath, 0775)
Just van Rossumad33d722002-11-21 10:23:04 +0000453
Just van Rossum2aa09562003-02-01 08:34:46 +0000454 if self.iconfile is not None:
455 iconbase = os.path.basename(self.iconfile)
456 self.plist.CFBundleIconFile = iconbase
457 self.files.append((self.iconfile, pathjoin(resdir, iconbase)))
458
Just van Rossum16aebf72002-11-22 11:43:10 +0000459 def postProcess(self):
Just van Rossum888e1002002-11-30 19:56:14 +0000460 if self.standalone:
461 self.addPythonModules()
Just van Rossum7322b1a2003-02-25 20:15:40 +0000462 else:
463 sitecustomizepath = pathjoin(self.bundlepath, "Contents", "Resources",
464 "sitecustomize" + PYC_EXT)
465 writePyc(SITECUSTOMIZE_CO, sitecustomizepath)
Just van Rossumcef32882002-11-26 00:34:52 +0000466 if self.strip and not self.symlink:
467 self.stripBinaries()
468
Just van Rossum16aebf72002-11-22 11:43:10 +0000469 if self.symlink_exec and self.executable:
470 self.message("Symlinking executable %s to %s" % (self.executable,
471 self.execpath), 2)
472 dst = pathjoin(self.bundlepath, self.execpath)
473 makedirs(os.path.dirname(dst))
474 os.symlink(os.path.abspath(self.executable), dst)
475
Just van Rossum74bdca82002-11-28 11:30:56 +0000476 if self.missingModules or self.maybeMissingModules:
Just van Rossumcef32882002-11-26 00:34:52 +0000477 self.reportMissing()
478
479 def addPythonModules(self):
480 self.message("Adding Python modules", 1)
Just van Rossumcef32882002-11-26 00:34:52 +0000481
Just van Rossum109ecbf2003-01-02 13:13:01 +0000482 if USE_ZIPIMPORT:
483 # Create a zip file containing all modules as pyc.
484 import zipfile
485 relpath = pathjoin("Contents", "Resources", ZIP_ARCHIVE)
Just van Rossumcef32882002-11-26 00:34:52 +0000486 abspath = pathjoin(self.bundlepath, relpath)
Just van Rossum109ecbf2003-01-02 13:13:01 +0000487 zf = zipfile.ZipFile(abspath, "w", zipfile.ZIP_DEFLATED)
488 for name, code, ispkg in self.pymodules:
489 self.message("Adding Python module %s" % name, 2)
490 path, pyc = getPycData(name, code, ispkg)
491 zf.writestr(path, pyc)
492 zf.close()
Just van Rossumcef32882002-11-26 00:34:52 +0000493 # add site.pyc
Just van Rossum535ffa22002-11-29 20:06:52 +0000494 sitepath = pathjoin(self.bundlepath, "Contents", "Resources",
495 "site" + PYC_EXT)
Just van Rossumcef32882002-11-26 00:34:52 +0000496 writePyc(SITE_CO, sitepath)
497 else:
498 # Create individual .pyc files.
Just van Rossum535ffa22002-11-29 20:06:52 +0000499 for name, code, ispkg in self.pymodules:
Just van Rossumcef32882002-11-26 00:34:52 +0000500 if ispkg:
501 name += ".__init__"
502 path = name.split(".")
Just van Rossum535ffa22002-11-29 20:06:52 +0000503 path = pathjoin("Contents", "Resources", *path) + PYC_EXT
Just van Rossumcef32882002-11-26 00:34:52 +0000504
505 if ispkg:
506 self.message("Adding Python package %s" % path, 2)
507 else:
508 self.message("Adding Python module %s" % path, 2)
509
510 abspath = pathjoin(self.bundlepath, path)
511 makedirs(os.path.dirname(abspath))
512 writePyc(code, abspath)
513
514 def stripBinaries(self):
515 if not os.path.exists(STRIP_EXEC):
516 self.message("Error: can't strip binaries: no strip program at "
517 "%s" % STRIP_EXEC, 0)
518 else:
519 self.message("Stripping binaries", 1)
520 for relpath in self.binaries:
521 self.message("Stripping %s" % relpath, 2)
522 abspath = pathjoin(self.bundlepath, relpath)
523 assert not os.path.islink(abspath)
524 rv = os.system("%s -S \"%s\"" % (STRIP_EXEC, abspath))
525
526 def findDependencies(self):
527 self.message("Finding module dependencies", 1)
528 import modulefinder
529 mf = modulefinder.ModuleFinder(excludes=self.excludeModules)
Just van Rossum109ecbf2003-01-02 13:13:01 +0000530 if USE_ZIPIMPORT:
531 # zipimport imports zlib, must add it manually
532 mf.import_hook("zlib")
Just van Rossumcef32882002-11-26 00:34:52 +0000533 # manually add our own site.py
534 site = mf.add_module("site")
535 site.__code__ = SITE_CO
536 mf.scan_code(SITE_CO, site)
537
Just van Rossum7322b1a2003-02-25 20:15:40 +0000538 # warnings.py gets imported implicitly from C
539 mf.import_hook("warnings")
540
Just van Rossumcef32882002-11-26 00:34:52 +0000541 includeModules = self.includeModules[:]
542 for name in self.includePackages:
543 includeModules.extend(findPackageContents(name).keys())
544 for name in includeModules:
545 try:
546 mf.import_hook(name)
547 except ImportError:
548 self.missingModules.append(name)
549
Just van Rossumcef32882002-11-26 00:34:52 +0000550 mf.run_script(self.mainprogram)
551 modules = mf.modules.items()
552 modules.sort()
553 for name, mod in modules:
554 if mod.__file__ and mod.__code__ is None:
555 # C extension
556 path = mod.__file__
Just van Rossum535ffa22002-11-29 20:06:52 +0000557 filename = os.path.basename(path)
Just van Rossum109ecbf2003-01-02 13:13:01 +0000558 if USE_ZIPIMPORT:
559 # Python modules are stored in a Zip archive, but put
560 # extensions in Contents/Resources/.a and add a tiny "loader"
561 # program in the Zip archive. Due to Thomas Heller.
Just van Rossum535ffa22002-11-29 20:06:52 +0000562 dstpath = pathjoin("Contents", "Resources", filename)
563 source = EXT_LOADER % {"name": name, "filename": filename}
564 code = compile(source, "<dynloader for %s>" % name, "exec")
565 mod.__code__ = code
Just van Rossumcef32882002-11-26 00:34:52 +0000566 else:
Just van Rossum535ffa22002-11-29 20:06:52 +0000567 # just copy the file
568 dstpath = name.split(".")[:-1] + [filename]
569 dstpath = pathjoin("Contents", "Resources", *dstpath)
Just van Rossumcef32882002-11-26 00:34:52 +0000570 self.files.append((path, dstpath))
Just van Rossumcef32882002-11-26 00:34:52 +0000571 self.binaries.append(dstpath)
Just van Rossum535ffa22002-11-29 20:06:52 +0000572 if mod.__code__ is not None:
Just van Rossumcef32882002-11-26 00:34:52 +0000573 ispkg = mod.__path__ is not None
Just van Rossum109ecbf2003-01-02 13:13:01 +0000574 if not USE_ZIPIMPORT or name != "site":
Just van Rossumcef32882002-11-26 00:34:52 +0000575 # Our site.py is doing the bootstrapping, so we must
Just van Rossum109ecbf2003-01-02 13:13:01 +0000576 # include a real .pyc file if USE_ZIPIMPORT is True.
Just van Rossumcef32882002-11-26 00:34:52 +0000577 self.pymodules.append((name, mod.__code__, ispkg))
578
Just van Rossum74bdca82002-11-28 11:30:56 +0000579 if hasattr(mf, "any_missing_maybe"):
580 missing, maybe = mf.any_missing_maybe()
581 else:
582 missing = mf.any_missing()
583 maybe = []
584 self.missingModules.extend(missing)
585 self.maybeMissingModules.extend(maybe)
Just van Rossumcef32882002-11-26 00:34:52 +0000586
587 def reportMissing(self):
588 missing = [name for name in self.missingModules
589 if name not in MAYMISS_MODULES]
Just van Rossum74bdca82002-11-28 11:30:56 +0000590 if self.maybeMissingModules:
591 maybe = self.maybeMissingModules
592 else:
593 maybe = [name for name in missing if "." in name]
594 missing = [name for name in missing if "." not in name]
Just van Rossumcef32882002-11-26 00:34:52 +0000595 missing.sort()
Just van Rossum74bdca82002-11-28 11:30:56 +0000596 maybe.sort()
597 if maybe:
598 self.message("Warning: couldn't find the following submodules:", 1)
599 self.message(" (Note that these could be false alarms -- "
600 "it's not always", 1)
Just van Rossumad692cc2002-11-28 18:56:50 +0000601 self.message(" possible to distinguish between \"from package "
602 "import submodule\" ", 1)
Just van Rossum74bdca82002-11-28 11:30:56 +0000603 self.message(" and \"from package import name\")", 1)
604 for name in maybe:
605 self.message(" ? " + name, 1)
Just van Rossumcef32882002-11-26 00:34:52 +0000606 if missing:
607 self.message("Warning: couldn't find the following modules:", 1)
Just van Rossum74bdca82002-11-28 11:30:56 +0000608 for name in missing:
609 self.message(" ? " + name, 1)
610
611 def report(self):
612 # XXX something decent
613 import pprint
614 pprint.pprint(self.__dict__)
615 if self.standalone:
616 self.reportMissing()
Just van Rossumcef32882002-11-26 00:34:52 +0000617
618#
619# Utilities.
620#
621
622SUFFIXES = [_suf for _suf, _mode, _tp in imp.get_suffixes()]
623identifierRE = re.compile(r"[_a-zA-z][_a-zA-Z0-9]*$")
624
625def findPackageContents(name, searchpath=None):
626 head = name.split(".")[-1]
627 if identifierRE.match(head) is None:
628 return {}
629 try:
630 fp, path, (ext, mode, tp) = imp.find_module(head, searchpath)
631 except ImportError:
632 return {}
633 modules = {name: None}
634 if tp == imp.PKG_DIRECTORY and path:
635 files = os.listdir(path)
636 for sub in files:
637 sub, ext = os.path.splitext(sub)
638 fullname = name + "." + sub
639 if sub != "__init__" and fullname not in modules:
640 modules.update(findPackageContents(fullname, [path]))
641 return modules
642
643def writePyc(code, path):
644 f = open(path, "wb")
Just van Rossumcef32882002-11-26 00:34:52 +0000645 f.write(MAGIC)
Just van Rossum109ecbf2003-01-02 13:13:01 +0000646 f.write("\0" * 4) # don't bother about a time stamp
647 marshal.dump(code, f)
Just van Rossumcef32882002-11-26 00:34:52 +0000648 f.close()
Just van Rossumad33d722002-11-21 10:23:04 +0000649
Just van Rossumad33d722002-11-21 10:23:04 +0000650def copy(src, dst, mkdirs=0):
651 """Copy a file or a directory."""
652 if mkdirs:
653 makedirs(os.path.dirname(dst))
654 if os.path.isdir(src):
655 shutil.copytree(src, dst)
656 else:
657 shutil.copy2(src, dst)
658
659def copytodir(src, dstdir):
660 """Copy a file or a directory to an existing directory."""
661 dst = pathjoin(dstdir, os.path.basename(src))
662 copy(src, dst)
663
664def makedirs(dir):
665 """Make all directories leading up to 'dir' including the leaf
666 directory. Don't moan if any path element already exists."""
667 try:
668 os.makedirs(dir)
669 except OSError, why:
670 if why.errno != errno.EEXIST:
671 raise
672
673def symlink(src, dst, mkdirs=0):
674 """Copy a file or a directory."""
Just van Rossum504377d2003-01-17 20:02:06 +0000675 if not os.path.exists(src):
676 raise IOError, "No such file or directory: '%s'" % src
Just van Rossumad33d722002-11-21 10:23:04 +0000677 if mkdirs:
678 makedirs(os.path.dirname(dst))
679 os.symlink(os.path.abspath(src), dst)
680
681def pathjoin(*args):
682 """Safe wrapper for os.path.join: asserts that all but the first
683 argument are relative paths."""
684 for seg in args[1:]:
685 assert seg[0] != "/"
686 return os.path.join(*args)
687
688
Just van Rossumceeb9622002-11-21 23:19:37 +0000689cmdline_doc = """\
690Usage:
Just van Rossumf7aba232002-11-22 00:31:50 +0000691 python bundlebuilder.py [options] command
Just van Rossumceeb9622002-11-21 23:19:37 +0000692 python mybuildscript.py [options] command
693
694Commands:
695 build build the application
696 report print a report
697
698Options:
699 -b, --builddir=DIR the build directory; defaults to "build"
700 -n, --name=NAME application name
701 -r, --resource=FILE extra file or folder to be copied to Resources
Just van Rossum7215e082003-02-25 21:00:55 +0000702 -f, --file=SRC:DST extra file or folder to be copied into the bundle;
703 DST must be a path relative to the bundle root
Just van Rossumceeb9622002-11-21 23:19:37 +0000704 -e, --executable=FILE the executable to be used
705 -m, --mainprogram=FILE the Python main program
Jack Jansena03adde2003-02-18 23:29:46 +0000706 -a, --argv add a wrapper main program to create sys.argv
Just van Rossumceeb9622002-11-21 23:19:37 +0000707 -p, --plist=FILE .plist file (default: generate one)
708 --nib=NAME main nib name
709 -c, --creator=CCCC 4-char creator code (default: '????')
Just van Rossum9af69682003-02-02 18:56:37 +0000710 --iconfile=FILE filename of the icon (an .icns file) to be used
Just van Rossum2aa09562003-02-01 08:34:46 +0000711 as the Finder icon
Just van Rossumceeb9622002-11-21 23:19:37 +0000712 -l, --link symlink files/folder instead of copying them
Just van Rossum16aebf72002-11-22 11:43:10 +0000713 --link-exec symlink the executable instead of copying it
Just van Rossumcef32882002-11-26 00:34:52 +0000714 --standalone build a standalone application, which is fully
715 independent of a Python installation
716 -x, --exclude=MODULE exclude module (with --standalone)
717 -i, --include=MODULE include module (with --standalone)
718 --package=PACKAGE include a whole package (with --standalone)
719 --strip strip binaries (remove debug info)
Just van Rossumceeb9622002-11-21 23:19:37 +0000720 -v, --verbose increase verbosity level
721 -q, --quiet decrease verbosity level
722 -h, --help print this message
723"""
724
725def usage(msg=None):
726 if msg:
727 print msg
728 print cmdline_doc
729 sys.exit(1)
730
731def main(builder=None):
732 if builder is None:
733 builder = AppBuilder(verbosity=1)
734
Jack Jansen00cbf072003-02-24 16:27:08 +0000735 shortopts = "b:n:r:f:e:m:c:p:lx:i:hvqa"
Just van Rossum7215e082003-02-25 21:00:55 +0000736 longopts = ("builddir=", "name=", "resource=", "file=", "executable=",
Just van Rossum16aebf72002-11-22 11:43:10 +0000737 "mainprogram=", "creator=", "nib=", "plist=", "link",
Jack Jansena03adde2003-02-18 23:29:46 +0000738 "link-exec", "help", "verbose", "quiet", "argv", "standalone",
Just van Rossum2aa09562003-02-01 08:34:46 +0000739 "exclude=", "include=", "package=", "strip", "iconfile=")
Just van Rossumceeb9622002-11-21 23:19:37 +0000740
741 try:
742 options, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
743 except getopt.error:
744 usage()
745
746 for opt, arg in options:
747 if opt in ('-b', '--builddir'):
748 builder.builddir = arg
749 elif opt in ('-n', '--name'):
750 builder.name = arg
751 elif opt in ('-r', '--resource'):
752 builder.resources.append(arg)
Just van Rossum7215e082003-02-25 21:00:55 +0000753 elif opt in ('-f', '--file'):
Jack Jansen00cbf072003-02-24 16:27:08 +0000754 srcdst = arg.split(':')
755 if len(srcdst) != 2:
Just van Rossum49833312003-02-25 21:08:12 +0000756 usage("-f or --file argument must be two paths, "
757 "separated by a colon")
Jack Jansen00cbf072003-02-24 16:27:08 +0000758 builder.files.append(srcdst)
Just van Rossumceeb9622002-11-21 23:19:37 +0000759 elif opt in ('-e', '--executable'):
760 builder.executable = arg
761 elif opt in ('-m', '--mainprogram'):
762 builder.mainprogram = arg
Jack Jansena03adde2003-02-18 23:29:46 +0000763 elif opt in ('-a', '--argv'):
764 builder.argv_emulation = 1
Just van Rossumceeb9622002-11-21 23:19:37 +0000765 elif opt in ('-c', '--creator'):
766 builder.creator = arg
Just van Rossum2aa09562003-02-01 08:34:46 +0000767 elif opt == '--iconfile':
768 builder.iconfile = arg
Just van Rossumceeb9622002-11-21 23:19:37 +0000769 elif opt == "--nib":
770 builder.nibname = arg
771 elif opt in ('-p', '--plist'):
772 builder.plist = Plist.fromFile(arg)
773 elif opt in ('-l', '--link'):
774 builder.symlink = 1
Just van Rossum16aebf72002-11-22 11:43:10 +0000775 elif opt == '--link-exec':
776 builder.symlink_exec = 1
Just van Rossumceeb9622002-11-21 23:19:37 +0000777 elif opt in ('-h', '--help'):
778 usage()
779 elif opt in ('-v', '--verbose'):
780 builder.verbosity += 1
781 elif opt in ('-q', '--quiet'):
782 builder.verbosity -= 1
Just van Rossumcef32882002-11-26 00:34:52 +0000783 elif opt == '--standalone':
784 builder.standalone = 1
785 elif opt in ('-x', '--exclude'):
786 builder.excludeModules.append(arg)
787 elif opt in ('-i', '--include'):
788 builder.includeModules.append(arg)
789 elif opt == '--package':
790 builder.includePackages.append(arg)
791 elif opt == '--strip':
792 builder.strip = 1
Just van Rossumceeb9622002-11-21 23:19:37 +0000793
794 if len(args) != 1:
795 usage("Must specify one command ('build', 'report' or 'help')")
796 command = args[0]
797
798 if command == "build":
799 builder.setup()
800 builder.build()
801 elif command == "report":
802 builder.setup()
803 builder.report()
804 elif command == "help":
805 usage()
806 else:
807 usage("Unknown command '%s'" % command)
808
809
Just van Rossumad33d722002-11-21 10:23:04 +0000810def buildapp(**kwargs):
Just van Rossumad33d722002-11-21 10:23:04 +0000811 builder = AppBuilder(**kwargs)
Just van Rossumceeb9622002-11-21 23:19:37 +0000812 main(builder)
Just van Rossumad33d722002-11-21 10:23:04 +0000813
814
815if __name__ == "__main__":
Just van Rossumceeb9622002-11-21 23:19:37 +0000816 main()