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