1# Copyright (c) 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import argparse
6import os
7import subprocess
8import sys
9import re
10
11def ListIdentities():
12  return subprocess.check_output([
13    'xcrun',
14    'security',
15    'find-identity',
16    '-v',
17    '-p',
18    'codesigning',
19  ])
20
21
22def FindValidIdentity(identity_description):
23  lines = list(map(str.strip, ListIdentities().splitlines()))
24  # Look for something like "2) XYZ "iPhone Developer: Name (ABC)""
25  exp = re.compile('[0-9]+\) ([A-F0-9]+) "([^"]*)"')
26  for line in lines:
27    res = exp.match(line)
28    if res is None:
29      continue
30    if identity_description in res.group(2):
31      yield res.group(1)
32
33
34if __name__ == '__main__':
35  parser = argparse.ArgumentParser('codesign iOS bundles')
36  parser.add_argument(
37      '--developer_dir', required=False,
38      help='Path to Xcode.')
39  parser.add_argument(
40      '--identity-description', required=True,
41      help='Text description used to select the code signing identity.')
42  args = parser.parse_args()
43  if args.developer_dir:
44    os.environ['DEVELOPER_DIR'] = args.developer_dir
45
46  for identity in FindValidIdentity(args.identity_description):
47    print identity
48