blob: eb07fc703ba3a789ed32b7983131aaa2c4ed846a [file] [log] [blame]
Mike Klein6d3b7aa2017-01-30 14:00:54 -05001#!/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 Klein63afe642017-01-31 12:07:33 -05008import glob
Mike Klein6d3b7aa2017-01-30 14:00:54 -05009import os
10import re
11import shutil
12import subprocess
13import sys
14import tempfile
15
16# Arguments to the script:
17# app path to binary to package, e.g. out/Debug/dm
Mike Klein63afe642017-01-31 12:07:33 -050018app, = sys.argv[1:]
19
20# Find the Google signing identity.
21identity = None
22for 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)
26assert identity
27
28# Find the Google mobile provisioning profile.
29mobileprovision = None
30for 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
35assert mobileprovision
Mike Klein6d3b7aa2017-01-30 14:00:54 -050036
37out, app = os.path.split(app)
38
39pkg = os.path.join(out, app + '.app')
40if not os.path.exists(pkg):
41 os.mkdir(pkg)
42
43# The binary and .mobileprovision just get copied into the package.
44shutil.copy(os.path.join(out, app), pkg)
45shutil.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.
49with 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>
Jim Van Verthdbb24ef2017-09-01 11:01:51 -040055 <key>CFBundlePackageType</key> <string>APPL</string>
Mike Klein6d3b7aa2017-01-30 14:00:54 -050056 </dict>
57</plist>
58'''.format(app=app))
59
60# Extract the appliciation identitifer prefix from the .mobileprovision.
61m = re.search(r'''<key>ApplicationIdentifierPrefix</key>
62\t<array>
63\t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE)
64prefix = m.group(1)
65
66# Write a minimal entitlements file, then codesign.
67with tempfile.NamedTemporaryFile() as f:
68 f.write('''
69<plist version="1.0">
70 <dict>
71 <key>application-identifier</key> <string>{prefix}.com.google.{app}</string>
72 <key>get-task-allow</key> <true/>
73 </dict>
74</plist>
75'''.format(prefix=prefix, app=app))
76 f.flush()
77
78 subprocess.check_call(['codesign',
79 '--force',
80 '--sign', identity,
81 '--entitlements', f.name,
82 '--timestamp=none',
83 pkg])