Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """\ |
| 4 | bundlebuilder.py -- Tools to assemble MacOS X (application) bundles. |
| 5 | |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 6 | This module contains two classes to build so called "bundles" for |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 7 | MacOS X. BundleBuilder is a general tool, AppBuilder is a subclass |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 8 | specialized in building application bundles. |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 9 | |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 10 | [Bundle|App]Builder objects are instantiated with a bunch of keyword |
| 11 | arguments, and have a build() method that will do all the work. See |
| 12 | the class doc strings for a description of the constructor arguments. |
| 13 | |
| 14 | The 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 | |
| 19 | Where "buildapp.py" is a user-supplied setup.py-like script following |
| 20 | this model: |
| 21 | |
| 22 | from bundlebuilder import buildapp |
| 23 | buildapp(<lots-of-keyword-args>) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 24 | |
| 25 | """ |
| 26 | |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 27 | |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 28 | __all__ = ["BundleBuilder", "BundleBuilderError", "AppBuilder", "buildapp"] |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | import sys |
| 32 | import os, errno, shutil |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 33 | import imp, marshal |
| 34 | import re |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 35 | from copy import deepcopy |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 36 | import getopt |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 37 | from plistlib import Plist |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 38 | from types import FunctionType as function |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 39 | |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 40 | class BundleBuilderError(Exception): pass |
| 41 | |
| 42 | |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 43 | class Defaults: |
| 44 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 45 | """Class attributes that don't start with an underscore and are |
| 46 | not functions or classmethods are (deep)copied to self.__dict__. |
| 47 | This allows for mutable default values. |
| 48 | """ |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 49 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 50 | def __init__(self, **kwargs): |
| 51 | defaults = self._getDefaults() |
| 52 | defaults.update(kwargs) |
| 53 | self.__dict__.update(defaults) |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 54 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 55 | def _getDefaults(cls): |
| 56 | defaults = {} |
| 57 | for name, value in cls.__dict__.items(): |
| 58 | if name[0] != "_" and not isinstance(value, |
| 59 | (function, classmethod)): |
| 60 | defaults[name] = deepcopy(value) |
| 61 | for base in cls.__bases__: |
| 62 | if hasattr(base, "_getDefaults"): |
| 63 | defaults.update(base._getDefaults()) |
| 64 | return defaults |
| 65 | _getDefaults = classmethod(_getDefaults) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 66 | |
| 67 | |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 68 | class BundleBuilder(Defaults): |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 69 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 70 | """BundleBuilder is a barebones class for assembling bundles. It |
| 71 | knows nothing about executables or icons, it only copies files |
| 72 | and creates the PkgInfo and Info.plist files. |
| 73 | """ |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 75 | # (Note that Defaults.__init__ (deep)copies these values to |
| 76 | # instance variables. Mutable defaults are therefore safe.) |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 77 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 78 | # Name of the bundle, with or without extension. |
| 79 | name = None |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 80 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 81 | # The property list ("plist") |
| 82 | plist = Plist(CFBundleDevelopmentRegion = "English", |
| 83 | CFBundleInfoDictionaryVersion = "6.0") |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 84 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 85 | # The type of the bundle. |
| 86 | type = "BNDL" |
| 87 | # The creator code of the bundle. |
| 88 | creator = None |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 89 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 90 | # List of files that have to be copied to <bundle>/Contents/Resources. |
| 91 | resources = [] |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 92 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 93 | # List of (src, dest) tuples; dest should be a path relative to the bundle |
| 94 | # (eg. "Contents/Resources/MyStuff/SomeFile.ext). |
| 95 | files = [] |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 96 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 97 | # List of shared libraries (dylibs, Frameworks) to bundle with the app |
| 98 | # will be placed in Contents/Frameworks |
| 99 | libs = [] |
Just van Rossum | 15624d8 | 2003-03-21 09:26:59 +0000 | [diff] [blame] | 100 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 101 | # Directory where the bundle will be assembled. |
| 102 | builddir = "build" |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 103 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 104 | # Make symlinks instead copying files. This is handy during debugging, but |
| 105 | # makes the bundle non-distributable. |
| 106 | symlink = 0 |
Just van Rossum | da302da | 2002-11-23 22:26:44 +0000 | [diff] [blame] | 107 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 108 | # Verbosity level. |
| 109 | verbosity = 1 |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 110 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 111 | def setup(self): |
| 112 | # XXX rethink self.name munging, this is brittle. |
| 113 | self.name, ext = os.path.splitext(self.name) |
| 114 | if not ext: |
| 115 | ext = ".bundle" |
| 116 | bundleextension = ext |
| 117 | # misc (derived) attributes |
| 118 | self.bundlepath = pathjoin(self.builddir, self.name + bundleextension) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 119 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 120 | plist = self.plist |
| 121 | plist.CFBundleName = self.name |
| 122 | plist.CFBundlePackageType = self.type |
| 123 | if self.creator is None: |
| 124 | if hasattr(plist, "CFBundleSignature"): |
| 125 | self.creator = plist.CFBundleSignature |
| 126 | else: |
| 127 | self.creator = "????" |
| 128 | plist.CFBundleSignature = self.creator |
| 129 | if not hasattr(plist, "CFBundleIdentifier"): |
| 130 | plist.CFBundleIdentifier = self.name |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 131 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 132 | def build(self): |
| 133 | """Build the bundle.""" |
| 134 | builddir = self.builddir |
| 135 | if builddir and not os.path.exists(builddir): |
| 136 | os.mkdir(builddir) |
| 137 | self.message("Building %s" % repr(self.bundlepath), 1) |
| 138 | if os.path.exists(self.bundlepath): |
| 139 | shutil.rmtree(self.bundlepath) |
| 140 | os.mkdir(self.bundlepath) |
| 141 | self.preProcess() |
| 142 | self._copyFiles() |
| 143 | self._addMetaFiles() |
| 144 | self.postProcess() |
| 145 | self.message("Done.", 1) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 146 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 147 | def preProcess(self): |
| 148 | """Hook for subclasses.""" |
| 149 | pass |
| 150 | def postProcess(self): |
| 151 | """Hook for subclasses.""" |
| 152 | pass |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 153 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 154 | def _addMetaFiles(self): |
| 155 | contents = pathjoin(self.bundlepath, "Contents") |
| 156 | makedirs(contents) |
| 157 | # |
| 158 | # Write Contents/PkgInfo |
| 159 | assert len(self.type) == len(self.creator) == 4, \ |
| 160 | "type and creator must be 4-byte strings." |
| 161 | pkginfo = pathjoin(contents, "PkgInfo") |
| 162 | f = open(pkginfo, "wb") |
| 163 | f.write(self.type + self.creator) |
| 164 | f.close() |
| 165 | # |
| 166 | # Write Contents/Info.plist |
| 167 | infoplist = pathjoin(contents, "Info.plist") |
| 168 | self.plist.write(infoplist) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 169 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 170 | def _copyFiles(self): |
| 171 | files = self.files[:] |
| 172 | for path in self.resources: |
| 173 | files.append((path, pathjoin("Contents", "Resources", |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 174 | os.path.basename(path)))) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 175 | for path in self.libs: |
| 176 | files.append((path, pathjoin("Contents", "Frameworks", |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 177 | os.path.basename(path)))) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 178 | if self.symlink: |
| 179 | self.message("Making symbolic links", 1) |
| 180 | msg = "Making symlink from" |
| 181 | else: |
| 182 | self.message("Copying files", 1) |
| 183 | msg = "Copying" |
| 184 | files.sort() |
| 185 | for src, dst in files: |
| 186 | if os.path.isdir(src): |
| 187 | self.message("%s %s/ to %s/" % (msg, src, dst), 2) |
| 188 | else: |
| 189 | self.message("%s %s to %s" % (msg, src, dst), 2) |
| 190 | dst = pathjoin(self.bundlepath, dst) |
| 191 | if self.symlink: |
| 192 | symlink(src, dst, mkdirs=1) |
| 193 | else: |
| 194 | copy(src, dst, mkdirs=1) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 195 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 196 | def message(self, msg, level=0): |
| 197 | if level <= self.verbosity: |
| 198 | indent = "" |
| 199 | if level > 1: |
| 200 | indent = (level - 1) * " " |
| 201 | sys.stderr.write(indent + msg + "\n") |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 202 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 203 | def report(self): |
| 204 | # XXX something decent |
| 205 | pass |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 206 | |
| 207 | |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 208 | if __debug__: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 209 | PYC_EXT = ".pyc" |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 210 | else: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 211 | PYC_EXT = ".pyo" |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 212 | |
| 213 | MAGIC = imp.get_magic() |
Just van Rossum | 109ecbf | 2003-01-02 13:13:01 +0000 | [diff] [blame] | 214 | USE_ZIPIMPORT = "zipimport" in sys.builtin_module_names |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 215 | |
| 216 | # For standalone apps, we have our own minimal site.py. We don't need |
| 217 | # all the cruft of the real site.py. |
| 218 | SITE_PY = """\ |
| 219 | import sys |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 220 | if not %(semi_standalone)s: |
| 221 | del sys.path[1:] # sys.path[0] is Contents/Resources/ |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 222 | """ |
| 223 | |
Just van Rossum | 109ecbf | 2003-01-02 13:13:01 +0000 | [diff] [blame] | 224 | if USE_ZIPIMPORT: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 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 Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 232 | |
Just van Rossum | 8a3ed3f | 2003-02-25 20:53:12 +0000 | [diff] [blame] | 233 | # |
| 234 | # Extension modules can't be in the modules zip archive, so a placeholder |
| 235 | # is added instead, that loads the extension from a specified location. |
| 236 | # |
Just van Rossum | 535ffa2 | 2002-11-29 20:06:52 +0000 | [diff] [blame] | 237 | EXT_LOADER = """\ |
Just van Rossum | 109ecbf | 2003-01-02 13:13:01 +0000 | [diff] [blame] | 238 | def __load(): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 239 | import imp, sys, os |
| 240 | for p in sys.path: |
| 241 | path = os.path.join(p, "%(filename)s") |
| 242 | if os.path.exists(path): |
| 243 | break |
| 244 | else: |
| 245 | assert 0, "file not found: %(filename)s" |
| 246 | mod = imp.load_dynamic("%(name)s", path) |
Just van Rossum | 109ecbf | 2003-01-02 13:13:01 +0000 | [diff] [blame] | 247 | |
| 248 | __load() |
| 249 | del __load |
Just van Rossum | 535ffa2 | 2002-11-29 20:06:52 +0000 | [diff] [blame] | 250 | """ |
| 251 | |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 252 | MAYMISS_MODULES = ['mac', 'os2', 'nt', 'ntpath', 'dos', 'dospath', |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 253 | 'win32api', 'ce', '_winreg', 'nturl2path', 'sitecustomize', |
| 254 | 'org.python.core', 'riscos', 'riscosenviron', 'riscospath' |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 255 | ] |
| 256 | |
| 257 | STRIP_EXEC = "/usr/bin/strip" |
| 258 | |
Just van Rossum | 8a3ed3f | 2003-02-25 20:53:12 +0000 | [diff] [blame] | 259 | # |
| 260 | # We're using a stock interpreter to run the app, yet we need |
| 261 | # a way to pass the Python main program to the interpreter. The |
| 262 | # bootstrapping script fires up the interpreter with the right |
| 263 | # arguments. os.execve() is used as OSX doesn't like us to |
| 264 | # start a real new process. Also, the executable name must match |
| 265 | # the CFBundleExecutable value in the Info.plist, so we lie |
| 266 | # deliberately with argv[0]. The actual Python executable is |
| 267 | # passed in an environment variable so we can "repair" |
| 268 | # sys.executable later. |
| 269 | # |
Just van Rossum | 74bdca8 | 2002-11-28 11:30:56 +0000 | [diff] [blame] | 270 | BOOTSTRAP_SCRIPT = """\ |
Just van Rossum | 0ff7a4e | 2003-02-26 11:27:56 +0000 | [diff] [blame] | 271 | #!%(hashbang)s |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 272 | |
Just van Rossum | 7322b1a | 2003-02-25 20:15:40 +0000 | [diff] [blame] | 273 | import sys, os |
| 274 | execdir = os.path.dirname(sys.argv[0]) |
| 275 | executable = os.path.join(execdir, "%(executable)s") |
| 276 | resdir = os.path.join(os.path.dirname(execdir), "Resources") |
Just van Rossum | 15624d8 | 2003-03-21 09:26:59 +0000 | [diff] [blame] | 277 | libdir = os.path.join(os.path.dirname(execdir), "Frameworks") |
Just van Rossum | 7322b1a | 2003-02-25 20:15:40 +0000 | [diff] [blame] | 278 | mainprogram = os.path.join(resdir, "%(mainprogram)s") |
| 279 | |
| 280 | sys.argv.insert(1, mainprogram) |
| 281 | os.environ["PYTHONPATH"] = resdir |
Just van Rossum | 82ad32e | 2003-03-21 11:32:37 +0000 | [diff] [blame] | 282 | if %(standalone)s: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 283 | os.environ["PYTHONHOME"] = resdir |
Just van Rossum | 7322b1a | 2003-02-25 20:15:40 +0000 | [diff] [blame] | 284 | os.environ["PYTHONEXECUTABLE"] = executable |
Just van Rossum | 15624d8 | 2003-03-21 09:26:59 +0000 | [diff] [blame] | 285 | os.environ["DYLD_LIBRARY_PATH"] = libdir |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 286 | os.environ["DYLD_FRAMEWORK_PATH"] = libdir |
Just van Rossum | 7322b1a | 2003-02-25 20:15:40 +0000 | [diff] [blame] | 287 | os.execve(executable, sys.argv, os.environ) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 288 | """ |
| 289 | |
Just van Rossum | 8a3ed3f | 2003-02-25 20:53:12 +0000 | [diff] [blame] | 290 | |
| 291 | # |
| 292 | # Optional wrapper that converts "dropped files" into sys.argv values. |
| 293 | # |
| 294 | ARGV_EMULATOR = """\ |
Jack Jansen | a03adde | 2003-02-18 23:29:46 +0000 | [diff] [blame] | 295 | import argvemulator, os |
| 296 | |
| 297 | argvemulator.ArgvCollector().mainloop() |
| 298 | execfile(os.path.join(os.path.split(__file__)[0], "%(realmainprogram)s")) |
| 299 | """ |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 300 | |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 301 | # |
| 302 | # When building a standalone app with Python.framework, we need to copy |
| 303 | # a subset from Python.framework to the bundle. The following list |
| 304 | # specifies exactly what items we'll copy. |
| 305 | # |
| 306 | PYTHONFRAMEWORKGOODIES = [ |
| 307 | "Python", # the Python core library |
| 308 | "Resources/English.lproj", |
| 309 | "Resources/Info.plist", |
| 310 | "Resources/version.plist", |
| 311 | ] |
| 312 | |
Just van Rossum | 79b0ae1 | 2003-06-29 22:20:26 +0000 | [diff] [blame^] | 313 | def isFramework(): |
| 314 | return sys.exec_prefix.find("Python.framework") > 0 |
| 315 | |
Just van Rossum | 8a3ed3f | 2003-02-25 20:53:12 +0000 | [diff] [blame] | 316 | |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 317 | LIB = os.path.join(sys.prefix, "lib", "python" + sys.version[:3]) |
| 318 | SITE_PACKAGES = os.path.join(LIB, "site-packages") |
| 319 | |
| 320 | |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 321 | class AppBuilder(BundleBuilder): |
| 322 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 323 | # Override type of the bundle. |
| 324 | type = "APPL" |
Jack Jansen | cc81b80 | 2003-03-05 14:42:18 +0000 | [diff] [blame] | 325 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 326 | # platform, name of the subfolder of Contents that contains the executable. |
| 327 | platform = "MacOS" |
Jack Jansen | cc81b80 | 2003-03-05 14:42:18 +0000 | [diff] [blame] | 328 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 329 | # A Python main program. If this argument is given, the main |
| 330 | # executable in the bundle will be a small wrapper that invokes |
| 331 | # the main program. (XXX Discuss why.) |
| 332 | mainprogram = None |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 333 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 334 | # The main executable. If a Python main program is specified |
| 335 | # the executable will be copied to Resources and be invoked |
| 336 | # by the wrapper program mentioned above. Otherwise it will |
| 337 | # simply be used as the main executable. |
| 338 | executable = None |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 339 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 340 | # The name of the main nib, for Cocoa apps. *Must* be specified |
| 341 | # when building a Cocoa app. |
| 342 | nibname = None |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 343 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 344 | # The name of the icon file to be copied to Resources and used for |
| 345 | # the Finder icon. |
| 346 | iconfile = None |
Just van Rossum | 2aa0956 | 2003-02-01 08:34:46 +0000 | [diff] [blame] | 347 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 348 | # Symlink the executable instead of copying it. |
| 349 | symlink_exec = 0 |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 350 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 351 | # If True, build standalone app. |
| 352 | standalone = 0 |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 353 | |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 354 | # If True, build semi-standalone app (only includes third-party modules). |
| 355 | semi_standalone = 0 |
| 356 | |
Jack Jansen | 8ba0e80 | 2003-05-25 22:00:17 +0000 | [diff] [blame] | 357 | # If set, use this for #! lines in stead of sys.executable |
| 358 | python = None |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 359 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 360 | # If True, add a real main program that emulates sys.argv before calling |
| 361 | # mainprogram |
| 362 | argv_emulation = 0 |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 363 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 364 | # The following attributes are only used when building a standalone app. |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 365 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 366 | # Exclude these modules. |
| 367 | excludeModules = [] |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 368 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 369 | # Include these modules. |
| 370 | includeModules = [] |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 371 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 372 | # Include these packages. |
| 373 | includePackages = [] |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 374 | |
Just van Rossum | 00a0b97 | 2003-06-20 21:18:22 +0000 | [diff] [blame] | 375 | # Strip binaries from debug info. |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 376 | strip = 0 |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 377 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 378 | # Found Python modules: [(name, codeobject, ispkg), ...] |
| 379 | pymodules = [] |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 380 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 381 | # Modules that modulefinder couldn't find: |
| 382 | missingModules = [] |
| 383 | maybeMissingModules = [] |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 384 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 385 | def setup(self): |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 386 | if ((self.standalone or self.semi_standalone) |
| 387 | and self.mainprogram is None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 388 | raise BundleBuilderError, ("must specify 'mainprogram' when " |
| 389 | "building a standalone application.") |
| 390 | if self.mainprogram is None and self.executable is None: |
| 391 | raise BundleBuilderError, ("must specify either or both of " |
| 392 | "'executable' and 'mainprogram'") |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 393 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 394 | self.execdir = pathjoin("Contents", self.platform) |
Jack Jansen | cc81b80 | 2003-03-05 14:42:18 +0000 | [diff] [blame] | 395 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 396 | if self.name is not None: |
| 397 | pass |
| 398 | elif self.mainprogram is not None: |
| 399 | self.name = os.path.splitext(os.path.basename(self.mainprogram))[0] |
| 400 | elif executable is not None: |
| 401 | self.name = os.path.splitext(os.path.basename(self.executable))[0] |
| 402 | if self.name[-4:] != ".app": |
| 403 | self.name += ".app" |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 404 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 405 | if self.executable is None: |
Just van Rossum | 79b0ae1 | 2003-06-29 22:20:26 +0000 | [diff] [blame^] | 406 | if not self.standalone and not isFramework(): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 407 | self.symlink_exec = 1 |
| 408 | self.executable = sys.executable |
Just van Rossum | 74bdca8 | 2002-11-28 11:30:56 +0000 | [diff] [blame] | 409 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 410 | if self.nibname: |
| 411 | self.plist.NSMainNibFile = self.nibname |
| 412 | if not hasattr(self.plist, "NSPrincipalClass"): |
| 413 | self.plist.NSPrincipalClass = "NSApplication" |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 414 | |
Just van Rossum | 79b0ae1 | 2003-06-29 22:20:26 +0000 | [diff] [blame^] | 415 | if self.standalone and isFramework(): |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 416 | self.addPythonFramework() |
| 417 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 418 | BundleBuilder.setup(self) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 419 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 420 | self.plist.CFBundleExecutable = self.name |
Just van Rossum | f7aba23 | 2002-11-22 00:31:50 +0000 | [diff] [blame] | 421 | |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 422 | if self.standalone or self.semi_standalone: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 423 | self.findDependencies() |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 424 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 425 | def preProcess(self): |
| 426 | resdir = "Contents/Resources" |
| 427 | if self.executable is not None: |
| 428 | if self.mainprogram is None: |
| 429 | execname = self.name |
| 430 | else: |
| 431 | execname = os.path.basename(self.executable) |
| 432 | execpath = pathjoin(self.execdir, execname) |
| 433 | if not self.symlink_exec: |
| 434 | self.files.append((self.executable, execpath)) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 435 | self.execpath = execpath |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 436 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 437 | if self.mainprogram is not None: |
| 438 | mainprogram = os.path.basename(self.mainprogram) |
| 439 | self.files.append((self.mainprogram, pathjoin(resdir, mainprogram))) |
| 440 | if self.argv_emulation: |
| 441 | # Change the main program, and create the helper main program (which |
| 442 | # does argv collection and then calls the real main). |
| 443 | # Also update the included modules (if we're creating a standalone |
| 444 | # program) and the plist |
| 445 | realmainprogram = mainprogram |
| 446 | mainprogram = '__argvemulator_' + mainprogram |
| 447 | resdirpath = pathjoin(self.bundlepath, resdir) |
| 448 | mainprogrampath = pathjoin(resdirpath, mainprogram) |
| 449 | makedirs(resdirpath) |
| 450 | open(mainprogrampath, "w").write(ARGV_EMULATOR % locals()) |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 451 | if self.standalone or self.semi_standalone: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 452 | self.includeModules.append("argvemulator") |
| 453 | self.includeModules.append("os") |
| 454 | if not self.plist.has_key("CFBundleDocumentTypes"): |
| 455 | self.plist["CFBundleDocumentTypes"] = [ |
| 456 | { "CFBundleTypeOSTypes" : [ |
| 457 | "****", |
| 458 | "fold", |
| 459 | "disk"], |
| 460 | "CFBundleTypeRole": "Viewer"}] |
| 461 | # Write bootstrap script |
| 462 | executable = os.path.basename(self.executable) |
| 463 | execdir = pathjoin(self.bundlepath, self.execdir) |
| 464 | bootstrappath = pathjoin(execdir, self.name) |
| 465 | makedirs(execdir) |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 466 | if self.standalone or self.semi_standalone: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 467 | # XXX we're screwed when the end user has deleted |
| 468 | # /usr/bin/python |
| 469 | hashbang = "/usr/bin/python" |
Jack Jansen | 8ba0e80 | 2003-05-25 22:00:17 +0000 | [diff] [blame] | 470 | elif self.python: |
| 471 | hashbang = self.python |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 472 | else: |
| 473 | hashbang = os.path.realpath(sys.executable) |
| 474 | standalone = self.standalone |
| 475 | open(bootstrappath, "w").write(BOOTSTRAP_SCRIPT % locals()) |
| 476 | os.chmod(bootstrappath, 0775) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 477 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 478 | if self.iconfile is not None: |
| 479 | iconbase = os.path.basename(self.iconfile) |
| 480 | self.plist.CFBundleIconFile = iconbase |
| 481 | self.files.append((self.iconfile, pathjoin(resdir, iconbase))) |
Just van Rossum | 2aa0956 | 2003-02-01 08:34:46 +0000 | [diff] [blame] | 482 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 483 | def postProcess(self): |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 484 | if self.standalone or self.semi_standalone: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 485 | self.addPythonModules() |
| 486 | if self.strip and not self.symlink: |
| 487 | self.stripBinaries() |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 488 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 489 | if self.symlink_exec and self.executable: |
| 490 | self.message("Symlinking executable %s to %s" % (self.executable, |
| 491 | self.execpath), 2) |
| 492 | dst = pathjoin(self.bundlepath, self.execpath) |
| 493 | makedirs(os.path.dirname(dst)) |
| 494 | os.symlink(os.path.abspath(self.executable), dst) |
Just van Rossum | 16aebf7 | 2002-11-22 11:43:10 +0000 | [diff] [blame] | 495 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 496 | if self.missingModules or self.maybeMissingModules: |
| 497 | self.reportMissing() |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 498 | |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 499 | def addPythonFramework(self): |
| 500 | # If we're building a standalone app with Python.framework, |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 501 | # include a minimal subset of Python.framework, *unless* |
| 502 | # Python.framework was specified manually in self.libs. |
| 503 | for lib in self.libs: |
| 504 | if os.path.basename(lib) == "Python.framework": |
| 505 | # a Python.framework was specified as a library |
| 506 | return |
| 507 | |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 508 | frameworkpath = sys.exec_prefix[:sys.exec_prefix.find( |
| 509 | "Python.framework") + len("Python.framework")] |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 510 | |
Just van Rossum | 3166f59 | 2003-06-20 18:56:10 +0000 | [diff] [blame] | 511 | version = sys.version[:3] |
| 512 | frameworkpath = pathjoin(frameworkpath, "Versions", version) |
| 513 | destbase = pathjoin("Contents", "Frameworks", "Python.framework", |
| 514 | "Versions", version) |
| 515 | for item in PYTHONFRAMEWORKGOODIES: |
| 516 | src = pathjoin(frameworkpath, item) |
| 517 | dst = pathjoin(destbase, item) |
| 518 | self.files.append((src, dst)) |
| 519 | |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 520 | def _getSiteCode(self): |
| 521 | return compile(SITE_PY % {"semi_standalone": self.semi_standalone}, |
| 522 | "<-bundlebuilder.py->", "exec") |
| 523 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 524 | def addPythonModules(self): |
| 525 | self.message("Adding Python modules", 1) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 526 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 527 | if USE_ZIPIMPORT: |
| 528 | # Create a zip file containing all modules as pyc. |
| 529 | import zipfile |
| 530 | relpath = pathjoin("Contents", "Resources", ZIP_ARCHIVE) |
| 531 | abspath = pathjoin(self.bundlepath, relpath) |
| 532 | zf = zipfile.ZipFile(abspath, "w", zipfile.ZIP_DEFLATED) |
| 533 | for name, code, ispkg in self.pymodules: |
| 534 | self.message("Adding Python module %s" % name, 2) |
| 535 | path, pyc = getPycData(name, code, ispkg) |
| 536 | zf.writestr(path, pyc) |
| 537 | zf.close() |
| 538 | # add site.pyc |
| 539 | sitepath = pathjoin(self.bundlepath, "Contents", "Resources", |
| 540 | "site" + PYC_EXT) |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 541 | writePyc(self._getSiteCode(), sitepath) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 542 | else: |
| 543 | # Create individual .pyc files. |
| 544 | for name, code, ispkg in self.pymodules: |
| 545 | if ispkg: |
| 546 | name += ".__init__" |
| 547 | path = name.split(".") |
| 548 | path = pathjoin("Contents", "Resources", *path) + PYC_EXT |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 549 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 550 | if ispkg: |
| 551 | self.message("Adding Python package %s" % path, 2) |
| 552 | else: |
| 553 | self.message("Adding Python module %s" % path, 2) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 554 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 555 | abspath = pathjoin(self.bundlepath, path) |
| 556 | makedirs(os.path.dirname(abspath)) |
| 557 | writePyc(code, abspath) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 558 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 559 | def stripBinaries(self): |
| 560 | if not os.path.exists(STRIP_EXEC): |
| 561 | self.message("Error: can't strip binaries: no strip program at " |
| 562 | "%s" % STRIP_EXEC, 0) |
| 563 | else: |
Just van Rossum | 00a0b97 | 2003-06-20 21:18:22 +0000 | [diff] [blame] | 564 | import stat |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 565 | self.message("Stripping binaries", 1) |
Just van Rossum | 00a0b97 | 2003-06-20 21:18:22 +0000 | [diff] [blame] | 566 | def walk(top): |
| 567 | for name in os.listdir(top): |
| 568 | path = pathjoin(top, name) |
| 569 | if os.path.islink(path): |
| 570 | continue |
| 571 | if os.path.isdir(path): |
| 572 | walk(path) |
| 573 | else: |
| 574 | mod = os.stat(path)[stat.ST_MODE] |
| 575 | if not (mod & 0100): |
| 576 | continue |
| 577 | relpath = path[len(self.bundlepath):] |
| 578 | self.message("Stripping %s" % relpath, 2) |
| 579 | inf, outf = os.popen4("%s -S \"%s\"" % |
| 580 | (STRIP_EXEC, path)) |
| 581 | output = outf.read().strip() |
| 582 | if output: |
| 583 | # usually not a real problem, like when we're |
| 584 | # trying to strip a script |
| 585 | self.message("Problem stripping %s:" % relpath, 3) |
| 586 | self.message(output, 3) |
| 587 | walk(self.bundlepath) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 588 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 589 | def findDependencies(self): |
| 590 | self.message("Finding module dependencies", 1) |
| 591 | import modulefinder |
| 592 | mf = modulefinder.ModuleFinder(excludes=self.excludeModules) |
| 593 | if USE_ZIPIMPORT: |
| 594 | # zipimport imports zlib, must add it manually |
| 595 | mf.import_hook("zlib") |
| 596 | # manually add our own site.py |
| 597 | site = mf.add_module("site") |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 598 | site.__code__ = self._getSiteCode() |
| 599 | mf.scan_code(site.__code__, site) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 600 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 601 | # warnings.py gets imported implicitly from C |
| 602 | mf.import_hook("warnings") |
Just van Rossum | 7322b1a | 2003-02-25 20:15:40 +0000 | [diff] [blame] | 603 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 604 | includeModules = self.includeModules[:] |
| 605 | for name in self.includePackages: |
| 606 | includeModules.extend(findPackageContents(name).keys()) |
| 607 | for name in includeModules: |
| 608 | try: |
| 609 | mf.import_hook(name) |
| 610 | except ImportError: |
| 611 | self.missingModules.append(name) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 612 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 613 | mf.run_script(self.mainprogram) |
| 614 | modules = mf.modules.items() |
| 615 | modules.sort() |
| 616 | for name, mod in modules: |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 617 | path = mod.__file__ |
| 618 | if path and self.semi_standalone: |
| 619 | # skip the standard library |
| 620 | if path.startswith(LIB) and not path.startswith(SITE_PACKAGES): |
| 621 | continue |
| 622 | if path and mod.__code__ is None: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 623 | # C extension |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 624 | filename = os.path.basename(path) |
Just van Rossum | 79b0ae1 | 2003-06-29 22:20:26 +0000 | [diff] [blame^] | 625 | pathitems = name.split(".")[:-1] + [filename] |
| 626 | dstpath = pathjoin(*pathitems) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 627 | if USE_ZIPIMPORT: |
Just van Rossum | 79b0ae1 | 2003-06-29 22:20:26 +0000 | [diff] [blame^] | 628 | if name != "zlib": |
| 629 | # neatly pack all extension modules in a subdirectory, |
| 630 | # except zlib, since it's neccesary for bootstrapping. |
| 631 | dstpath = pathjoin("ExtensionModules", dstpath) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 632 | # Python modules are stored in a Zip archive, but put |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 633 | # extensions in Contents/Resources/. Add a tiny "loader" |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 634 | # program in the Zip archive. Due to Thomas Heller. |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 635 | source = EXT_LOADER % {"name": name, "filename": dstpath} |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 636 | code = compile(source, "<dynloader for %s>" % name, "exec") |
| 637 | mod.__code__ = code |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 638 | self.files.append((path, pathjoin("Contents", "Resources", dstpath))) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 639 | if mod.__code__ is not None: |
| 640 | ispkg = mod.__path__ is not None |
| 641 | if not USE_ZIPIMPORT or name != "site": |
| 642 | # Our site.py is doing the bootstrapping, so we must |
| 643 | # include a real .pyc file if USE_ZIPIMPORT is True. |
| 644 | self.pymodules.append((name, mod.__code__, ispkg)) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 645 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 646 | if hasattr(mf, "any_missing_maybe"): |
| 647 | missing, maybe = mf.any_missing_maybe() |
| 648 | else: |
| 649 | missing = mf.any_missing() |
| 650 | maybe = [] |
| 651 | self.missingModules.extend(missing) |
| 652 | self.maybeMissingModules.extend(maybe) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 653 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 654 | def reportMissing(self): |
| 655 | missing = [name for name in self.missingModules |
| 656 | if name not in MAYMISS_MODULES] |
| 657 | if self.maybeMissingModules: |
| 658 | maybe = self.maybeMissingModules |
| 659 | else: |
| 660 | maybe = [name for name in missing if "." in name] |
| 661 | missing = [name for name in missing if "." not in name] |
| 662 | missing.sort() |
| 663 | maybe.sort() |
| 664 | if maybe: |
| 665 | self.message("Warning: couldn't find the following submodules:", 1) |
| 666 | self.message(" (Note that these could be false alarms -- " |
| 667 | "it's not always", 1) |
| 668 | self.message(" possible to distinguish between \"from package " |
| 669 | "import submodule\" ", 1) |
| 670 | self.message(" and \"from package import name\")", 1) |
| 671 | for name in maybe: |
| 672 | self.message(" ? " + name, 1) |
| 673 | if missing: |
| 674 | self.message("Warning: couldn't find the following modules:", 1) |
| 675 | for name in missing: |
| 676 | self.message(" ? " + name, 1) |
Just van Rossum | 74bdca8 | 2002-11-28 11:30:56 +0000 | [diff] [blame] | 677 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 678 | def report(self): |
| 679 | # XXX something decent |
| 680 | import pprint |
| 681 | pprint.pprint(self.__dict__) |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 682 | if self.standalone or self.semi_standalone: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 683 | self.reportMissing() |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 684 | |
| 685 | # |
| 686 | # Utilities. |
| 687 | # |
| 688 | |
| 689 | SUFFIXES = [_suf for _suf, _mode, _tp in imp.get_suffixes()] |
| 690 | identifierRE = re.compile(r"[_a-zA-z][_a-zA-Z0-9]*$") |
| 691 | |
| 692 | def findPackageContents(name, searchpath=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 693 | head = name.split(".")[-1] |
| 694 | if identifierRE.match(head) is None: |
| 695 | return {} |
| 696 | try: |
| 697 | fp, path, (ext, mode, tp) = imp.find_module(head, searchpath) |
| 698 | except ImportError: |
| 699 | return {} |
| 700 | modules = {name: None} |
| 701 | if tp == imp.PKG_DIRECTORY and path: |
| 702 | files = os.listdir(path) |
| 703 | for sub in files: |
| 704 | sub, ext = os.path.splitext(sub) |
| 705 | fullname = name + "." + sub |
| 706 | if sub != "__init__" and fullname not in modules: |
| 707 | modules.update(findPackageContents(fullname, [path])) |
| 708 | return modules |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 709 | |
| 710 | def writePyc(code, path): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 711 | f = open(path, "wb") |
| 712 | f.write(MAGIC) |
| 713 | f.write("\0" * 4) # don't bother about a time stamp |
| 714 | marshal.dump(code, f) |
| 715 | f.close() |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 716 | |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 717 | def copy(src, dst, mkdirs=0): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 718 | """Copy a file or a directory.""" |
| 719 | if mkdirs: |
| 720 | makedirs(os.path.dirname(dst)) |
| 721 | if os.path.isdir(src): |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 722 | shutil.copytree(src, dst, symlinks=1) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 723 | else: |
| 724 | shutil.copy2(src, dst) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 725 | |
| 726 | def copytodir(src, dstdir): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 727 | """Copy a file or a directory to an existing directory.""" |
| 728 | dst = pathjoin(dstdir, os.path.basename(src)) |
| 729 | copy(src, dst) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 730 | |
| 731 | def makedirs(dir): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 732 | """Make all directories leading up to 'dir' including the leaf |
| 733 | directory. Don't moan if any path element already exists.""" |
| 734 | try: |
| 735 | os.makedirs(dir) |
| 736 | except OSError, why: |
| 737 | if why.errno != errno.EEXIST: |
| 738 | raise |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 739 | |
| 740 | def symlink(src, dst, mkdirs=0): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 741 | """Copy a file or a directory.""" |
| 742 | if not os.path.exists(src): |
| 743 | raise IOError, "No such file or directory: '%s'" % src |
| 744 | if mkdirs: |
| 745 | makedirs(os.path.dirname(dst)) |
| 746 | os.symlink(os.path.abspath(src), dst) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 747 | |
| 748 | def pathjoin(*args): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 749 | """Safe wrapper for os.path.join: asserts that all but the first |
| 750 | argument are relative paths.""" |
| 751 | for seg in args[1:]: |
| 752 | assert seg[0] != "/" |
| 753 | return os.path.join(*args) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 754 | |
| 755 | |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 756 | cmdline_doc = """\ |
| 757 | Usage: |
Just van Rossum | f7aba23 | 2002-11-22 00:31:50 +0000 | [diff] [blame] | 758 | python bundlebuilder.py [options] command |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 759 | python mybuildscript.py [options] command |
| 760 | |
| 761 | Commands: |
| 762 | build build the application |
| 763 | report print a report |
| 764 | |
| 765 | Options: |
| 766 | -b, --builddir=DIR the build directory; defaults to "build" |
| 767 | -n, --name=NAME application name |
| 768 | -r, --resource=FILE extra file or folder to be copied to Resources |
Just van Rossum | 7215e08 | 2003-02-25 21:00:55 +0000 | [diff] [blame] | 769 | -f, --file=SRC:DST extra file or folder to be copied into the bundle; |
| 770 | DST must be a path relative to the bundle root |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 771 | -e, --executable=FILE the executable to be used |
| 772 | -m, --mainprogram=FILE the Python main program |
Jack Jansen | a03adde | 2003-02-18 23:29:46 +0000 | [diff] [blame] | 773 | -a, --argv add a wrapper main program to create sys.argv |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 774 | -p, --plist=FILE .plist file (default: generate one) |
| 775 | --nib=NAME main nib name |
| 776 | -c, --creator=CCCC 4-char creator code (default: '????') |
Just van Rossum | 9af6968 | 2003-02-02 18:56:37 +0000 | [diff] [blame] | 777 | --iconfile=FILE filename of the icon (an .icns file) to be used |
Just van Rossum | 2aa0956 | 2003-02-01 08:34:46 +0000 | [diff] [blame] | 778 | as the Finder icon |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 779 | -l, --link symlink files/folder instead of copying them |
Just van Rossum | 16aebf7 | 2002-11-22 11:43:10 +0000 | [diff] [blame] | 780 | --link-exec symlink the executable instead of copying it |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 781 | --standalone build a standalone application, which is fully |
| 782 | independent of a Python installation |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 783 | --semi-standalone build a standalone application, which depends on |
| 784 | an installed Python, yet includes all third-party |
| 785 | modules. |
Jack Jansen | 8ba0e80 | 2003-05-25 22:00:17 +0000 | [diff] [blame] | 786 | --python=FILE Python to use in #! line in stead of current Python |
Just van Rossum | 15624d8 | 2003-03-21 09:26:59 +0000 | [diff] [blame] | 787 | --lib=FILE shared library or framework to be copied into |
| 788 | the bundle |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 789 | -x, --exclude=MODULE exclude module (with --(semi-)standalone) |
| 790 | -i, --include=MODULE include module (with --(semi-)standalone) |
| 791 | --package=PACKAGE include a whole package (with --(semi-)standalone) |
Just van Rossum | cef3288 | 2002-11-26 00:34:52 +0000 | [diff] [blame] | 792 | --strip strip binaries (remove debug info) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 793 | -v, --verbose increase verbosity level |
| 794 | -q, --quiet decrease verbosity level |
| 795 | -h, --help print this message |
| 796 | """ |
| 797 | |
| 798 | def usage(msg=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 799 | if msg: |
| 800 | print msg |
| 801 | print cmdline_doc |
| 802 | sys.exit(1) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 803 | |
| 804 | def main(builder=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 805 | if builder is None: |
| 806 | builder = AppBuilder(verbosity=1) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 807 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 808 | shortopts = "b:n:r:f:e:m:c:p:lx:i:hvqa" |
| 809 | longopts = ("builddir=", "name=", "resource=", "file=", "executable=", |
| 810 | "mainprogram=", "creator=", "nib=", "plist=", "link", |
| 811 | "link-exec", "help", "verbose", "quiet", "argv", "standalone", |
| 812 | "exclude=", "include=", "package=", "strip", "iconfile=", |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 813 | "lib=", "python=", "semi-standalone") |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 814 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 815 | try: |
| 816 | options, args = getopt.getopt(sys.argv[1:], shortopts, longopts) |
| 817 | except getopt.error: |
| 818 | usage() |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 819 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 820 | for opt, arg in options: |
| 821 | if opt in ('-b', '--builddir'): |
| 822 | builder.builddir = arg |
| 823 | elif opt in ('-n', '--name'): |
| 824 | builder.name = arg |
| 825 | elif opt in ('-r', '--resource'): |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 826 | builder.resources.append(os.path.normpath(arg)) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 827 | elif opt in ('-f', '--file'): |
| 828 | srcdst = arg.split(':') |
| 829 | if len(srcdst) != 2: |
| 830 | usage("-f or --file argument must be two paths, " |
| 831 | "separated by a colon") |
| 832 | builder.files.append(srcdst) |
| 833 | elif opt in ('-e', '--executable'): |
| 834 | builder.executable = arg |
| 835 | elif opt in ('-m', '--mainprogram'): |
| 836 | builder.mainprogram = arg |
| 837 | elif opt in ('-a', '--argv'): |
| 838 | builder.argv_emulation = 1 |
| 839 | elif opt in ('-c', '--creator'): |
| 840 | builder.creator = arg |
| 841 | elif opt == '--iconfile': |
| 842 | builder.iconfile = arg |
| 843 | elif opt == "--lib": |
Just van Rossum | dc31dc0 | 2003-06-20 21:43:36 +0000 | [diff] [blame] | 844 | builder.libs.append(os.path.normpath(arg)) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 845 | elif opt == "--nib": |
| 846 | builder.nibname = arg |
| 847 | elif opt in ('-p', '--plist'): |
| 848 | builder.plist = Plist.fromFile(arg) |
| 849 | elif opt in ('-l', '--link'): |
| 850 | builder.symlink = 1 |
| 851 | elif opt == '--link-exec': |
| 852 | builder.symlink_exec = 1 |
| 853 | elif opt in ('-h', '--help'): |
| 854 | usage() |
| 855 | elif opt in ('-v', '--verbose'): |
| 856 | builder.verbosity += 1 |
| 857 | elif opt in ('-q', '--quiet'): |
| 858 | builder.verbosity -= 1 |
| 859 | elif opt == '--standalone': |
| 860 | builder.standalone = 1 |
Just van Rossum | 762d2cc | 2003-06-29 21:54:12 +0000 | [diff] [blame] | 861 | elif opt == '--semi-standalone': |
| 862 | builder.semi_standalone = 1 |
Jack Jansen | 8ba0e80 | 2003-05-25 22:00:17 +0000 | [diff] [blame] | 863 | elif opt == '--python': |
| 864 | builder.python = arg |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 865 | elif opt in ('-x', '--exclude'): |
| 866 | builder.excludeModules.append(arg) |
| 867 | elif opt in ('-i', '--include'): |
| 868 | builder.includeModules.append(arg) |
| 869 | elif opt == '--package': |
| 870 | builder.includePackages.append(arg) |
| 871 | elif opt == '--strip': |
| 872 | builder.strip = 1 |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 873 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 874 | if len(args) != 1: |
| 875 | usage("Must specify one command ('build', 'report' or 'help')") |
| 876 | command = args[0] |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 877 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 878 | if command == "build": |
| 879 | builder.setup() |
| 880 | builder.build() |
| 881 | elif command == "report": |
| 882 | builder.setup() |
| 883 | builder.report() |
| 884 | elif command == "help": |
| 885 | usage() |
| 886 | else: |
| 887 | usage("Unknown command '%s'" % command) |
Just van Rossum | ceeb962 | 2002-11-21 23:19:37 +0000 | [diff] [blame] | 888 | |
| 889 | |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 890 | def buildapp(**kwargs): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 891 | builder = AppBuilder(**kwargs) |
| 892 | main(builder) |
Just van Rossum | ad33d72 | 2002-11-21 10:23:04 +0000 | [diff] [blame] | 893 | |
| 894 | |
| 895 | if __name__ == "__main__": |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 896 | main() |