blob: 8f5996951609dd5ac00159eb5e20d34441731fac [file] [log] [blame]
Just van Rossumad33d722002-11-21 10:23:04 +00001#! /usr/bin/env python
2
3"""\
4bundlebuilder.py -- Tools to assemble MacOS X (application) bundles.
5
Just van Rossumceeb9622002-11-21 23:19:37 +00006This module contains two classes to build so called "bundles" for
Just van Rossumad33d722002-11-21 10:23:04 +00007MacOS X. BundleBuilder is a general tool, AppBuilder is a subclass
Just van Rossumceeb9622002-11-21 23:19:37 +00008specialized in building application bundles.
Just van Rossumad33d722002-11-21 10:23:04 +00009
Just van Rossumceeb9622002-11-21 23:19:37 +000010[Bundle|App]Builder objects are instantiated with a bunch of keyword
11arguments, and have a build() method that will do all the work. See
12the class doc strings for a description of the constructor arguments.
13
14The module contains a main program that can be used in two ways:
15
16 % python bundlebuilder.py [options] build
17 % python buildapp.py [options] build
18
19Where "buildapp.py" is a user-supplied setup.py-like script following
20this model:
21
22 from bundlebuilder import buildapp
23 buildapp(<lots-of-keyword-args>)
Just van Rossumad33d722002-11-21 10:23:04 +000024
25"""
26
Just van Rossumad33d722002-11-21 10:23:04 +000027
Just van Rossumcef32882002-11-26 00:34:52 +000028__all__ = ["BundleBuilder", "BundleBuilderError", "AppBuilder", "buildapp"]
Just van Rossumad33d722002-11-21 10:23:04 +000029
30
Benjamin Peterson23681932008-05-12 21:42:13 +000031from warnings import warnpy3k
Benjamin Petersona6864e02008-07-14 17:42:17 +000032warnpy3k("In 3.x, the bundlebuilder module is removed.", stacklevel=2)
Benjamin Peterson23681932008-05-12 21:42:13 +000033
Just van Rossumad33d722002-11-21 10:23:04 +000034import sys
35import os, errno, shutil
Just van Rossumcef32882002-11-26 00:34:52 +000036import imp, marshal
37import re
Just van Rossumda302da2002-11-23 22:26:44 +000038from copy import deepcopy
Just van Rossumceeb9622002-11-21 23:19:37 +000039import getopt
Just van Rossumad33d722002-11-21 10:23:04 +000040from plistlib import Plist
Just van Rossumda302da2002-11-23 22:26:44 +000041from types import FunctionType as function
Just van Rossumad33d722002-11-21 10:23:04 +000042
Just van Rossumcef32882002-11-26 00:34:52 +000043class BundleBuilderError(Exception): pass
44
45
Just van Rossumda302da2002-11-23 22:26:44 +000046class Defaults:
47
Jack Jansen0ae32202003-04-09 13:25:43 +000048 """Class attributes that don't start with an underscore and are
49 not functions or classmethods are (deep)copied to self.__dict__.
50 This allows for mutable default values.
51 """
Just van Rossumda302da2002-11-23 22:26:44 +000052
Jack Jansen0ae32202003-04-09 13:25:43 +000053 def __init__(self, **kwargs):
54 defaults = self._getDefaults()
55 defaults.update(kwargs)
56 self.__dict__.update(defaults)
Just van Rossumda302da2002-11-23 22:26:44 +000057
Jack Jansen0ae32202003-04-09 13:25:43 +000058 def _getDefaults(cls):
59 defaults = {}
Just van Rossumed8bfce2003-07-10 14:53:27 +000060 for base in cls.__bases__:
61 if hasattr(base, "_getDefaults"):
62 defaults.update(base._getDefaults())
Jack Jansen0ae32202003-04-09 13:25:43 +000063 for name, value in cls.__dict__.items():
64 if name[0] != "_" and not isinstance(value,
65 (function, classmethod)):
66 defaults[name] = deepcopy(value)
Jack Jansen0ae32202003-04-09 13:25:43 +000067 return defaults
68 _getDefaults = classmethod(_getDefaults)
Just van Rossumad33d722002-11-21 10:23:04 +000069
70
Just van Rossumda302da2002-11-23 22:26:44 +000071class BundleBuilder(Defaults):
Just van Rossumad33d722002-11-21 10:23:04 +000072
Jack Jansen0ae32202003-04-09 13:25:43 +000073 """BundleBuilder is a barebones class for assembling bundles. It
74 knows nothing about executables or icons, it only copies files
75 and creates the PkgInfo and Info.plist files.
76 """
Just van Rossumad33d722002-11-21 10:23:04 +000077
Jack Jansen0ae32202003-04-09 13:25:43 +000078 # (Note that Defaults.__init__ (deep)copies these values to
79 # instance variables. Mutable defaults are therefore safe.)
Just van Rossumda302da2002-11-23 22:26:44 +000080
Jack Jansen0ae32202003-04-09 13:25:43 +000081 # Name of the bundle, with or without extension.
82 name = None
Just van Rossumda302da2002-11-23 22:26:44 +000083
Jack Jansen0ae32202003-04-09 13:25:43 +000084 # The property list ("plist")
85 plist = Plist(CFBundleDevelopmentRegion = "English",
86 CFBundleInfoDictionaryVersion = "6.0")
Just van Rossumda302da2002-11-23 22:26:44 +000087
Jack Jansen0ae32202003-04-09 13:25:43 +000088 # The type of the bundle.
89 type = "BNDL"
90 # The creator code of the bundle.
91 creator = None
Just van Rossumda302da2002-11-23 22:26:44 +000092
Just van Rossumbe56aae2003-07-04 14:20:03 +000093 # the CFBundleIdentifier (this is used for the preferences file name)
94 bundle_id = None
95
Jack Jansen0ae32202003-04-09 13:25:43 +000096 # List of files that have to be copied to <bundle>/Contents/Resources.
97 resources = []
Just van Rossumda302da2002-11-23 22:26:44 +000098
Jack Jansen0ae32202003-04-09 13:25:43 +000099 # List of (src, dest) tuples; dest should be a path relative to the bundle
100 # (eg. "Contents/Resources/MyStuff/SomeFile.ext).
101 files = []
Just van Rossumda302da2002-11-23 22:26:44 +0000102
Jack Jansen0ae32202003-04-09 13:25:43 +0000103 # List of shared libraries (dylibs, Frameworks) to bundle with the app
104 # will be placed in Contents/Frameworks
105 libs = []
Just van Rossum15624d82003-03-21 09:26:59 +0000106
Jack Jansen0ae32202003-04-09 13:25:43 +0000107 # Directory where the bundle will be assembled.
108 builddir = "build"
Just van Rossumda302da2002-11-23 22:26:44 +0000109
Jack Jansen0ae32202003-04-09 13:25:43 +0000110 # Make symlinks instead copying files. This is handy during debugging, but
111 # makes the bundle non-distributable.
112 symlink = 0
Just van Rossumda302da2002-11-23 22:26:44 +0000113
Jack Jansen0ae32202003-04-09 13:25:43 +0000114 # Verbosity level.
115 verbosity = 1
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000116
Jack Jansenc77f6df2004-12-27 15:51:03 +0000117 # Destination root directory
118 destroot = ""
Just van Rossumad33d722002-11-21 10:23:04 +0000119
Jack Jansen0ae32202003-04-09 13:25:43 +0000120 def setup(self):
121 # XXX rethink self.name munging, this is brittle.
122 self.name, ext = os.path.splitext(self.name)
123 if not ext:
124 ext = ".bundle"
125 bundleextension = ext
126 # misc (derived) attributes
127 self.bundlepath = pathjoin(self.builddir, self.name + bundleextension)
Just van Rossumceeb9622002-11-21 23:19:37 +0000128
Jack Jansen0ae32202003-04-09 13:25:43 +0000129 plist = self.plist
130 plist.CFBundleName = self.name
131 plist.CFBundlePackageType = self.type
132 if self.creator is None:
133 if hasattr(plist, "CFBundleSignature"):
134 self.creator = plist.CFBundleSignature
135 else:
136 self.creator = "????"
137 plist.CFBundleSignature = self.creator
Just van Rossumbe56aae2003-07-04 14:20:03 +0000138 if self.bundle_id:
139 plist.CFBundleIdentifier = self.bundle_id
140 elif not hasattr(plist, "CFBundleIdentifier"):
Jack Jansen0ae32202003-04-09 13:25:43 +0000141 plist.CFBundleIdentifier = self.name
Just van Rossumceeb9622002-11-21 23:19:37 +0000142
Jack Jansen0ae32202003-04-09 13:25:43 +0000143 def build(self):
144 """Build the bundle."""
145 builddir = self.builddir
146 if builddir and not os.path.exists(builddir):
147 os.mkdir(builddir)
148 self.message("Building %s" % repr(self.bundlepath), 1)
149 if os.path.exists(self.bundlepath):
150 shutil.rmtree(self.bundlepath)
Ronald Oussoren836b0392006-05-14 19:56:34 +0000151 if os.path.exists(self.bundlepath + '~'):
152 shutil.rmtree(self.bundlepath + '~')
153 bp = self.bundlepath
154
155 # Create the app bundle in a temporary location and then
Tim Peterscbd7b752006-05-16 23:22:20 +0000156 # rename the completed bundle. This way the Finder will
Ronald Oussoren836b0392006-05-14 19:56:34 +0000157 # never see an incomplete bundle (where it might pick up
158 # and cache the wrong meta data)
159 self.bundlepath = bp + '~'
160 try:
161 os.mkdir(self.bundlepath)
162 self.preProcess()
163 self._copyFiles()
164 self._addMetaFiles()
165 self.postProcess()
166 os.rename(self.bundlepath, bp)
167 finally:
168 self.bundlepath = bp
Jack Jansen0ae32202003-04-09 13:25:43 +0000169 self.message("Done.", 1)
Just van Rossumad33d722002-11-21 10:23:04 +0000170
Jack Jansen0ae32202003-04-09 13:25:43 +0000171 def preProcess(self):
172 """Hook for subclasses."""
173 pass
174 def postProcess(self):
175 """Hook for subclasses."""
176 pass
Just van Rossumad33d722002-11-21 10:23:04 +0000177
Jack Jansen0ae32202003-04-09 13:25:43 +0000178 def _addMetaFiles(self):
179 contents = pathjoin(self.bundlepath, "Contents")
180 makedirs(contents)
181 #
182 # Write Contents/PkgInfo
183 assert len(self.type) == len(self.creator) == 4, \
184 "type and creator must be 4-byte strings."
185 pkginfo = pathjoin(contents, "PkgInfo")
186 f = open(pkginfo, "wb")
187 f.write(self.type + self.creator)
188 f.close()
189 #
190 # Write Contents/Info.plist
191 infoplist = pathjoin(contents, "Info.plist")
192 self.plist.write(infoplist)
Just van Rossumad33d722002-11-21 10:23:04 +0000193
Jack Jansen0ae32202003-04-09 13:25:43 +0000194 def _copyFiles(self):
195 files = self.files[:]
196 for path in self.resources:
197 files.append((path, pathjoin("Contents", "Resources",
Just van Rossumdc31dc02003-06-20 21:43:36 +0000198 os.path.basename(path))))
Jack Jansen0ae32202003-04-09 13:25:43 +0000199 for path in self.libs:
200 files.append((path, pathjoin("Contents", "Frameworks",
Just van Rossumdc31dc02003-06-20 21:43:36 +0000201 os.path.basename(path))))
Jack Jansen0ae32202003-04-09 13:25:43 +0000202 if self.symlink:
203 self.message("Making symbolic links", 1)
204 msg = "Making symlink from"
205 else:
206 self.message("Copying files", 1)
207 msg = "Copying"
208 files.sort()
209 for src, dst in files:
210 if os.path.isdir(src):
211 self.message("%s %s/ to %s/" % (msg, src, dst), 2)
212 else:
213 self.message("%s %s to %s" % (msg, src, dst), 2)
214 dst = pathjoin(self.bundlepath, dst)
215 if self.symlink:
216 symlink(src, dst, mkdirs=1)
217 else:
218 copy(src, dst, mkdirs=1)
Just van Rossumad33d722002-11-21 10:23:04 +0000219
Jack Jansen0ae32202003-04-09 13:25:43 +0000220 def message(self, msg, level=0):
221 if level <= self.verbosity:
222 indent = ""
223 if level > 1:
224 indent = (level - 1) * " "
225 sys.stderr.write(indent + msg + "\n")
Just van Rossumceeb9622002-11-21 23:19:37 +0000226
Jack Jansen0ae32202003-04-09 13:25:43 +0000227 def report(self):
228 # XXX something decent
229 pass
Just van Rossumad33d722002-11-21 10:23:04 +0000230
231
Just van Rossumcef32882002-11-26 00:34:52 +0000232if __debug__:
Jack Jansen0ae32202003-04-09 13:25:43 +0000233 PYC_EXT = ".pyc"
Just van Rossumcef32882002-11-26 00:34:52 +0000234else:
Jack Jansen0ae32202003-04-09 13:25:43 +0000235 PYC_EXT = ".pyo"
Just van Rossumcef32882002-11-26 00:34:52 +0000236
237MAGIC = imp.get_magic()
Just van Rossum109ecbf2003-01-02 13:13:01 +0000238USE_ZIPIMPORT = "zipimport" in sys.builtin_module_names
Just van Rossumcef32882002-11-26 00:34:52 +0000239
240# For standalone apps, we have our own minimal site.py. We don't need
241# all the cruft of the real site.py.
242SITE_PY = """\
243import sys
Just van Rossum762d2cc2003-06-29 21:54:12 +0000244if not %(semi_standalone)s:
245 del sys.path[1:] # sys.path[0] is Contents/Resources/
Just van Rossumcef32882002-11-26 00:34:52 +0000246"""
247
Ronald Oussoren072bb402009-01-02 15:25:36 +0000248ZIP_ARCHIVE = "Modules.zip"
249SITE_PY_ZIP = SITE_PY + ("sys.path.append(sys.path[0] + '/%s')\n" % ZIP_ARCHIVE)
250
251def getPycData(fullname, code, ispkg):
252 if ispkg:
253 fullname += ".__init__"
254 path = fullname.replace(".", os.sep) + PYC_EXT
255 return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
Just van Rossumcef32882002-11-26 00:34:52 +0000256
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000257#
258# Extension modules can't be in the modules zip archive, so a placeholder
259# is added instead, that loads the extension from a specified location.
260#
Just van Rossum535ffa22002-11-29 20:06:52 +0000261EXT_LOADER = """\
Just van Rossum109ecbf2003-01-02 13:13:01 +0000262def __load():
Jack Jansen0ae32202003-04-09 13:25:43 +0000263 import imp, sys, os
264 for p in sys.path:
265 path = os.path.join(p, "%(filename)s")
266 if os.path.exists(path):
267 break
268 else:
269 assert 0, "file not found: %(filename)s"
270 mod = imp.load_dynamic("%(name)s", path)
Just van Rossum109ecbf2003-01-02 13:13:01 +0000271
272__load()
273del __load
Just van Rossum535ffa22002-11-29 20:06:52 +0000274"""
275
Just van Rossumcef32882002-11-26 00:34:52 +0000276MAYMISS_MODULES = ['mac', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
Jack Jansen0ae32202003-04-09 13:25:43 +0000277 'win32api', 'ce', '_winreg', 'nturl2path', 'sitecustomize',
278 'org.python.core', 'riscos', 'riscosenviron', 'riscospath'
Just van Rossumcef32882002-11-26 00:34:52 +0000279]
280
281STRIP_EXEC = "/usr/bin/strip"
282
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000283#
284# We're using a stock interpreter to run the app, yet we need
285# a way to pass the Python main program to the interpreter. The
286# bootstrapping script fires up the interpreter with the right
287# arguments. os.execve() is used as OSX doesn't like us to
288# start a real new process. Also, the executable name must match
289# the CFBundleExecutable value in the Info.plist, so we lie
290# deliberately with argv[0]. The actual Python executable is
291# passed in an environment variable so we can "repair"
292# sys.executable later.
293#
Just van Rossum74bdca82002-11-28 11:30:56 +0000294BOOTSTRAP_SCRIPT = """\
Just van Rossum0ff7a4e2003-02-26 11:27:56 +0000295#!%(hashbang)s
Just van Rossumad33d722002-11-21 10:23:04 +0000296
Just van Rossum7322b1a2003-02-25 20:15:40 +0000297import sys, os
298execdir = os.path.dirname(sys.argv[0])
299executable = os.path.join(execdir, "%(executable)s")
300resdir = os.path.join(os.path.dirname(execdir), "Resources")
Just van Rossum15624d82003-03-21 09:26:59 +0000301libdir = os.path.join(os.path.dirname(execdir), "Frameworks")
Just van Rossum7322b1a2003-02-25 20:15:40 +0000302mainprogram = os.path.join(resdir, "%(mainprogram)s")
303
Ronald Oussoren072bb402009-01-02 15:25:36 +0000304if %(optimize)s:
305 sys.argv.insert(1, '-O')
306
Just van Rossum7322b1a2003-02-25 20:15:40 +0000307sys.argv.insert(1, mainprogram)
Just van Rossumbe56aae2003-07-04 14:20:03 +0000308if %(standalone)s or %(semi_standalone)s:
309 os.environ["PYTHONPATH"] = resdir
310 if %(standalone)s:
311 os.environ["PYTHONHOME"] = resdir
312else:
313 pypath = os.getenv("PYTHONPATH", "")
314 if pypath:
315 pypath = ":" + pypath
316 os.environ["PYTHONPATH"] = resdir + pypath
Ronald Oussoren072bb402009-01-02 15:25:36 +0000317
Just van Rossum7322b1a2003-02-25 20:15:40 +0000318os.environ["PYTHONEXECUTABLE"] = executable
Just van Rossum15624d82003-03-21 09:26:59 +0000319os.environ["DYLD_LIBRARY_PATH"] = libdir
Just van Rossum3166f592003-06-20 18:56:10 +0000320os.environ["DYLD_FRAMEWORK_PATH"] = libdir
Just van Rossum7322b1a2003-02-25 20:15:40 +0000321os.execve(executable, sys.argv, os.environ)
Just van Rossumad33d722002-11-21 10:23:04 +0000322"""
323
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000324
325#
326# Optional wrapper that converts "dropped files" into sys.argv values.
327#
328ARGV_EMULATOR = """\
Jack Jansena03adde2003-02-18 23:29:46 +0000329import argvemulator, os
330
331argvemulator.ArgvCollector().mainloop()
332execfile(os.path.join(os.path.split(__file__)[0], "%(realmainprogram)s"))
333"""
Just van Rossumcef32882002-11-26 00:34:52 +0000334
Just van Rossum3166f592003-06-20 18:56:10 +0000335#
336# When building a standalone app with Python.framework, we need to copy
337# a subset from Python.framework to the bundle. The following list
338# specifies exactly what items we'll copy.
339#
340PYTHONFRAMEWORKGOODIES = [
341 "Python", # the Python core library
342 "Resources/English.lproj",
343 "Resources/Info.plist",
344 "Resources/version.plist",
345]
346
Just van Rossum79b0ae12003-06-29 22:20:26 +0000347def isFramework():
348 return sys.exec_prefix.find("Python.framework") > 0
349
Just van Rossum8a3ed3f2003-02-25 20:53:12 +0000350
Just van Rossum762d2cc2003-06-29 21:54:12 +0000351LIB = os.path.join(sys.prefix, "lib", "python" + sys.version[:3])
352SITE_PACKAGES = os.path.join(LIB, "site-packages")
353
354
Just van Rossumad33d722002-11-21 10:23:04 +0000355class AppBuilder(BundleBuilder):
356
Ronald Oussoren072bb402009-01-02 15:25:36 +0000357 use_zipimport = USE_ZIPIMPORT
358
Jack Jansen0ae32202003-04-09 13:25:43 +0000359 # Override type of the bundle.
360 type = "APPL"
Jack Jansencc81b802003-03-05 14:42:18 +0000361
Jack Jansen0ae32202003-04-09 13:25:43 +0000362 # platform, name of the subfolder of Contents that contains the executable.
363 platform = "MacOS"
Jack Jansencc81b802003-03-05 14:42:18 +0000364
Jack Jansen0ae32202003-04-09 13:25:43 +0000365 # A Python main program. If this argument is given, the main
366 # executable in the bundle will be a small wrapper that invokes
367 # the main program. (XXX Discuss why.)
368 mainprogram = None
Just van Rossumceeb9622002-11-21 23:19:37 +0000369
Jack Jansen0ae32202003-04-09 13:25:43 +0000370 # The main executable. If a Python main program is specified
371 # the executable will be copied to Resources and be invoked
372 # by the wrapper program mentioned above. Otherwise it will
373 # simply be used as the main executable.
374 executable = None
Just van Rossumceeb9622002-11-21 23:19:37 +0000375
Jack Jansen0ae32202003-04-09 13:25:43 +0000376 # The name of the main nib, for Cocoa apps. *Must* be specified
377 # when building a Cocoa app.
378 nibname = None
Just van Rossumad33d722002-11-21 10:23:04 +0000379
Jack Jansen0ae32202003-04-09 13:25:43 +0000380 # The name of the icon file to be copied to Resources and used for
381 # the Finder icon.
382 iconfile = None
Just van Rossum2aa09562003-02-01 08:34:46 +0000383
Jack Jansen0ae32202003-04-09 13:25:43 +0000384 # Symlink the executable instead of copying it.
385 symlink_exec = 0
Just van Rossumad33d722002-11-21 10:23:04 +0000386
Jack Jansen0ae32202003-04-09 13:25:43 +0000387 # If True, build standalone app.
388 standalone = 0
Just van Rossum3166f592003-06-20 18:56:10 +0000389
Just van Rossum762d2cc2003-06-29 21:54:12 +0000390 # If True, build semi-standalone app (only includes third-party modules).
391 semi_standalone = 0
392
Jack Jansen8ba0e802003-05-25 22:00:17 +0000393 # If set, use this for #! lines in stead of sys.executable
394 python = None
Just van Rossum3166f592003-06-20 18:56:10 +0000395
Jack Jansen0ae32202003-04-09 13:25:43 +0000396 # If True, add a real main program that emulates sys.argv before calling
397 # mainprogram
398 argv_emulation = 0
Just van Rossumcef32882002-11-26 00:34:52 +0000399
Jack Jansen0ae32202003-04-09 13:25:43 +0000400 # The following attributes are only used when building a standalone app.
Just van Rossumcef32882002-11-26 00:34:52 +0000401
Jack Jansen0ae32202003-04-09 13:25:43 +0000402 # Exclude these modules.
403 excludeModules = []
Just van Rossumcef32882002-11-26 00:34:52 +0000404
Jack Jansen0ae32202003-04-09 13:25:43 +0000405 # Include these modules.
406 includeModules = []
Just van Rossumcef32882002-11-26 00:34:52 +0000407
Jack Jansen0ae32202003-04-09 13:25:43 +0000408 # Include these packages.
409 includePackages = []
Just van Rossumcef32882002-11-26 00:34:52 +0000410
Just van Rossum00a0b972003-06-20 21:18:22 +0000411 # Strip binaries from debug info.
Jack Jansen0ae32202003-04-09 13:25:43 +0000412 strip = 0
Just van Rossumcef32882002-11-26 00:34:52 +0000413
Jack Jansen0ae32202003-04-09 13:25:43 +0000414 # Found Python modules: [(name, codeobject, ispkg), ...]
415 pymodules = []
Just van Rossumcef32882002-11-26 00:34:52 +0000416
Jack Jansen0ae32202003-04-09 13:25:43 +0000417 # Modules that modulefinder couldn't find:
418 missingModules = []
419 maybeMissingModules = []
Just van Rossumcef32882002-11-26 00:34:52 +0000420
Jack Jansen0ae32202003-04-09 13:25:43 +0000421 def setup(self):
Just van Rossum762d2cc2003-06-29 21:54:12 +0000422 if ((self.standalone or self.semi_standalone)
423 and self.mainprogram is None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000424 raise BundleBuilderError, ("must specify 'mainprogram' when "
425 "building a standalone application.")
426 if self.mainprogram is None and self.executable is None:
427 raise BundleBuilderError, ("must specify either or both of "
428 "'executable' and 'mainprogram'")
Just van Rossumceeb9622002-11-21 23:19:37 +0000429
Jack Jansen0ae32202003-04-09 13:25:43 +0000430 self.execdir = pathjoin("Contents", self.platform)
Jack Jansencc81b802003-03-05 14:42:18 +0000431
Jack Jansen0ae32202003-04-09 13:25:43 +0000432 if self.name is not None:
433 pass
434 elif self.mainprogram is not None:
435 self.name = os.path.splitext(os.path.basename(self.mainprogram))[0]
436 elif executable is not None:
437 self.name = os.path.splitext(os.path.basename(self.executable))[0]
438 if self.name[-4:] != ".app":
439 self.name += ".app"
Just van Rossumceeb9622002-11-21 23:19:37 +0000440
Jack Jansen0ae32202003-04-09 13:25:43 +0000441 if self.executable is None:
Just van Rossum79b0ae12003-06-29 22:20:26 +0000442 if not self.standalone and not isFramework():
Jack Jansen0ae32202003-04-09 13:25:43 +0000443 self.symlink_exec = 1
Jack Jansenbbaa0832003-07-04 11:05:35 +0000444 if self.python:
445 self.executable = self.python
446 else:
447 self.executable = sys.executable
Just van Rossum74bdca82002-11-28 11:30:56 +0000448
Jack Jansen0ae32202003-04-09 13:25:43 +0000449 if self.nibname:
450 self.plist.NSMainNibFile = self.nibname
451 if not hasattr(self.plist, "NSPrincipalClass"):
452 self.plist.NSPrincipalClass = "NSApplication"
Just van Rossumceeb9622002-11-21 23:19:37 +0000453
Just van Rossum79b0ae12003-06-29 22:20:26 +0000454 if self.standalone and isFramework():
Just van Rossum3166f592003-06-20 18:56:10 +0000455 self.addPythonFramework()
456
Jack Jansen0ae32202003-04-09 13:25:43 +0000457 BundleBuilder.setup(self)
Just van Rossumceeb9622002-11-21 23:19:37 +0000458
Jack Jansen0ae32202003-04-09 13:25:43 +0000459 self.plist.CFBundleExecutable = self.name
Just van Rossumf7aba232002-11-22 00:31:50 +0000460
Just van Rossum762d2cc2003-06-29 21:54:12 +0000461 if self.standalone or self.semi_standalone:
Jack Jansen0ae32202003-04-09 13:25:43 +0000462 self.findDependencies()
Just van Rossumcef32882002-11-26 00:34:52 +0000463
Jack Jansen0ae32202003-04-09 13:25:43 +0000464 def preProcess(self):
465 resdir = "Contents/Resources"
466 if self.executable is not None:
467 if self.mainprogram is None:
468 execname = self.name
469 else:
470 execname = os.path.basename(self.executable)
471 execpath = pathjoin(self.execdir, execname)
472 if not self.symlink_exec:
Jack Jansenc77f6df2004-12-27 15:51:03 +0000473 self.files.append((self.destroot + self.executable, execpath))
Jack Jansen0ae32202003-04-09 13:25:43 +0000474 self.execpath = execpath
Just van Rossumad33d722002-11-21 10:23:04 +0000475
Jack Jansen0ae32202003-04-09 13:25:43 +0000476 if self.mainprogram is not None:
477 mainprogram = os.path.basename(self.mainprogram)
478 self.files.append((self.mainprogram, pathjoin(resdir, mainprogram)))
479 if self.argv_emulation:
480 # Change the main program, and create the helper main program (which
481 # does argv collection and then calls the real main).
482 # Also update the included modules (if we're creating a standalone
483 # program) and the plist
484 realmainprogram = mainprogram
485 mainprogram = '__argvemulator_' + mainprogram
486 resdirpath = pathjoin(self.bundlepath, resdir)
487 mainprogrampath = pathjoin(resdirpath, mainprogram)
488 makedirs(resdirpath)
489 open(mainprogrampath, "w").write(ARGV_EMULATOR % locals())
Just van Rossum762d2cc2003-06-29 21:54:12 +0000490 if self.standalone or self.semi_standalone:
Jack Jansen0ae32202003-04-09 13:25:43 +0000491 self.includeModules.append("argvemulator")
492 self.includeModules.append("os")
493 if not self.plist.has_key("CFBundleDocumentTypes"):
494 self.plist["CFBundleDocumentTypes"] = [
495 { "CFBundleTypeOSTypes" : [
496 "****",
497 "fold",
498 "disk"],
499 "CFBundleTypeRole": "Viewer"}]
500 # Write bootstrap script
501 executable = os.path.basename(self.executable)
502 execdir = pathjoin(self.bundlepath, self.execdir)
503 bootstrappath = pathjoin(execdir, self.name)
504 makedirs(execdir)
Just van Rossum762d2cc2003-06-29 21:54:12 +0000505 if self.standalone or self.semi_standalone:
Jack Jansen0ae32202003-04-09 13:25:43 +0000506 # XXX we're screwed when the end user has deleted
507 # /usr/bin/python
508 hashbang = "/usr/bin/python"
Jack Jansen8ba0e802003-05-25 22:00:17 +0000509 elif self.python:
510 hashbang = self.python
Jack Jansen0ae32202003-04-09 13:25:43 +0000511 else:
512 hashbang = os.path.realpath(sys.executable)
513 standalone = self.standalone
Just van Rossumbe56aae2003-07-04 14:20:03 +0000514 semi_standalone = self.semi_standalone
Ronald Oussoren072bb402009-01-02 15:25:36 +0000515 optimize = sys.flags.optimize
Jack Jansen0ae32202003-04-09 13:25:43 +0000516 open(bootstrappath, "w").write(BOOTSTRAP_SCRIPT % locals())
517 os.chmod(bootstrappath, 0775)
Just van Rossumad33d722002-11-21 10:23:04 +0000518
Jack Jansen0ae32202003-04-09 13:25:43 +0000519 if self.iconfile is not None:
520 iconbase = os.path.basename(self.iconfile)
521 self.plist.CFBundleIconFile = iconbase
522 self.files.append((self.iconfile, pathjoin(resdir, iconbase)))
Just van Rossum2aa09562003-02-01 08:34:46 +0000523
Jack Jansen0ae32202003-04-09 13:25:43 +0000524 def postProcess(self):
Just van Rossum762d2cc2003-06-29 21:54:12 +0000525 if self.standalone or self.semi_standalone:
Jack Jansen0ae32202003-04-09 13:25:43 +0000526 self.addPythonModules()
527 if self.strip and not self.symlink:
528 self.stripBinaries()
Just van Rossumcef32882002-11-26 00:34:52 +0000529
Jack Jansen0ae32202003-04-09 13:25:43 +0000530 if self.symlink_exec and self.executable:
531 self.message("Symlinking executable %s to %s" % (self.executable,
532 self.execpath), 2)
533 dst = pathjoin(self.bundlepath, self.execpath)
534 makedirs(os.path.dirname(dst))
535 os.symlink(os.path.abspath(self.executable), dst)
Just van Rossum16aebf72002-11-22 11:43:10 +0000536
Jack Jansen0ae32202003-04-09 13:25:43 +0000537 if self.missingModules or self.maybeMissingModules:
538 self.reportMissing()
Just van Rossumcef32882002-11-26 00:34:52 +0000539
Just van Rossum3166f592003-06-20 18:56:10 +0000540 def addPythonFramework(self):
541 # If we're building a standalone app with Python.framework,
Just van Rossumdc31dc02003-06-20 21:43:36 +0000542 # include a minimal subset of Python.framework, *unless*
543 # Python.framework was specified manually in self.libs.
544 for lib in self.libs:
545 if os.path.basename(lib) == "Python.framework":
546 # a Python.framework was specified as a library
547 return
548
Just van Rossum3166f592003-06-20 18:56:10 +0000549 frameworkpath = sys.exec_prefix[:sys.exec_prefix.find(
550 "Python.framework") + len("Python.framework")]
Just van Rossumdc31dc02003-06-20 21:43:36 +0000551
Just van Rossum3166f592003-06-20 18:56:10 +0000552 version = sys.version[:3]
553 frameworkpath = pathjoin(frameworkpath, "Versions", version)
554 destbase = pathjoin("Contents", "Frameworks", "Python.framework",
555 "Versions", version)
556 for item in PYTHONFRAMEWORKGOODIES:
557 src = pathjoin(frameworkpath, item)
558 dst = pathjoin(destbase, item)
559 self.files.append((src, dst))
560
Just van Rossum762d2cc2003-06-29 21:54:12 +0000561 def _getSiteCode(self):
Ronald Oussoren072bb402009-01-02 15:25:36 +0000562 if self.use_zipimport:
563 return compile(SITE_PY % {"semi_standalone": self.semi_standalone},
Just van Rossum762d2cc2003-06-29 21:54:12 +0000564 "<-bundlebuilder.py->", "exec")
565
Jack Jansen0ae32202003-04-09 13:25:43 +0000566 def addPythonModules(self):
567 self.message("Adding Python modules", 1)
Just van Rossumcef32882002-11-26 00:34:52 +0000568
Ronald Oussoren072bb402009-01-02 15:25:36 +0000569 if self.use_zipimport:
Jack Jansen0ae32202003-04-09 13:25:43 +0000570 # Create a zip file containing all modules as pyc.
571 import zipfile
572 relpath = pathjoin("Contents", "Resources", ZIP_ARCHIVE)
573 abspath = pathjoin(self.bundlepath, relpath)
574 zf = zipfile.ZipFile(abspath, "w", zipfile.ZIP_DEFLATED)
575 for name, code, ispkg in self.pymodules:
576 self.message("Adding Python module %s" % name, 2)
577 path, pyc = getPycData(name, code, ispkg)
578 zf.writestr(path, pyc)
579 zf.close()
580 # add site.pyc
581 sitepath = pathjoin(self.bundlepath, "Contents", "Resources",
582 "site" + PYC_EXT)
Just van Rossum762d2cc2003-06-29 21:54:12 +0000583 writePyc(self._getSiteCode(), sitepath)
Jack Jansen0ae32202003-04-09 13:25:43 +0000584 else:
585 # Create individual .pyc files.
586 for name, code, ispkg in self.pymodules:
587 if ispkg:
588 name += ".__init__"
589 path = name.split(".")
590 path = pathjoin("Contents", "Resources", *path) + PYC_EXT
Just van Rossumcef32882002-11-26 00:34:52 +0000591
Jack Jansen0ae32202003-04-09 13:25:43 +0000592 if ispkg:
593 self.message("Adding Python package %s" % path, 2)
594 else:
595 self.message("Adding Python module %s" % path, 2)
Just van Rossumcef32882002-11-26 00:34:52 +0000596
Jack Jansen0ae32202003-04-09 13:25:43 +0000597 abspath = pathjoin(self.bundlepath, path)
598 makedirs(os.path.dirname(abspath))
599 writePyc(code, abspath)
Just van Rossumcef32882002-11-26 00:34:52 +0000600
Jack Jansen0ae32202003-04-09 13:25:43 +0000601 def stripBinaries(self):
602 if not os.path.exists(STRIP_EXEC):
603 self.message("Error: can't strip binaries: no strip program at "
604 "%s" % STRIP_EXEC, 0)
605 else:
Just van Rossum00a0b972003-06-20 21:18:22 +0000606 import stat
Jack Jansen0ae32202003-04-09 13:25:43 +0000607 self.message("Stripping binaries", 1)
Just van Rossum00a0b972003-06-20 21:18:22 +0000608 def walk(top):
609 for name in os.listdir(top):
610 path = pathjoin(top, name)
611 if os.path.islink(path):
612 continue
613 if os.path.isdir(path):
614 walk(path)
615 else:
616 mod = os.stat(path)[stat.ST_MODE]
617 if not (mod & 0100):
618 continue
619 relpath = path[len(self.bundlepath):]
620 self.message("Stripping %s" % relpath, 2)
621 inf, outf = os.popen4("%s -S \"%s\"" %
622 (STRIP_EXEC, path))
623 output = outf.read().strip()
624 if output:
625 # usually not a real problem, like when we're
626 # trying to strip a script
627 self.message("Problem stripping %s:" % relpath, 3)
628 self.message(output, 3)
629 walk(self.bundlepath)
Just van Rossumcef32882002-11-26 00:34:52 +0000630
Jack Jansen0ae32202003-04-09 13:25:43 +0000631 def findDependencies(self):
632 self.message("Finding module dependencies", 1)
633 import modulefinder
634 mf = modulefinder.ModuleFinder(excludes=self.excludeModules)
Ronald Oussoren072bb402009-01-02 15:25:36 +0000635 if self.use_zipimport:
Jack Jansen0ae32202003-04-09 13:25:43 +0000636 # zipimport imports zlib, must add it manually
637 mf.import_hook("zlib")
638 # manually add our own site.py
639 site = mf.add_module("site")
Just van Rossum762d2cc2003-06-29 21:54:12 +0000640 site.__code__ = self._getSiteCode()
641 mf.scan_code(site.__code__, site)
Just van Rossumcef32882002-11-26 00:34:52 +0000642
Jack Jansen0ae32202003-04-09 13:25:43 +0000643 # warnings.py gets imported implicitly from C
644 mf.import_hook("warnings")
Just van Rossum7322b1a2003-02-25 20:15:40 +0000645
Jack Jansen0ae32202003-04-09 13:25:43 +0000646 includeModules = self.includeModules[:]
647 for name in self.includePackages:
648 includeModules.extend(findPackageContents(name).keys())
649 for name in includeModules:
650 try:
651 mf.import_hook(name)
652 except ImportError:
653 self.missingModules.append(name)
Just van Rossumcef32882002-11-26 00:34:52 +0000654
Jack Jansen0ae32202003-04-09 13:25:43 +0000655 mf.run_script(self.mainprogram)
656 modules = mf.modules.items()
657 modules.sort()
658 for name, mod in modules:
Just van Rossum762d2cc2003-06-29 21:54:12 +0000659 path = mod.__file__
660 if path and self.semi_standalone:
661 # skip the standard library
662 if path.startswith(LIB) and not path.startswith(SITE_PACKAGES):
663 continue
664 if path and mod.__code__ is None:
Jack Jansen0ae32202003-04-09 13:25:43 +0000665 # C extension
Jack Jansen0ae32202003-04-09 13:25:43 +0000666 filename = os.path.basename(path)
Just van Rossum79b0ae12003-06-29 22:20:26 +0000667 pathitems = name.split(".")[:-1] + [filename]
668 dstpath = pathjoin(*pathitems)
Ronald Oussoren072bb402009-01-02 15:25:36 +0000669 if self.use_zipimport:
Just van Rossum79b0ae12003-06-29 22:20:26 +0000670 if name != "zlib":
671 # neatly pack all extension modules in a subdirectory,
672 # except zlib, since it's neccesary for bootstrapping.
673 dstpath = pathjoin("ExtensionModules", dstpath)
Jack Jansen0ae32202003-04-09 13:25:43 +0000674 # Python modules are stored in a Zip archive, but put
Just van Rossum762d2cc2003-06-29 21:54:12 +0000675 # extensions in Contents/Resources/. Add a tiny "loader"
Jack Jansen0ae32202003-04-09 13:25:43 +0000676 # program in the Zip archive. Due to Thomas Heller.
Just van Rossum762d2cc2003-06-29 21:54:12 +0000677 source = EXT_LOADER % {"name": name, "filename": dstpath}
Jack Jansen0ae32202003-04-09 13:25:43 +0000678 code = compile(source, "<dynloader for %s>" % name, "exec")
679 mod.__code__ = code
Just van Rossum762d2cc2003-06-29 21:54:12 +0000680 self.files.append((path, pathjoin("Contents", "Resources", dstpath)))
Jack Jansen0ae32202003-04-09 13:25:43 +0000681 if mod.__code__ is not None:
682 ispkg = mod.__path__ is not None
Ronald Oussoren072bb402009-01-02 15:25:36 +0000683 if not self.use_zipimport or name != "site":
Jack Jansen0ae32202003-04-09 13:25:43 +0000684 # Our site.py is doing the bootstrapping, so we must
Ronald Oussoren072bb402009-01-02 15:25:36 +0000685 # include a real .pyc file if self.use_zipimport is True.
Jack Jansen0ae32202003-04-09 13:25:43 +0000686 self.pymodules.append((name, mod.__code__, ispkg))
Just van Rossumcef32882002-11-26 00:34:52 +0000687
Jack Jansen0ae32202003-04-09 13:25:43 +0000688 if hasattr(mf, "any_missing_maybe"):
689 missing, maybe = mf.any_missing_maybe()
690 else:
691 missing = mf.any_missing()
692 maybe = []
693 self.missingModules.extend(missing)
694 self.maybeMissingModules.extend(maybe)
Just van Rossumcef32882002-11-26 00:34:52 +0000695
Jack Jansen0ae32202003-04-09 13:25:43 +0000696 def reportMissing(self):
697 missing = [name for name in self.missingModules
698 if name not in MAYMISS_MODULES]
699 if self.maybeMissingModules:
700 maybe = self.maybeMissingModules
701 else:
702 maybe = [name for name in missing if "." in name]
703 missing = [name for name in missing if "." not in name]
704 missing.sort()
705 maybe.sort()
706 if maybe:
707 self.message("Warning: couldn't find the following submodules:", 1)
708 self.message(" (Note that these could be false alarms -- "
709 "it's not always", 1)
710 self.message(" possible to distinguish between \"from package "
711 "import submodule\" ", 1)
712 self.message(" and \"from package import name\")", 1)
713 for name in maybe:
714 self.message(" ? " + name, 1)
715 if missing:
716 self.message("Warning: couldn't find the following modules:", 1)
717 for name in missing:
718 self.message(" ? " + name, 1)
Just van Rossum74bdca82002-11-28 11:30:56 +0000719
Jack Jansen0ae32202003-04-09 13:25:43 +0000720 def report(self):
721 # XXX something decent
722 import pprint
723 pprint.pprint(self.__dict__)
Just van Rossum762d2cc2003-06-29 21:54:12 +0000724 if self.standalone or self.semi_standalone:
Jack Jansen0ae32202003-04-09 13:25:43 +0000725 self.reportMissing()
Just van Rossumcef32882002-11-26 00:34:52 +0000726
727#
728# Utilities.
729#
730
731SUFFIXES = [_suf for _suf, _mode, _tp in imp.get_suffixes()]
732identifierRE = re.compile(r"[_a-zA-z][_a-zA-Z0-9]*$")
733
734def findPackageContents(name, searchpath=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000735 head = name.split(".")[-1]
736 if identifierRE.match(head) is None:
737 return {}
738 try:
739 fp, path, (ext, mode, tp) = imp.find_module(head, searchpath)
740 except ImportError:
741 return {}
742 modules = {name: None}
743 if tp == imp.PKG_DIRECTORY and path:
744 files = os.listdir(path)
745 for sub in files:
746 sub, ext = os.path.splitext(sub)
747 fullname = name + "." + sub
748 if sub != "__init__" and fullname not in modules:
749 modules.update(findPackageContents(fullname, [path]))
750 return modules
Just van Rossumcef32882002-11-26 00:34:52 +0000751
752def writePyc(code, path):
Jack Jansen0ae32202003-04-09 13:25:43 +0000753 f = open(path, "wb")
754 f.write(MAGIC)
755 f.write("\0" * 4) # don't bother about a time stamp
756 marshal.dump(code, f)
757 f.close()
Just van Rossumad33d722002-11-21 10:23:04 +0000758
Just van Rossumad33d722002-11-21 10:23:04 +0000759def copy(src, dst, mkdirs=0):
Jack Jansen0ae32202003-04-09 13:25:43 +0000760 """Copy a file or a directory."""
761 if mkdirs:
762 makedirs(os.path.dirname(dst))
763 if os.path.isdir(src):
Just van Rossumdc31dc02003-06-20 21:43:36 +0000764 shutil.copytree(src, dst, symlinks=1)
Jack Jansen0ae32202003-04-09 13:25:43 +0000765 else:
766 shutil.copy2(src, dst)
Just van Rossumad33d722002-11-21 10:23:04 +0000767
768def copytodir(src, dstdir):
Jack Jansen0ae32202003-04-09 13:25:43 +0000769 """Copy a file or a directory to an existing directory."""
770 dst = pathjoin(dstdir, os.path.basename(src))
771 copy(src, dst)
Just van Rossumad33d722002-11-21 10:23:04 +0000772
773def makedirs(dir):
Jack Jansen0ae32202003-04-09 13:25:43 +0000774 """Make all directories leading up to 'dir' including the leaf
775 directory. Don't moan if any path element already exists."""
776 try:
777 os.makedirs(dir)
778 except OSError, why:
779 if why.errno != errno.EEXIST:
780 raise
Just van Rossumad33d722002-11-21 10:23:04 +0000781
782def symlink(src, dst, mkdirs=0):
Jack Jansen0ae32202003-04-09 13:25:43 +0000783 """Copy a file or a directory."""
784 if not os.path.exists(src):
785 raise IOError, "No such file or directory: '%s'" % src
786 if mkdirs:
787 makedirs(os.path.dirname(dst))
788 os.symlink(os.path.abspath(src), dst)
Just van Rossumad33d722002-11-21 10:23:04 +0000789
790def pathjoin(*args):
Jack Jansen0ae32202003-04-09 13:25:43 +0000791 """Safe wrapper for os.path.join: asserts that all but the first
792 argument are relative paths."""
793 for seg in args[1:]:
794 assert seg[0] != "/"
795 return os.path.join(*args)
Just van Rossumad33d722002-11-21 10:23:04 +0000796
797
Just van Rossumceeb9622002-11-21 23:19:37 +0000798cmdline_doc = """\
799Usage:
Just van Rossumf7aba232002-11-22 00:31:50 +0000800 python bundlebuilder.py [options] command
Just van Rossumceeb9622002-11-21 23:19:37 +0000801 python mybuildscript.py [options] command
802
803Commands:
804 build build the application
805 report print a report
806
807Options:
808 -b, --builddir=DIR the build directory; defaults to "build"
809 -n, --name=NAME application name
810 -r, --resource=FILE extra file or folder to be copied to Resources
Just van Rossum7215e082003-02-25 21:00:55 +0000811 -f, --file=SRC:DST extra file or folder to be copied into the bundle;
812 DST must be a path relative to the bundle root
Just van Rossumceeb9622002-11-21 23:19:37 +0000813 -e, --executable=FILE the executable to be used
814 -m, --mainprogram=FILE the Python main program
Jack Jansena03adde2003-02-18 23:29:46 +0000815 -a, --argv add a wrapper main program to create sys.argv
Just van Rossumceeb9622002-11-21 23:19:37 +0000816 -p, --plist=FILE .plist file (default: generate one)
817 --nib=NAME main nib name
818 -c, --creator=CCCC 4-char creator code (default: '????')
Just van Rossum9af69682003-02-02 18:56:37 +0000819 --iconfile=FILE filename of the icon (an .icns file) to be used
Just van Rossum2aa09562003-02-01 08:34:46 +0000820 as the Finder icon
Just van Rossumbe56aae2003-07-04 14:20:03 +0000821 --bundle-id=ID the CFBundleIdentifier, in reverse-dns format
822 (eg. org.python.BuildApplet; this is used for
823 the preferences file name)
Just van Rossumceeb9622002-11-21 23:19:37 +0000824 -l, --link symlink files/folder instead of copying them
Just van Rossum16aebf72002-11-22 11:43:10 +0000825 --link-exec symlink the executable instead of copying it
Just van Rossumcef32882002-11-26 00:34:52 +0000826 --standalone build a standalone application, which is fully
827 independent of a Python installation
Just van Rossum762d2cc2003-06-29 21:54:12 +0000828 --semi-standalone build a standalone application, which depends on
829 an installed Python, yet includes all third-party
830 modules.
Ronald Oussoren072bb402009-01-02 15:25:36 +0000831 --no-zipimport Do not copy code into a zip file
Jack Jansen8ba0e802003-05-25 22:00:17 +0000832 --python=FILE Python to use in #! line in stead of current Python
Just van Rossum15624d82003-03-21 09:26:59 +0000833 --lib=FILE shared library or framework to be copied into
834 the bundle
Just van Rossum762d2cc2003-06-29 21:54:12 +0000835 -x, --exclude=MODULE exclude module (with --(semi-)standalone)
836 -i, --include=MODULE include module (with --(semi-)standalone)
837 --package=PACKAGE include a whole package (with --(semi-)standalone)
Just van Rossumcef32882002-11-26 00:34:52 +0000838 --strip strip binaries (remove debug info)
Just van Rossumceeb9622002-11-21 23:19:37 +0000839 -v, --verbose increase verbosity level
840 -q, --quiet decrease verbosity level
841 -h, --help print this message
842"""
843
844def usage(msg=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000845 if msg:
846 print msg
847 print cmdline_doc
848 sys.exit(1)
Just van Rossumceeb9622002-11-21 23:19:37 +0000849
850def main(builder=None):
Jack Jansen0ae32202003-04-09 13:25:43 +0000851 if builder is None:
852 builder = AppBuilder(verbosity=1)
Just van Rossumceeb9622002-11-21 23:19:37 +0000853
Jack Jansen0ae32202003-04-09 13:25:43 +0000854 shortopts = "b:n:r:f:e:m:c:p:lx:i:hvqa"
855 longopts = ("builddir=", "name=", "resource=", "file=", "executable=",
856 "mainprogram=", "creator=", "nib=", "plist=", "link",
857 "link-exec", "help", "verbose", "quiet", "argv", "standalone",
858 "exclude=", "include=", "package=", "strip", "iconfile=",
Ronald Oussoren072bb402009-01-02 15:25:36 +0000859 "lib=", "python=", "semi-standalone", "bundle-id=", "destroot="
860 "no-zipimport"
861 )
Just van Rossumceeb9622002-11-21 23:19:37 +0000862
Jack Jansen0ae32202003-04-09 13:25:43 +0000863 try:
864 options, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
865 except getopt.error:
866 usage()
Just van Rossumceeb9622002-11-21 23:19:37 +0000867
Jack Jansen0ae32202003-04-09 13:25:43 +0000868 for opt, arg in options:
869 if opt in ('-b', '--builddir'):
870 builder.builddir = arg
871 elif opt in ('-n', '--name'):
872 builder.name = arg
873 elif opt in ('-r', '--resource'):
Just van Rossumdc31dc02003-06-20 21:43:36 +0000874 builder.resources.append(os.path.normpath(arg))
Jack Jansen0ae32202003-04-09 13:25:43 +0000875 elif opt in ('-f', '--file'):
876 srcdst = arg.split(':')
877 if len(srcdst) != 2:
878 usage("-f or --file argument must be two paths, "
879 "separated by a colon")
880 builder.files.append(srcdst)
881 elif opt in ('-e', '--executable'):
882 builder.executable = arg
883 elif opt in ('-m', '--mainprogram'):
884 builder.mainprogram = arg
885 elif opt in ('-a', '--argv'):
886 builder.argv_emulation = 1
887 elif opt in ('-c', '--creator'):
888 builder.creator = arg
Just van Rossumbe56aae2003-07-04 14:20:03 +0000889 elif opt == '--bundle-id':
890 builder.bundle_id = arg
Jack Jansen0ae32202003-04-09 13:25:43 +0000891 elif opt == '--iconfile':
892 builder.iconfile = arg
893 elif opt == "--lib":
Just van Rossumdc31dc02003-06-20 21:43:36 +0000894 builder.libs.append(os.path.normpath(arg))
Jack Jansen0ae32202003-04-09 13:25:43 +0000895 elif opt == "--nib":
896 builder.nibname = arg
897 elif opt in ('-p', '--plist'):
898 builder.plist = Plist.fromFile(arg)
899 elif opt in ('-l', '--link'):
900 builder.symlink = 1
901 elif opt == '--link-exec':
902 builder.symlink_exec = 1
903 elif opt in ('-h', '--help'):
904 usage()
905 elif opt in ('-v', '--verbose'):
906 builder.verbosity += 1
907 elif opt in ('-q', '--quiet'):
908 builder.verbosity -= 1
909 elif opt == '--standalone':
910 builder.standalone = 1
Just van Rossum762d2cc2003-06-29 21:54:12 +0000911 elif opt == '--semi-standalone':
912 builder.semi_standalone = 1
Jack Jansen8ba0e802003-05-25 22:00:17 +0000913 elif opt == '--python':
914 builder.python = arg
Jack Jansen0ae32202003-04-09 13:25:43 +0000915 elif opt in ('-x', '--exclude'):
916 builder.excludeModules.append(arg)
917 elif opt in ('-i', '--include'):
918 builder.includeModules.append(arg)
919 elif opt == '--package':
920 builder.includePackages.append(arg)
921 elif opt == '--strip':
922 builder.strip = 1
Jack Jansenc77f6df2004-12-27 15:51:03 +0000923 elif opt == '--destroot':
924 builder.destroot = arg
Ronald Oussoren072bb402009-01-02 15:25:36 +0000925 elif opt == '--no-zipimport':
926 builder.use_zipimport = False
Just van Rossumceeb9622002-11-21 23:19:37 +0000927
Jack Jansen0ae32202003-04-09 13:25:43 +0000928 if len(args) != 1:
929 usage("Must specify one command ('build', 'report' or 'help')")
930 command = args[0]
Just van Rossumceeb9622002-11-21 23:19:37 +0000931
Jack Jansen0ae32202003-04-09 13:25:43 +0000932 if command == "build":
933 builder.setup()
934 builder.build()
935 elif command == "report":
936 builder.setup()
937 builder.report()
938 elif command == "help":
939 usage()
940 else:
941 usage("Unknown command '%s'" % command)
Just van Rossumceeb9622002-11-21 23:19:37 +0000942
943
Just van Rossumad33d722002-11-21 10:23:04 +0000944def buildapp(**kwargs):
Jack Jansen0ae32202003-04-09 13:25:43 +0000945 builder = AppBuilder(**kwargs)
946 main(builder)
Just van Rossumad33d722002-11-21 10:23:04 +0000947
948
949if __name__ == "__main__":
Jack Jansen0ae32202003-04-09 13:25:43 +0000950 main()