blob: beb3603b04e2081842163d4cad37c5f28b3a8bee [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
Jim Van Verth4e502972017-12-07 15:16:10 -050019# identstr search string (regex fragment) for code signing identity
20# profile name of provisioning profile
21pkg,identstr,profile = sys.argv[1:]
Mike Klein63afe642017-01-31 12:07:33 -050022
23# Find the Google signing identity.
24identity = None
25for line in subprocess.check_output(['security', 'find-identity']).split('\n'):
Jim Van Verth4e502972017-12-07 15:16:10 -050026 m = re.match(r'''.*\) (.*) "''' + identstr + '"', line)
Mike Klein63afe642017-01-31 12:07:33 -050027 if m:
28 identity = m.group(1)
29assert identity
30
31# Find the Google mobile provisioning profile.
32mobileprovision = None
33for p in glob.glob(os.path.join(os.environ['HOME'], 'Library', 'MobileDevice',
34 'Provisioning Profiles', '*.mobileprovision')):
35 if re.search(r'''<key>Name</key>
Jim Van Verth4e502972017-12-07 15:16:10 -050036\t<string>''' + profile + r'''</string>''', open(p).read(), re.MULTILINE):
Mike Klein63afe642017-01-31 12:07:33 -050037 mobileprovision = p
38assert mobileprovision
Mike Klein6d3b7aa2017-01-30 14:00:54 -050039
Jim Van Verth443a9132017-11-28 09:45:26 -050040# The .mobileprovision just gets copied into the package.
Mike Klein6d3b7aa2017-01-30 14:00:54 -050041shutil.copy(mobileprovision,
42 os.path.join(pkg, 'embedded.mobileprovision'))
43
Mike Klein6d3b7aa2017-01-30 14:00:54 -050044# Extract the appliciation identitifer prefix from the .mobileprovision.
45m = re.search(r'''<key>ApplicationIdentifierPrefix</key>
46\t<array>
47\t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE)
48prefix = m.group(1)
49
Jim Van Verth443a9132017-11-28 09:45:26 -050050app, _ = os.path.splitext(os.path.basename(pkg))
51
Mike Klein6d3b7aa2017-01-30 14:00:54 -050052# Write a minimal entitlements file, then codesign.
53with tempfile.NamedTemporaryFile() as f:
54 f.write('''
55<plist version="1.0">
56 <dict>
57 <key>application-identifier</key> <string>{prefix}.com.google.{app}</string>
58 <key>get-task-allow</key> <true/>
59 </dict>
60</plist>
61'''.format(prefix=prefix, app=app))
62 f.flush()
63
64 subprocess.check_call(['codesign',
65 '--force',
66 '--sign', identity,
67 '--entitlements', f.name,
68 '--timestamp=none',
69 pkg])