blob: 66a97d39eeaf96c7effb865bb244e89c167f699f [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:
Jim Van Verth443a9132017-11-28 09:45:26 -050017# pkg path to application directory, e.g. out/Debug/dm.app
18# executable and plist should already be in this directory
19pkg, = sys.argv[1:]
Mike Klein63afe642017-01-31 12:07:33 -050020
21# Find the Google signing identity.
22identity = None
23for line in subprocess.check_output(['security', 'find-identity']).split('\n'):
24 m = re.match(r'''.*\) (.*) ".*Google.*"''', line)
25 if m:
26 identity = m.group(1)
27assert identity
28
29# Find the Google mobile provisioning profile.
30mobileprovision = None
31for p in glob.glob(os.path.join(os.environ['HOME'], 'Library', 'MobileDevice',
32 'Provisioning Profiles', '*.mobileprovision')):
33 if re.search(r'''<key>Name</key>
34\t<string>Google Development</string>''', open(p).read(), re.MULTILINE):
35 mobileprovision = p
36assert mobileprovision
Mike Klein6d3b7aa2017-01-30 14:00:54 -050037
Jim Van Verth443a9132017-11-28 09:45:26 -050038# The .mobileprovision just gets copied into the package.
Mike Klein6d3b7aa2017-01-30 14:00:54 -050039shutil.copy(mobileprovision,
40 os.path.join(pkg, 'embedded.mobileprovision'))
41
Mike Klein6d3b7aa2017-01-30 14:00:54 -050042# Extract the appliciation identitifer prefix from the .mobileprovision.
43m = re.search(r'''<key>ApplicationIdentifierPrefix</key>
44\t<array>
45\t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE)
46prefix = m.group(1)
47
Jim Van Verth443a9132017-11-28 09:45:26 -050048app, _ = os.path.splitext(os.path.basename(pkg))
49
Mike Klein6d3b7aa2017-01-30 14:00:54 -050050# Write a minimal entitlements file, then codesign.
51with 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])