blob: 88d8b59493375d8c38523c9871a1792e51944bb5 [file] [log] [blame]
Jack Jansena6db44f2002-09-06 19:47:49 +00001#!/usr/bin/env python
2
3"""buildpkg.py -- Build OS X packages for Apple's Installer.app.
4
5This is an experimental command-line tool for building packages to be
6installed with the Mac OS X Installer.app application.
7
8It is much inspired by Apple's GUI tool called PackageMaker.app, that
9seems to be part of the OS X developer tools installed in the folder
10/Developer/Applications. But apparently there are other free tools to
11do the same thing which are also named PackageMaker like Brian Hill's
12one:
13
14 http://personalpages.tds.net/~brian_hill/packagemaker.html
15
16Beware of the multi-package features of Installer.app (which are not
17yet supported here) that can potentially screw-up your installation
18and are discussed in these articles on Stepwise:
19
20 http://www.stepwise.com/Articles/Technical/Packages/InstallerWoes.html
21 http://www.stepwise.com/Articles/Technical/Packages/InstallerOnX.html
22
23Beside using the PackageMaker class directly, by importing it inside
24another module, say, there are additional ways of using this module:
25the top-level buildPackage() function provides a shortcut to the same
26feature and is also called when using this module from the command-
27line.
28
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 ****************************************************************
36
37TODO:
38 - test pre-process and post-process scripts (Python ones?)
39 - handle multi-volume packages (?)
40 - integrate into distutils (?)
41
42Dinu C. Gherman,
43gherman@europemail.com
44November 2001
45
46!! USE AT YOUR OWN RISK !!
47"""
48
49__version__ = 0.2
50__license__ = "FreeBSD"
51
52
53import os, sys, glob, fnmatch, shutil, string, copy, getopt
54from os.path import basename, dirname, join, islink, isdir, isfile
55
Jack Jansen997429a2002-09-06 21:55:13 +000056Error = "buildpkg.Error"
Jack Jansena6db44f2002-09-06 19:47:49 +000057
58PKG_INFO_FIELDS = """\
59Title
60Version
61Description
62DefaultLocation
63Diskname
64DeleteWarning
65NeedsAuthorization
66DisableStop
67UseUserMask
68Application
69Relocatable
70Required
71InstallOnly
72RequiresReboot
73InstallFat\
74"""
75
76######################################################################
77# Helpers
78######################################################################
79
80# Convenience class, as suggested by /F.
81
82class GlobDirectoryWalker:
83 "A forward iterator that traverses files in a directory tree."
84
85 def __init__(self, directory, pattern="*"):
86 self.stack = [directory]
87 self.pattern = pattern
88 self.files = []
89 self.index = 0
90
91
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
109
110
111######################################################################
112# The real thing
113######################################################################
114
115class PackageMaker:
116 """A class to generate packages for Mac OS X.
117
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.
121
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.
128
129 E.g. this will create a package folder /my/space/distutils.pkg/:
130
131 pm = PackageMaker("distutils", "1.0.2", "Python distutils.")
132 pm.build("/my/space/distutils")
133 """
134
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'}
151
152
153 def __init__(self, title, version, desc):
154 "Init. with mandatory title/version/description arguments."
155
156 info = {"Title": title, "Version": version, "Description": desc}
157 self.packageInfo = copy.deepcopy(self.packageInfoDefaults)
158 self.packageInfo.update(info)
159
160 # variables set later
161 self.packageRootFolder = None
162 self.packageResourceFolder = None
Jack Jansen997429a2002-09-06 21:55:13 +0000163 self.sourceFolder = None
Jack Jansena6db44f2002-09-06 19:47:49 +0000164 self.resourceFolder = None
165
166
167 def build(self, root, resources=None, **options):
168 """Create a package for some given root folder.
169
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.
173 """
174
175 # set folder attributes
Jack Jansen997429a2002-09-06 21:55:13 +0000176 self.sourceFolder = root
Jack Jansena6db44f2002-09-06 19:47:49 +0000177 if resources == None:
Jack Jansen997429a2002-09-06 21:55:13 +0000178 self.resourceFolder = root
179 else:
180 self.resourceFolder = resources
Jack Jansena6db44f2002-09-06 19:47:49 +0000181
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
Jack Jansen997429a2002-09-06 21:55:13 +0000187 elif not k in ["OutputDir"]:
188 raise Error, "Unknown package option: %s" % k
189
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")
194
Jack Jansena6db44f2002-09-06 19:47:49 +0000195 # do what needs to be done
196 self._makeFolders()
197 self._addInfo()
198 self._addBom()
199 self._addArchive()
200 self._addResources()
201 self._addSizes()
202
203
204 def _makeFolders(self):
205 "Create package folder structure."
206
207 # Not sure if the package name should contain the version or not...
208 # packageName = "%s-%s" % (self.packageInfo["Title"],
209 # self.packageInfo["Version"]) # ??
210
Jack Jansen997429a2002-09-06 21:55:13 +0000211 contFolder = join(self.PackageRootFolder, "Contents")
212 self.packageResourceFolder = join(contFolder, "Resources")
213 os.mkdir(self.PackageRootFolder)
Jack Jansena6db44f2002-09-06 19:47:49 +0000214 os.mkdir(contFolder)
Jack Jansen997429a2002-09-06 21:55:13 +0000215 os.mkdir(self.packageResourceFolder)
Jack Jansena6db44f2002-09-06 19:47:49 +0000216
217 def _addInfo(self):
218 "Write .info file containing installing options."
219
220 # Not sure if options in PKG_INFO_FIELDS are complete...
221
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
Jack Jansen997429a2002-09-06 21:55:13 +0000226 base = self.packageInfo["Title"] + ".info"
227 path = join(self.packageResourceFolder, base)
Jack Jansena6db44f2002-09-06 19:47:49 +0000228 f = open(path, "w")
229 f.write(info)
230
231
232 def _addBom(self):
233 "Write .bom file containing 'Bill of Materials'."
234
235 # Currently ignores if the 'mkbom' tool is not available.
236
237 try:
Jack Jansen997429a2002-09-06 21:55:13 +0000238 base = self.packageInfo["Title"] + ".bom"
239 bomPath = join(self.packageResourceFolder, base)
240 cmd = "mkbom %s %s" % (self.sourceFolder, bomPath)
Jack Jansena6db44f2002-09-06 19:47:49 +0000241 res = os.system(cmd)
242 except:
243 pass
244
245
246 def _addArchive(self):
247 "Write .pax.gz file, a compressed archive using pax/gzip."
248
249 # Currently ignores if the 'pax' tool is not available.
250
251 cwd = os.getcwd()
252
Jack Jansen997429a2002-09-06 21:55:13 +0000253 # 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)
259
260 # compress archive
261 cmd = "gzip %s" % self.archPath
262 res = os.system(cmd)
Jack Jansena6db44f2002-09-06 19:47:49 +0000263 os.chdir(cwd)
264
265
266 def _addResources(self):
267 "Add Welcome/ReadMe/License files, .lproj folders and scripts."
268
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...
272
Jack Jansen997429a2002-09-06 21:55:13 +0000273 if not self.resourceFolder:
Jack Jansena6db44f2002-09-06 19:47:49 +0000274 return
275
276 # find candidate resource files (txt html rtf rtfd/ or lproj/)
277 allFiles = []
278 for pat in string.split("*.txt *.html *.rtf *.rtfd *.lproj", " "):
Jack Jansen997429a2002-09-06 21:55:13 +0000279 pattern = join(self.resourceFolder, pat)
Jack Jansena6db44f2002-09-06 19:47:49 +0000280 allFiles = allFiles + glob.glob(pattern)
281
282 # find pre-process and post-process scripts
283 # naming convention: packageName.{pre,post}-{upgrade,install}
Jack Jansen997429a2002-09-06 21:55:13 +0000284 # Alternatively the filenames can be {pre,post}-{upgrade,install}
285 # in which case we prepend the package name
Jack Jansena6db44f2002-09-06 19:47:49 +0000286 packageName = self.packageInfo["Title"]
287 for pat in ("*upgrade", "*install"):
Jack Jansen997429a2002-09-06 21:55:13 +0000288 pattern = join(self.resourceFolder, packageName + pat)
Jack Jansena6db44f2002-09-06 19:47:49 +0000289 allFiles = allFiles + glob.glob(pattern)
290
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:
Jack Jansen997429a2002-09-06 21:55:13 +0000296 files.append((f, f))
Jack Jansena6db44f2002-09-06 19:47:49 +0000297 if f[-6:] == ".lproj":
Jack Jansen997429a2002-09-06 21:55:13 +0000298 files.append((f, f))
299 elif f in ["pre-upgrade", "pre-install", "post-upgrade", "post-install"]:
300 files.append((f, self.packageInfo["Title"]+"."+f))
Jack Jansena6db44f2002-09-06 19:47:49 +0000301 elif f[-8:] == "-upgrade":
Jack Jansen997429a2002-09-06 21:55:13 +0000302 files.append((f,f))
Jack Jansena6db44f2002-09-06 19:47:49 +0000303 elif f[-8:] == "-install":
Jack Jansen997429a2002-09-06 21:55:13 +0000304 files.append((f,f))
Jack Jansena6db44f2002-09-06 19:47:49 +0000305
306 # copy files
Jack Jansen997429a2002-09-06 21:55:13 +0000307 for src, dst in files:
308 f = join(self.resourceFolder, src)
Jack Jansena6db44f2002-09-06 19:47:49 +0000309 if isfile(f):
Jack Jansen997429a2002-09-06 21:55:13 +0000310 shutil.copy(f, os.path.join(self.packageResourceFolder, dst))
Jack Jansena6db44f2002-09-06 19:47:49 +0000311 elif isdir(f):
312 # special case for .rtfd and .lproj folders...
Jack Jansen997429a2002-09-06 21:55:13 +0000313 d = join(self.packageResourceFolder, dst)
Jack Jansena6db44f2002-09-06 19:47:49 +0000314 os.mkdir(d)
315 files = GlobDirectoryWalker(f)
316 for file in files:
317 shutil.copy(file, d)
318
319
320 def _addSizes(self):
321 "Write .sizes file with info about number and size of files."
322
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...
327
328 numFiles = 0
329 installedSize = 0
330 zippedSize = 0
331
Jack Jansen997429a2002-09-06 21:55:13 +0000332 files = GlobDirectoryWalker(self.sourceFolder)
Jack Jansena6db44f2002-09-06 19:47:49 +0000333 for f in files:
334 numFiles = numFiles + 1
Jack Jansen997429a2002-09-06 21:55:13 +0000335 installedSize = installedSize + os.lstat(f)[6]
Jack Jansena6db44f2002-09-06 19:47:49 +0000336
Jack Jansena6db44f2002-09-06 19:47:49 +0000337 try:
Jack Jansen997429a2002-09-06 21:55:13 +0000338 zippedSize = os.stat(self.archPath+ ".gz")[6]
Jack Jansena6db44f2002-09-06 19:47:49 +0000339 except OSError: # ignore error
340 pass
Jack Jansen997429a2002-09-06 21:55:13 +0000341 base = self.packageInfo["Title"] + ".sizes"
342 f = open(join(self.packageResourceFolder, base), "w")
343 format = "NumFiles %d\nInstalledSize %d\nCompressedSize %d\n"
Jack Jansena6db44f2002-09-06 19:47:49 +0000344 f.write(format % (numFiles, installedSize, zippedSize))
345
346
347# Shortcut function interface
348
349def buildPackage(*args, **options):
350 "A Shortcut function for building a package."
351
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)
356
357
358######################################################################
359# Tests
360######################################################################
361
362def test0():
363 "Vanilla test for the distutils distribution."
364
365 pm = PackageMaker("distutils2", "1.0.2", "Python distutils package.")
366 pm.build("/Users/dinu/Desktop/distutils2")
367
368
369def test1():
370 "Test for the reportlab distribution with modified options."
371
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")
377
378def test2():
379 "Shortcut test for the reportlab distribution with modified options."
380
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")
388
389
390######################################################################
391# Command-line interface
392######################################################################
393
394def printUsage():
395 "Print usage message."
396
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)"
410
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]))
421
422
423def main():
424 "Command-line interface."
425
426 shortOpts = ""
427 keys = PackageMaker.packageInfoDefaults.keys()
428 longOpts = map(lambda k: k+"=", keys)
429
430 try:
431 opts, args = getopt.getopt(sys.argv[1:], shortOpts, longOpts)
432 except getopt.GetoptError, details:
433 print details
434 printUsage()
435 return
436
437 optsDict = {}
438 for k, v in opts:
439 optsDict[k[2:]] = v
440
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
451
452 printUsage()
453
454 # sample use:
455 # buildpkg.py --Title=distutils \
456 # --Version=1.0.2 \
457 # --Description="Python distutils package." \
458 # /Users/dinu/Desktop/distutils
459
460
461if __name__ == "__main__":
462 main()