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