/Mac/scripts/buildpkg.py

http://unladen-swallow.googlecode.com/ · Python · 484 lines · 394 code · 52 blank · 38 comment · 43 complexity · d124a224689fce11825055261a055080 MD5 · raw file

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