blob: 6f546531c80af7e1e38c30c8acf71a403f397e30 [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
Ben Wagner076c50c2019-09-27 18:16:02 -040010import os.path
Mike Klein6d3b7aa2017-01-30 14:00:54 -050011import re
12import shutil
13import subprocess
14import sys
15import tempfile
16
17# Arguments to the script:
Jim Van Verth443a9132017-11-28 09:45:26 -050018# pkg path to application directory, e.g. out/Debug/dm.app
19# executable and plist should already be in this directory
Jim Van Verth4e502972017-12-07 15:16:10 -050020# identstr search string (regex fragment) for code signing identity
Ben Wagner076c50c2019-09-27 18:16:02 -040021# profile path or name of provisioning profile
Jim Van Verth4e502972017-12-07 15:16:10 -050022pkg,identstr,profile = sys.argv[1:]
Mike Klein63afe642017-01-31 12:07:33 -050023
Jim Van Verthee1098d2020-11-09 12:12:13 -050024# Find the signing identity.
Mike Klein63afe642017-01-31 12:07:33 -050025identity = None
26for line in subprocess.check_output(['security', 'find-identity']).split('\n'):
Jim Van Verth4e502972017-12-07 15:16:10 -050027 m = re.match(r'''.*\) (.*) "''' + identstr + '"', line)
Mike Klein63afe642017-01-31 12:07:33 -050028 if m:
29 identity = m.group(1)
Jim Van Verthee1098d2020-11-09 12:12:13 -050030if identity is None:
31 print("Signing identity matching '" + identstr + "' not found.")
32 print("Please verify by running 'security find-identity' or checking your keychain.")
33 sys.exit(1)
Mike Klein63afe642017-01-31 12:07:33 -050034
Jim Van Verthee1098d2020-11-09 12:12:13 -050035# Find the mobile provisioning profile.
Mike Klein63afe642017-01-31 12:07:33 -050036mobileprovision = None
Ben Wagner076c50c2019-09-27 18:16:02 -040037if os.path.isfile(profile):
38 mobileprovision = profile
39else:
40 for p in glob.glob(os.path.join(os.environ['HOME'], 'Library', 'MobileDevice',
41 'Provisioning Profiles',
42 '*.mobileprovision')):
43 if re.search(r'''<key>Name</key>
Jim Van Verth4e502972017-12-07 15:16:10 -050044\t<string>''' + profile + r'''</string>''', open(p).read(), re.MULTILINE):
Ben Wagner076c50c2019-09-27 18:16:02 -040045 mobileprovision = p
Jim Van Verthee1098d2020-11-09 12:12:13 -050046if mobileprovision is None:
47 print("Provisioning profile matching '" + profile + "' not found.")
48 print("Please verify that the correct profile is installed in '${HOME}/Library/MobileDevice/Provisioning Profiles' or specify the path directly.")
49 sys.exit(1)
Mike Klein6d3b7aa2017-01-30 14:00:54 -050050
Jim Van Verth443a9132017-11-28 09:45:26 -050051# The .mobileprovision just gets copied into the package.
Mike Klein6d3b7aa2017-01-30 14:00:54 -050052shutil.copy(mobileprovision,
53 os.path.join(pkg, 'embedded.mobileprovision'))
54
Mike Klein6d3b7aa2017-01-30 14:00:54 -050055# Extract the appliciation identitifer prefix from the .mobileprovision.
56m = re.search(r'''<key>ApplicationIdentifierPrefix</key>
57\t<array>
58\t<string>(.*)</string>''', open(mobileprovision).read(), re.MULTILINE)
59prefix = m.group(1)
60
Jim Van Verth443a9132017-11-28 09:45:26 -050061app, _ = os.path.splitext(os.path.basename(pkg))
62
Mike Klein6d3b7aa2017-01-30 14:00:54 -050063# Write a minimal entitlements file, then codesign.
64with tempfile.NamedTemporaryFile() as f:
65 f.write('''
66<plist version="1.0">
67 <dict>
68 <key>application-identifier</key> <string>{prefix}.com.google.{app}</string>
69 <key>get-task-allow</key> <true/>
70 </dict>
71</plist>
72'''.format(prefix=prefix, app=app))
73 f.flush()
74
75 subprocess.check_call(['codesign',
76 '--force',
77 '--sign', identity,
78 '--entitlements', f.name,
79 '--timestamp=none',
80 pkg])