Alexander Afanasyev | 46c7f84 | 2013-11-07 23:28:10 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # |
| 5 | # Loosely based on original bash-version by Sebastian Schlingmann (based, again, on a OSX application bundler |
| 6 | # by Thomas Keller). |
| 7 | # |
| 8 | |
| 9 | import sys, os, string, re, shutil, plistlib, tempfile, exceptions, datetime, tarfile |
| 10 | from subprocess import Popen, PIPE |
| 11 | from optparse import OptionParser |
| 12 | |
| 13 | import platform |
| 14 | |
| 15 | if platform.system () != 'Darwin': |
| 16 | print "This script is indended to be run only on OSX platform" |
| 17 | exit (1) |
| 18 | |
| 19 | SUPPORTED_VERSION = "10.9" |
| 20 | BINARY_POSTFIX = "Mavericks-10.9" |
| 21 | |
| 22 | if '.'.join (platform.mac_ver()[0].split('.')[0:2]) != SUPPORTED_VERSION: |
| 23 | print "This script is indended to be run only on OSX %s platform" % SUPPORTED_VERSION |
| 24 | exit (1) |
| 25 | |
| 26 | options = None |
| 27 | |
| 28 | def gitrev(): |
| 29 | return os.popen('git describe').read()[:-1] |
| 30 | |
| 31 | def codesign(path): |
| 32 | '''Call the codesign executable.''' |
| 33 | |
| 34 | if hasattr(path, 'isalpha'): |
| 35 | path = (path,) |
| 36 | |
| 37 | for p in path: |
| 38 | p = Popen(('codesign', '-vvvv', '--deep', '--force', '--sign', options.codesign, p)) |
| 39 | retval = p.wait() |
| 40 | if retval != 0: |
| 41 | return retval |
| 42 | return 0 |
| 43 | |
| 44 | class AppBundle(object): |
| 45 | |
| 46 | def __init__(self, bundle, version, binary): |
| 47 | shutil.copytree (src = binary, dst = bundle, symlinks = True) |
| 48 | |
| 49 | self.framework_path = '' |
| 50 | self.handled_libs = {} |
| 51 | self.bundle = bundle |
| 52 | self.version = version |
| 53 | self.infopath = os.path.join(os.path.abspath(bundle), 'Contents', 'Info.plist') |
| 54 | self.infoplist = plistlib.readPlist(self.infopath) |
| 55 | self.binary = os.path.join(os.path.abspath(bundle), 'Contents', 'MacOS', self.infoplist['CFBundleExecutable']) |
| 56 | print ' * Preparing AppBundle' |
| 57 | |
| 58 | def is_system_lib(self, lib): |
| 59 | ''' |
| 60 | Is the library a system library, meaning that we should not include it in our bundle? |
| 61 | ''' |
| 62 | if lib.startswith('/System/Library/'): |
| 63 | return True |
| 64 | if lib.startswith('/usr/lib/'): |
| 65 | return True |
| 66 | if lib.startswith('/usr/local/ndn/lib/'): |
| 67 | return True |
| 68 | |
| 69 | return False |
| 70 | |
| 71 | def is_dylib(self, lib): |
| 72 | ''' |
| 73 | Is the library a dylib? |
| 74 | ''' |
| 75 | return lib.endswith('.dylib') |
| 76 | |
| 77 | def get_framework_base(self, fw): |
| 78 | ''' |
| 79 | Extracts the base .framework bundle path from a library in an abitrary place in a framework. |
| 80 | ''' |
| 81 | paths = fw.split('/') |
| 82 | for i, str in enumerate(paths): |
| 83 | if str.endswith('.framework'): |
| 84 | return '/'.join(paths[:i+1]) |
| 85 | return None |
| 86 | |
| 87 | def is_framework(self, lib): |
| 88 | ''' |
| 89 | Is the library a framework? |
| 90 | ''' |
| 91 | return bool(self.get_framework_base(lib)) |
| 92 | |
| 93 | def get_binary_libs(self, path): |
| 94 | ''' |
| 95 | Get a list of libraries that we depend on. |
| 96 | ''' |
| 97 | m = re.compile('^\t(.*)\ \(.*$') |
| 98 | libs = Popen(['otool', '-L', path], stdout=PIPE).communicate()[0] |
| 99 | libs = string.split(libs, '\n') |
| 100 | ret = [] |
| 101 | bn = os.path.basename(path) |
| 102 | for line in libs: |
| 103 | g = m.match(line) |
| 104 | if g is not None: |
| 105 | lib = g.groups()[0] |
| 106 | if lib != bn: |
| 107 | ret.append(lib) |
| 108 | return ret |
| 109 | |
| 110 | def handle_libs(self): |
| 111 | ''' |
| 112 | Copy non-system libraries that we depend on into our bundle, and fix linker |
| 113 | paths so they are relative to our bundle. |
| 114 | ''' |
| 115 | print ' * Taking care of libraries' |
| 116 | |
| 117 | # Does our fwpath exist? |
| 118 | fwpath = os.path.join(os.path.abspath(self.bundle), 'Contents', 'Frameworks') |
| 119 | if not os.path.exists(fwpath): |
| 120 | os.mkdir(fwpath) |
| 121 | |
| 122 | self.handle_binary_libs() |
| 123 | |
| 124 | #actd = os.path.join(os.path.abspath(self.bundle), 'Contents', 'MacOS', 'actd') |
| 125 | #if os.path.exists(actd): |
| 126 | # self.handle_binary_libs(actd) |
| 127 | |
| 128 | def handle_binary_libs(self, macho=None): |
| 129 | ''' |
| 130 | Fix up dylib depends for a specific binary. |
| 131 | ''' |
| 132 | print "macho is ", macho |
| 133 | # Does our fwpath exist already? If not, create it. |
| 134 | if not self.framework_path: |
| 135 | self.framework_path = self.bundle + '/Contents/Frameworks' |
| 136 | if not os.path.exists(self.framework_path): |
| 137 | os.mkdir(self.framework_path) |
| 138 | else: |
| 139 | shutil.rmtree(self.framework_path) |
| 140 | os.mkdir(self.framework_path) |
| 141 | |
| 142 | # If we weren't explicitly told which binary to operate on, pick the |
| 143 | # bundle's default executable from its property list. |
| 144 | if macho is None: |
| 145 | macho = os.path.abspath(self.binary) |
| 146 | else: |
| 147 | macho = os.path.abspath(macho) |
| 148 | |
| 149 | libs = self.get_binary_libs(macho) |
| 150 | |
| 151 | for lib in libs: |
| 152 | |
| 153 | # Skip system libraries |
| 154 | if self.is_system_lib(lib): |
| 155 | continue |
| 156 | |
| 157 | # Frameworks are 'special'. |
| 158 | if self.is_framework(lib): |
| 159 | fw_path = self.get_framework_base(lib) |
| 160 | basename = os.path.basename(fw_path) |
| 161 | name = basename.split('.framework')[0] |
| 162 | rel = basename + '/' + name |
| 163 | |
| 164 | abs = self.framework_path + '/' + rel |
| 165 | |
| 166 | if not basename in self.handled_libs: |
| 167 | dst = self.framework_path + '/' + basename |
| 168 | shutil.copytree(fw_path, dst, symlinks=True) |
| 169 | if name.startswith('Qt'): |
| 170 | try: |
| 171 | os.remove(dst + '/Headers') |
| 172 | os.remove(dst + '/' + name + '.prl') |
| 173 | os.remove(dst + '/' + name + '_debug') |
| 174 | os.remove(dst + '/' + name + '_debug.prl') |
| 175 | shutil.rmtree(dst + '/Versions/4/Headers') |
| 176 | os.remove(dst + '/Versions/4/' + name + '_debug') |
| 177 | except OSError: |
| 178 | pass |
| 179 | os.chmod(abs, 0755) |
| 180 | os.system('install_name_tool -id @executable_path/../Frameworks/%s %s' % (rel, abs)) |
| 181 | self.handled_libs[basename] = True |
| 182 | self.handle_binary_libs(abs) |
| 183 | |
| 184 | try: |
| 185 | f=open("%s/Resources/Info.plist" % dst, 'w') |
Alexander Afanasyev | 503917a | 2013-11-08 11:17:55 -0800 | [diff] [blame] | 186 | f.write('''<?xml version="1.0" encoding="UTF-8"?> |
Alexander Afanasyev | 46c7f84 | 2013-11-07 23:28:10 -0800 | [diff] [blame] | 187 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 188 | <plist version="1.0"> |
| 189 | <dict> |
| 190 | <key>CFBundleSignature</key> |
| 191 | <string>????</string> |
| 192 | </dict> |
| 193 | </plist>''') |
| 194 | except: |
| 195 | pass |
| 196 | |
| 197 | os.chmod(macho, 0755) |
| 198 | os.system('install_name_tool -change %s @executable_path/../Frameworks/%s %s' % (lib, rel, macho)) |
| 199 | |
| 200 | # Regular dylibs |
| 201 | else: |
| 202 | basename = os.path.basename(lib) |
| 203 | rel = basename |
| 204 | |
| 205 | if not basename in self.handled_libs: |
| 206 | print lib |
| 207 | print self.framework_path + '/' + basename |
| 208 | try: |
| 209 | shutil.copy(lib, self.framework_path + '/' + basename) |
| 210 | except IOError: |
| 211 | print "IOError!" + self.framework_path + '/' + basename + "does not exist\n" |
| 212 | continue |
| 213 | |
| 214 | abs = self.framework_path + '/' + rel |
| 215 | os.chmod(abs, 0755) |
| 216 | os.system('install_name_tool -id @executable_path/../Frameworks/%s %s' % (rel, abs)) |
| 217 | self.handled_libs[basename] = True |
| 218 | self.handle_binary_libs(abs) |
| 219 | os.chmod(macho, 0755) |
| 220 | os.system('install_name_tool -change %s @executable_path/../Frameworks/%s %s' % (lib, rel, macho)) |
| 221 | |
| 222 | def copy_resources(self, rsrcs): |
| 223 | ''' |
| 224 | Copy needed resources into our bundle. |
| 225 | ''' |
| 226 | print ' * Copying needed resources' |
| 227 | rsrcpath = os.path.join(self.bundle, 'Contents', 'Resources') |
| 228 | if not os.path.exists(rsrcpath): |
| 229 | os.mkdir(rsrcpath) |
| 230 | |
| 231 | # Copy resources already in the bundle |
| 232 | for rsrc in rsrcs: |
| 233 | b = os.path.basename(rsrc) |
| 234 | if os.path.isdir(rsrc): |
| 235 | shutil.copytree(rsrc, os.path.join(rsrcpath, b), symlinks=True) |
| 236 | elif os.path.isfile(rsrc): |
| 237 | shutil.copy(rsrc, os.path.join(rsrcpath, b)) |
| 238 | |
| 239 | return |
| 240 | |
| 241 | def copy_qt_plugins(self): |
| 242 | ''' |
| 243 | Copy over any needed Qt plugins. |
| 244 | ''' |
| 245 | |
| 246 | print ' * Copying Qt and preparing plugins' |
| 247 | |
| 248 | src = os.popen('qmake -query QT_INSTALL_PLUGINS').read().strip() |
| 249 | dst = os.path.join(self.bundle, 'Contents', 'QtPlugins') |
| 250 | shutil.copytree(src, dst, symlinks=False) |
| 251 | |
| 252 | top = dst |
| 253 | files = {} |
| 254 | |
| 255 | def cb(arg, dirname, fnames): |
| 256 | if dirname == top: |
| 257 | return |
| 258 | files[os.path.basename(dirname)] = fnames |
| 259 | |
| 260 | os.path.walk(top, cb, None) |
| 261 | |
| 262 | exclude = ( 'phonon_backend', 'designer', 'script' ) |
| 263 | |
| 264 | for dir, files in files.items(): |
| 265 | absdir = dst + '/' + dir |
| 266 | if dir in exclude: |
| 267 | shutil.rmtree(absdir) |
| 268 | continue |
| 269 | for file in files: |
| 270 | abs = absdir + '/' + file |
| 271 | if file.endswith('_debug.dylib'): |
| 272 | os.remove(abs) |
| 273 | else: |
| 274 | os.system('install_name_tool -id %s %s' % (file, abs)) |
| 275 | self.handle_binary_libs(abs) |
| 276 | |
| 277 | def set_min_macosx_version(self, version): |
| 278 | ''' |
| 279 | Set the minimum version of Mac OS X version that this App will run on. |
| 280 | ''' |
| 281 | print ' * Setting minimum Mac OS X version to: %s' % (version) |
| 282 | self.infoplist['LSMinimumSystemVersion'] = version |
| 283 | |
| 284 | def done(self): |
| 285 | plistlib.writePlist(self.infoplist, self.infopath) |
| 286 | print ' * Done!' |
| 287 | print '' |
| 288 | |
| 289 | class FolderObject(object): |
| 290 | class Exception(exceptions.Exception): |
| 291 | pass |
| 292 | |
| 293 | def __init__(self): |
| 294 | self.tmp = tempfile.mkdtemp() |
| 295 | |
| 296 | def copy(self, src, dst='/'): |
| 297 | ''' |
| 298 | Copy a file or directory into foler |
| 299 | ''' |
| 300 | asrc = os.path.abspath(src) |
| 301 | |
| 302 | if dst[0] != '/': |
| 303 | raise self.Exception |
| 304 | |
| 305 | # Determine destination |
| 306 | if dst[-1] == '/': |
| 307 | adst = os.path.abspath(self.tmp + '/' + dst + os.path.basename(src)) |
| 308 | else: |
| 309 | adst = os.path.abspath(self.tmp + '/' + dst) |
| 310 | |
| 311 | if os.path.isdir(asrc): |
| 312 | print ' * Copying directory: %s' % os.path.basename(asrc) |
| 313 | shutil.copytree(asrc, adst, symlinks=True) |
| 314 | elif os.path.isfile(asrc): |
| 315 | print ' * Copying file: %s' % os.path.basename(asrc) |
| 316 | shutil.copy(asrc, adst) |
| 317 | |
| 318 | def symlink(self, src, dst): |
| 319 | ''' |
| 320 | Create a symlink inside the folder |
| 321 | ''' |
| 322 | asrc = os.path.abspath(src) |
| 323 | adst = self.tmp + '/' + dst |
| 324 | print " * Creating symlink %s" % os.path.basename(asrc) |
| 325 | os.symlink(asrc, adst) |
| 326 | |
| 327 | def mkdir(self, name): |
| 328 | ''' |
| 329 | Create a directory inside the folder. |
| 330 | ''' |
| 331 | print ' * Creating directory %s' % os.path.basename(name) |
| 332 | adst = self.tmp + '/' + name |
| 333 | os.makedirs(adst) |
| 334 | |
| 335 | class DiskImage(FolderObject): |
| 336 | |
| 337 | def __init__(self, filename, volname): |
| 338 | FolderObject.__init__(self) |
| 339 | print ' * Preparing to create diskimage' |
| 340 | self.filename = filename |
| 341 | self.volname = volname |
| 342 | |
| 343 | def create(self): |
| 344 | ''' |
| 345 | Create the disk image |
| 346 | ''' |
| 347 | print ' * Creating disk image. Please wait...' |
| 348 | if os.path.exists(self.filename): |
| 349 | shutil.rmtree(self.filename) |
| 350 | p = Popen(['hdiutil', 'create', |
| 351 | '-srcfolder', self.tmp, |
| 352 | '-format', 'UDBZ', |
| 353 | '-volname', self.volname, |
| 354 | self.filename]) |
| 355 | |
| 356 | retval = p.wait() |
| 357 | print ' * Removing temporary directory.' |
| 358 | shutil.rmtree(self.tmp) |
| 359 | print ' * Done!' |
| 360 | |
| 361 | |
| 362 | if __name__ == '__main__': |
| 363 | parser = OptionParser() |
| 364 | parser.add_option('-r', '--release', dest='release', help='Build a release. This determines the version number of the release.') |
| 365 | parser.add_option('-s', '--snapshot', dest='snapshot', help='Build a snapshot release. This determines the \'snapshot version\'.') |
| 366 | parser.add_option('-g', '--git', dest='git', help='Build a snapshot release. Use the git revision number as the \'snapshot version\'.', action='store_true', default=False) |
| 367 | parser.add_option('--codesign', dest='codesign', help='Identity to use for code signing. (If not set, no code signing will occur)') |
| 368 | # parser.add_option('--codesign-keychain', dest='codesign_keychain', help='The keychain to use when invoking the codesign utility.') |
| 369 | |
| 370 | options, args = parser.parse_args() |
| 371 | |
| 372 | # Release |
| 373 | if options.release: |
| 374 | ver = options.release |
| 375 | # Snapshot |
| 376 | elif options.snapshot or options.git: |
| 377 | if not options.git: |
| 378 | ver = options.snapshot |
| 379 | else: |
| 380 | ver = gitrev() |
| 381 | else: |
| 382 | print 'ERROR: Neither snapshot or release selected. Bailing.' |
| 383 | parser.print_help () |
| 384 | sys.exit(1) |
| 385 | |
| 386 | |
| 387 | # Do the finishing touches to our Application bundle before release |
| 388 | a = AppBundle('build/%s/ChronoChat.app' % (BINARY_POSTFIX), ver, 'build/ChronoChat.app') |
| 389 | a.copy_qt_plugins() |
| 390 | a.handle_libs() |
| 391 | a.copy_resources(['qt.conf']) |
| 392 | a.set_min_macosx_version('%s.0' % SUPPORTED_VERSION) |
| 393 | a.done() |
| 394 | |
| 395 | # Sign our binaries, etc. |
| 396 | if options.codesign: |
| 397 | print ' * Signing binaries with identity `%s\'' % options.codesign |
| 398 | binaries = ( |
| 399 | 'build/%s/ChronoChat.app' % (BINARY_POSTFIX), |
| 400 | ) |
| 401 | |
| 402 | codesign(binaries) |
| 403 | print '' |
| 404 | |
| 405 | # Create diskimage |
| 406 | title = "ChronoChat-%s-%s" % (ver, BINARY_POSTFIX) |
| 407 | fn = "build/%s.dmg" % title |
| 408 | d = DiskImage(fn, title) |
| 409 | d.symlink('/Applications', '/Applications') |
| 410 | d.copy('build/%s/ChronoChat.app' % BINARY_POSTFIX, '/ChronoChat.app') |
| 411 | d.create() |
| 412 | |