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 | |
| 8 | import os |
| 9 | import re |
| 10 | import shutil |
| 11 | import subprocess |
| 12 | import sys |
| 13 | import tempfile |
| 14 | |
| 15 | # Arguments to the script: |
| 16 | # app path to binary to package, e.g. out/Debug/dm |
| 17 | # identity code signing identity, a long hex string |
| 18 | # (run security find-identity -v -p codesigning | grep Google) |
| 19 | # mobileprovision path to Google_Development.mobileprovision |
| 20 | app, identity, mobileprovision = sys.argv[1:] |
| 21 | |
| 22 | out, app = os.path.split(app) |
| 23 | |
| 24 | pkg = os.path.join(out, app + '.app') |
| 25 | if not os.path.exists(pkg): |
| 26 | os.mkdir(pkg) |
| 27 | |
| 28 | # The binary and .mobileprovision just get copied into the package. |
| 29 | shutil.copy(os.path.join(out, app), pkg) |
| 30 | shutil.copy(mobileprovision, |
| 31 | os.path.join(pkg, 'embedded.mobileprovision')) |
| 32 | |
| 33 | # Write a minimal Info.plist to name the package and point at the binary. |
| 34 | with open(os.path.join(pkg, 'Info.plist'), 'w') as f: |
| 35 | f.write(''' |
| 36 | <plist version="1.0"> |
| 37 | <dict> |
| 38 | <key>CFBundleExecutable</key> <string>{app}</string> |
| 39 | <key>CFBundleIdentifier</key> <string>com.google.{app}</string> |
| 40 | </dict> |
| 41 | </plist> |
| 42 | '''.format(app=app)) |
| 43 | |
| 44 | # Extract the appliciation identitifer prefix from the .mobileprovision. |
| 45 | m = re.search(r'''<key>ApplicationIdentifierPrefix</key> |
| 46 | \t<array> |
| 47 | \t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE) |
| 48 | prefix = m.group(1) |
| 49 | |
| 50 | # Write a minimal entitlements file, then codesign. |
| 51 | with tempfile.NamedTemporaryFile() as f: |
| 52 | f.write(''' |
| 53 | <plist version="1.0"> |
| 54 | <dict> |
| 55 | <key>application-identifier</key> <string>{prefix}.com.google.{app}</string> |
| 56 | <key>get-task-allow</key> <true/> |
| 57 | </dict> |
| 58 | </plist> |
| 59 | '''.format(prefix=prefix, app=app)) |
| 60 | f.flush() |
| 61 | |
| 62 | subprocess.check_call(['codesign', |
| 63 | '--force', |
| 64 | '--sign', identity, |
| 65 | '--entitlements', f.name, |
| 66 | '--timestamp=none', |
| 67 | pkg]) |