Update for release.
[python/dscho.git] / Mac / scripts / buildpkg.py
blob88d8b59493375d8c38523c9871a1792e51944bb5
1 #!/usr/bin/env python
3 """buildpkg.py -- Build OS X packages for Apple's Installer.app.
5 This is an experimental command-line tool for building packages to be
6 installed with the Mac OS X Installer.app application.
8 It is much inspired by Apple's GUI tool called PackageMaker.app, that
9 seems to be part of the OS X developer tools installed in the folder
10 /Developer/Applications. But apparently there are other free tools to
11 do the same thing which are also named PackageMaker like Brian Hill's
12 one:
14 http://personalpages.tds.net/~brian_hill/packagemaker.html
16 Beware of the multi-package features of Installer.app (which are not
17 yet supported here) that can potentially screw-up your installation
18 and are discussed in these articles on Stepwise:
20 http://www.stepwise.com/Articles/Technical/Packages/InstallerWoes.html
21 http://www.stepwise.com/Articles/Technical/Packages/InstallerOnX.html
23 Beside using the PackageMaker class directly, by importing it inside
24 another module, say, there are additional ways of using this module:
25 the top-level buildPackage() function provides a shortcut to the same
26 feature and is also called when using this module from the command-
27 line.
29 ****************************************************************
30 NOTE: For now you should be able to run this even on a non-OS X
31 system and get something similar to a package, but without
32 the real archive (needs pax) and bom files (needs mkbom)
33 inside! This is only for providing a chance for testing to
34 folks without OS X.
35 ****************************************************************
37 TODO:
38 - test pre-process and post-process scripts (Python ones?)
39 - handle multi-volume packages (?)
40 - integrate into distutils (?)
42 Dinu C. Gherman,
43 gherman@europemail.com
44 November 2001
46 !! USE AT YOUR OWN RISK !!
47 """
49 __version__ = 0.2
50 __license__ = "FreeBSD"
53 import os, sys, glob, fnmatch, shutil, string, copy, getopt
54 from os.path import basename, dirname, join, islink, isdir, isfile
56 Error = "buildpkg.Error"
58 PKG_INFO_FIELDS = """\
59 Title
60 Version
61 Description
62 DefaultLocation
63 Diskname
64 DeleteWarning
65 NeedsAuthorization
66 DisableStop
67 UseUserMask
68 Application
69 Relocatable
70 Required
71 InstallOnly
72 RequiresReboot
73 InstallFat\
74 """
76 ######################################################################
77 # Helpers
78 ######################################################################
80 # Convenience class, as suggested by /F.
82 class GlobDirectoryWalker:
83 "A forward iterator that traverses files in a directory tree."
85 def __init__(self, directory, pattern="*"):
86 self.stack = [directory]
87 self.pattern = pattern
88 self.files = []
89 self.index = 0
92 def __getitem__(self, index):
93 while 1:
94 try:
95 file = self.files[self.index]
96 self.index = self.index + 1
97 except IndexError:
98 # pop next directory from stack
99 self.directory = self.stack.pop()
100 self.files = os.listdir(self.directory)
101 self.index = 0
102 else:
103 # got a filename
104 fullname = join(self.directory, file)
105 if isdir(fullname) and not islink(fullname):
106 self.stack.append(fullname)
107 if fnmatch.fnmatch(file, self.pattern):
108 return fullname
111 ######################################################################
112 # The real thing
113 ######################################################################
115 class PackageMaker:
116 """A class to generate packages for Mac OS X.
118 This is intended to create OS X packages (with extension .pkg)
119 containing archives of arbitrary files that the Installer.app
120 will be able to handle.
122 As of now, PackageMaker instances need to be created with the
123 title, version and description of the package to be built.
124 The package is built after calling the instance method
125 build(root, **options). It has the same name as the constructor's
126 title argument plus a '.pkg' extension and is located in the same
127 parent folder that contains the root folder.
129 E.g. this will create a package folder /my/space/distutils.pkg/:
131 pm = PackageMaker("distutils", "1.0.2", "Python distutils.")
132 pm.build("/my/space/distutils")
135 packageInfoDefaults = {
136 'Title': None,
137 'Version': None,
138 'Description': '',
139 'DefaultLocation': '/',
140 'Diskname': '(null)',
141 'DeleteWarning': '',
142 'NeedsAuthorization': 'NO',
143 'DisableStop': 'NO',
144 'UseUserMask': 'YES',
145 'Application': 'NO',
146 'Relocatable': 'YES',
147 'Required': 'NO',
148 'InstallOnly': 'NO',
149 'RequiresReboot': 'NO',
150 'InstallFat': 'NO'}
153 def __init__(self, title, version, desc):
154 "Init. with mandatory title/version/description arguments."
156 info = {"Title": title, "Version": version, "Description": desc}
157 self.packageInfo = copy.deepcopy(self.packageInfoDefaults)
158 self.packageInfo.update(info)
160 # variables set later
161 self.packageRootFolder = None
162 self.packageResourceFolder = None
163 self.sourceFolder = None
164 self.resourceFolder = None
167 def build(self, root, resources=None, **options):
168 """Create a package for some given root folder.
170 With no 'resources' argument set it is assumed to be the same
171 as the root directory. Option items replace the default ones
172 in the package info.
175 # set folder attributes
176 self.sourceFolder = root
177 if resources == None:
178 self.resourceFolder = root
179 else:
180 self.resourceFolder = resources
182 # replace default option settings with user ones if provided
183 fields = self. packageInfoDefaults.keys()
184 for k, v in options.items():
185 if k in fields:
186 self.packageInfo[k] = v
187 elif not k in ["OutputDir"]:
188 raise Error, "Unknown package option: %s" % k
190 # Check where we should leave the output. Default is current directory
191 outputdir = options.get("OutputDir", os.getcwd())
192 packageName = self.packageInfo["Title"]
193 self.PackageRootFolder = os.path.join(outputdir, packageName + ".pkg")
195 # do what needs to be done
196 self._makeFolders()
197 self._addInfo()
198 self._addBom()
199 self._addArchive()
200 self._addResources()
201 self._addSizes()
204 def _makeFolders(self):
205 "Create package folder structure."
207 # Not sure if the package name should contain the version or not...
208 # packageName = "%s-%s" % (self.packageInfo["Title"],
209 # self.packageInfo["Version"]) # ??
211 contFolder = join(self.PackageRootFolder, "Contents")
212 self.packageResourceFolder = join(contFolder, "Resources")
213 os.mkdir(self.PackageRootFolder)
214 os.mkdir(contFolder)
215 os.mkdir(self.packageResourceFolder)
217 def _addInfo(self):
218 "Write .info file containing installing options."
220 # Not sure if options in PKG_INFO_FIELDS are complete...
222 info = ""
223 for f in string.split(PKG_INFO_FIELDS, "\n"):
224 info = info + "%s %%(%s)s\n" % (f, f)
225 info = info % self.packageInfo
226 base = self.packageInfo["Title"] + ".info"
227 path = join(self.packageResourceFolder, base)
228 f = open(path, "w")
229 f.write(info)
232 def _addBom(self):
233 "Write .bom file containing 'Bill of Materials'."
235 # Currently ignores if the 'mkbom' tool is not available.
237 try:
238 base = self.packageInfo["Title"] + ".bom"
239 bomPath = join(self.packageResourceFolder, base)
240 cmd = "mkbom %s %s" % (self.sourceFolder, bomPath)
241 res = os.system(cmd)
242 except:
243 pass
246 def _addArchive(self):
247 "Write .pax.gz file, a compressed archive using pax/gzip."
249 # Currently ignores if the 'pax' tool is not available.
251 cwd = os.getcwd()
253 # create archive
254 os.chdir(self.sourceFolder)
255 base = basename(self.packageInfo["Title"]) + ".pax"
256 self.archPath = join(self.packageResourceFolder, base)
257 cmd = "pax -w -f %s %s" % (self.archPath, ".")
258 res = os.system(cmd)
260 # compress archive
261 cmd = "gzip %s" % self.archPath
262 res = os.system(cmd)
263 os.chdir(cwd)
266 def _addResources(self):
267 "Add Welcome/ReadMe/License files, .lproj folders and scripts."
269 # Currently we just copy everything that matches the allowed
270 # filenames. So, it's left to Installer.app to deal with the
271 # same file available in multiple formats...
273 if not self.resourceFolder:
274 return
276 # find candidate resource files (txt html rtf rtfd/ or lproj/)
277 allFiles = []
278 for pat in string.split("*.txt *.html *.rtf *.rtfd *.lproj", " "):
279 pattern = join(self.resourceFolder, pat)
280 allFiles = allFiles + glob.glob(pattern)
282 # find pre-process and post-process scripts
283 # naming convention: packageName.{pre,post}-{upgrade,install}
284 # Alternatively the filenames can be {pre,post}-{upgrade,install}
285 # in which case we prepend the package name
286 packageName = self.packageInfo["Title"]
287 for pat in ("*upgrade", "*install"):
288 pattern = join(self.resourceFolder, packageName + pat)
289 allFiles = allFiles + glob.glob(pattern)
291 # check name patterns
292 files = []
293 for f in allFiles:
294 for s in ("Welcome", "License", "ReadMe"):
295 if string.find(basename(f), s) == 0:
296 files.append((f, f))
297 if f[-6:] == ".lproj":
298 files.append((f, f))
299 elif f in ["pre-upgrade", "pre-install", "post-upgrade", "post-install"]:
300 files.append((f, self.packageInfo["Title"]+"."+f))
301 elif f[-8:] == "-upgrade":
302 files.append((f,f))
303 elif f[-8:] == "-install":
304 files.append((f,f))
306 # copy files
307 for src, dst in files:
308 f = join(self.resourceFolder, src)
309 if isfile(f):
310 shutil.copy(f, os.path.join(self.packageResourceFolder, dst))
311 elif isdir(f):
312 # special case for .rtfd and .lproj folders...
313 d = join(self.packageResourceFolder, dst)
314 os.mkdir(d)
315 files = GlobDirectoryWalker(f)
316 for file in files:
317 shutil.copy(file, d)
320 def _addSizes(self):
321 "Write .sizes file with info about number and size of files."
323 # Not sure if this is correct, but 'installedSize' and
324 # 'zippedSize' are now in Bytes. Maybe blocks are needed?
325 # Well, Installer.app doesn't seem to care anyway, saying
326 # the installation needs 100+ MB...
328 numFiles = 0
329 installedSize = 0
330 zippedSize = 0
332 files = GlobDirectoryWalker(self.sourceFolder)
333 for f in files:
334 numFiles = numFiles + 1
335 installedSize = installedSize + os.lstat(f)[6]
337 try:
338 zippedSize = os.stat(self.archPath+ ".gz")[6]
339 except OSError: # ignore error
340 pass
341 base = self.packageInfo["Title"] + ".sizes"
342 f = open(join(self.packageResourceFolder, base), "w")
343 format = "NumFiles %d\nInstalledSize %d\nCompressedSize %d\n"
344 f.write(format % (numFiles, installedSize, zippedSize))
347 # Shortcut function interface
349 def buildPackage(*args, **options):
350 "A Shortcut function for building a package."
352 o = options
353 title, version, desc = o["Title"], o["Version"], o["Description"]
354 pm = PackageMaker(title, version, desc)
355 apply(pm.build, list(args), options)
358 ######################################################################
359 # Tests
360 ######################################################################
362 def test0():
363 "Vanilla test for the distutils distribution."
365 pm = PackageMaker("distutils2", "1.0.2", "Python distutils package.")
366 pm.build("/Users/dinu/Desktop/distutils2")
369 def test1():
370 "Test for the reportlab distribution with modified options."
372 pm = PackageMaker("reportlab", "1.10",
373 "ReportLab's Open Source PDF toolkit.")
374 pm.build(root="/Users/dinu/Desktop/reportlab",
375 DefaultLocation="/Applications/ReportLab",
376 Relocatable="YES")
378 def test2():
379 "Shortcut test for the reportlab distribution with modified options."
381 buildPackage(
382 "/Users/dinu/Desktop/reportlab",
383 Title="reportlab",
384 Version="1.10",
385 Description="ReportLab's Open Source PDF toolkit.",
386 DefaultLocation="/Applications/ReportLab",
387 Relocatable="YES")
390 ######################################################################
391 # Command-line interface
392 ######################################################################
394 def printUsage():
395 "Print usage message."
397 format = "Usage: %s <opts1> [<opts2>] <root> [<resources>]"
398 print format % basename(sys.argv[0])
399 print
400 print " with arguments:"
401 print " (mandatory) root: the package root folder"
402 print " (optional) resources: the package resources folder"
403 print
404 print " and options:"
405 print " (mandatory) opts1:"
406 mandatoryKeys = string.split("Title Version Description", " ")
407 for k in mandatoryKeys:
408 print " --%s" % k
409 print " (optional) opts2: (with default values)"
411 pmDefaults = PackageMaker.packageInfoDefaults
412 optionalKeys = pmDefaults.keys()
413 for k in mandatoryKeys:
414 optionalKeys.remove(k)
415 optionalKeys.sort()
416 maxKeyLen = max(map(len, optionalKeys))
417 for k in optionalKeys:
418 format = " --%%s:%s %%s"
419 format = format % (" " * (maxKeyLen-len(k)))
420 print format % (k, repr(pmDefaults[k]))
423 def main():
424 "Command-line interface."
426 shortOpts = ""
427 keys = PackageMaker.packageInfoDefaults.keys()
428 longOpts = map(lambda k: k+"=", keys)
430 try:
431 opts, args = getopt.getopt(sys.argv[1:], shortOpts, longOpts)
432 except getopt.GetoptError, details:
433 print details
434 printUsage()
435 return
437 optsDict = {}
438 for k, v in opts:
439 optsDict[k[2:]] = v
441 ok = optsDict.keys()
442 if not (1 <= len(args) <= 2):
443 print "No argument given!"
444 elif not ("Title" in ok and \
445 "Version" in ok and \
446 "Description" in ok):
447 print "Missing mandatory option!"
448 else:
449 apply(buildPackage, args, optsDict)
450 return
452 printUsage()
454 # sample use:
455 # buildpkg.py --Title=distutils \
456 # --Version=1.0.2 \
457 # --Description="Python distutils package." \
458 # /Users/dinu/Desktop/distutils
461 if __name__ == "__main__":
462 main()