Mike Klein | 6d3b7aa | 2017-01-30 14:00:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | # |
| 3 | # Copyright 2017 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
Mike Klein | 63afe64 | 2017-01-31 12:07:33 -0500 | [diff] [blame] | 8 | import glob |
Mike Klein | 6d3b7aa | 2017-01-30 14:00:54 -0500 | [diff] [blame] | 9 | import os |
| 10 | import re |
| 11 | import shutil |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import tempfile |
| 15 | |
| 16 | # Arguments to the script: |
| 17 | # app path to binary to package, e.g. out/Debug/dm |
Mike Klein | 63afe64 | 2017-01-31 12:07:33 -0500 | [diff] [blame] | 18 | app, = sys.argv[1:] |
| 19 | |
| 20 | # Find the Google signing identity. |
| 21 | identity = None |
| 22 | for line in subprocess.check_output(['security', 'find-identity']).split('\n'): |
| 23 | m = re.match(r'''.*\) (.*) ".*Google.*"''', line) |
| 24 | if m: |
| 25 | identity = m.group(1) |
| 26 | assert identity |
| 27 | |
| 28 | # Find the Google mobile provisioning profile. |
| 29 | mobileprovision = None |
| 30 | for p in glob.glob(os.path.join(os.environ['HOME'], 'Library', 'MobileDevice', |
| 31 | 'Provisioning Profiles', '*.mobileprovision')): |
| 32 | if re.search(r'''<key>Name</key> |
| 33 | \t<string>Google Development</string>''', open(p).read(), re.MULTILINE): |
| 34 | mobileprovision = p |
| 35 | assert mobileprovision |
Mike Klein | 6d3b7aa | 2017-01-30 14:00:54 -0500 | [diff] [blame] | 36 | |
| 37 | out, app = os.path.split(app) |
| 38 | |
| 39 | pkg = os.path.join(out, app + '.app') |
| 40 | if not os.path.exists(pkg): |
| 41 | os.mkdir(pkg) |
| 42 | |
| 43 | # The binary and .mobileprovision just get copied into the package. |
| 44 | shutil.copy(os.path.join(out, app), pkg) |
| 45 | shutil.copy(mobileprovision, |
| 46 | os.path.join(pkg, 'embedded.mobileprovision')) |
| 47 | |
| 48 | # Write a minimal Info.plist to name the package and point at the binary. |
| 49 | with open(os.path.join(pkg, 'Info.plist'), 'w') as f: |
| 50 | f.write(''' |
| 51 | <plist version="1.0"> |
| 52 | <dict> |
| 53 | <key>CFBundleExecutable</key> <string>{app}</string> |
| 54 | <key>CFBundleIdentifier</key> <string>com.google.{app}</string> |
| 55 | </dict> |
| 56 | </plist> |
| 57 | '''.format(app=app)) |
| 58 | |
| 59 | # Extract the appliciation identitifer prefix from the .mobileprovision. |
| 60 | m = re.search(r'''<key>ApplicationIdentifierPrefix</key> |
| 61 | \t<array> |
| 62 | \t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE) |
| 63 | prefix = m.group(1) |
| 64 | |
| 65 | # Write a minimal entitlements file, then codesign. |
| 66 | with tempfile.NamedTemporaryFile() as f: |
| 67 | f.write(''' |
| 68 | <plist version="1.0"> |
| 69 | <dict> |
| 70 | <key>application-identifier</key> <string>{prefix}.com.google.{app}</string> |
| 71 | <key>get-task-allow</key> <true/> |
| 72 | </dict> |
| 73 | </plist> |
| 74 | '''.format(prefix=prefix, app=app)) |
| 75 | f.flush() |
| 76 | |
| 77 | subprocess.check_call(['codesign', |
| 78 | '--force', |
| 79 | '--sign', identity, |
| 80 | '--entitlements', f.name, |
| 81 | '--timestamp=none', |
| 82 | pkg]) |