Louis Dionne | 1c28a70 | 2020-06-12 10:28:19 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 2 | #===----------------------------------------------------------------------===## |
| 3 | # |
| 4 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | # See https://llvm.org/LICENSE.txt for license information. |
| 6 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | # |
| 8 | #===----------------------------------------------------------------------===## |
| 9 | |
| 10 | """run.py is a utility for running a program. |
| 11 | |
| 12 | It can perform code signing, forward arguments to the program, and return the |
| 13 | program's error code. |
| 14 | """ |
| 15 | |
Louis Dionne | ab950ac | 2020-03-20 19:28:36 -0400 | [diff] [blame] | 16 | import argparse |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 17 | import subprocess |
| 18 | import sys |
| 19 | |
| 20 | |
| 21 | def main(): |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 22 | parser = argparse.ArgumentParser() |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 23 | parser.add_argument('--execdir', type=str, required=True) |
Louis Dionne | 00fddf4 | 2020-04-03 17:50:39 -0400 | [diff] [blame] | 24 | parser.add_argument('--codesign_identity', type=str, required=False, default=None) |
Louis Dionne | 00fddf4 | 2020-04-03 17:50:39 -0400 | [diff] [blame] | 25 | parser.add_argument('--env', type=str, nargs='*', required=False, default=dict()) |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 26 | (args, remaining) = parser.parse_known_args(sys.argv[1:]) |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 27 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 28 | if len(remaining) < 2: |
| 29 | sys.stderr.write('Missing actual commands to run') |
| 30 | exit(1) |
Louis Dionne | ac78e5b | 2020-04-17 16:43:35 -0400 | [diff] [blame] | 31 | commandLine = remaining[1:] # Skip the '--' |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 32 | |
| 33 | # Do any necessary codesigning. |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 34 | if args.codesign_identity: |
Louis Dionne | ac78e5b | 2020-04-17 16:43:35 -0400 | [diff] [blame] | 35 | exe = commandLine[0] |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 36 | rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={}) |
| 37 | if rc != 0: |
| 38 | sys.stderr.write('Failed to codesign: ' + exe) |
| 39 | return rc |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 40 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 41 | # Extract environment variables into a dictionary |
Louis Dionne | ab950ac | 2020-03-20 19:28:36 -0400 | [diff] [blame] | 42 | env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)} |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 43 | |
Louis Dionne | f3fe5f3 | 2020-06-10 14:41:47 -0400 | [diff] [blame] | 44 | # Run the command line with the given environment in the execution directory. |
| 45 | return subprocess.call(subprocess.list2cmdline(commandLine), cwd=args.execdir, env=env, shell=True) |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 46 | |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 47 | |
| 48 | if __name__ == '__main__': |
| 49 | exit(main()) |