Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 1 | #===----------------------------------------------------------------------===## |
| 2 | # |
| 3 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | # See https://llvm.org/LICENSE.txt for license information. |
| 5 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | # |
| 7 | #===----------------------------------------------------------------------===## |
| 8 | |
| 9 | """run.py is a utility for running a program. |
| 10 | |
| 11 | It can perform code signing, forward arguments to the program, and return the |
| 12 | program's error code. |
| 13 | """ |
| 14 | |
| 15 | import subprocess |
| 16 | import sys |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame^] | 17 | import argparse |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | def main(): |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame^] | 21 | parser = argparse.ArgumentParser() |
| 22 | parser.add_argument('--codesign_identity', type=str, required=False) |
| 23 | parser.add_argument('--working_directory', type=str, required=True) |
| 24 | parser.add_argument('--dependencies', type=str, nargs='*', required=True) |
| 25 | parser.add_argument('--env', type=str, nargs='*', required=True) |
| 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) |
| 31 | remaining = 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: |
| 35 | exe = remaining[0] |
| 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 |
| 42 | env = {k : v for (k, v) in map(lambda s: s.split('='), args.env)} |
| 43 | |
| 44 | # Ensure the file dependencies exist |
| 45 | for file in args.dependencies: |
| 46 | if not os.path.exists(file): |
| 47 | sys.stderr.write('Missing file {} marked as a dependency of a test'.format(file)) |
| 48 | exit(1) |
| 49 | |
| 50 | # Run the executable with the given environment in the given working directory |
| 51 | return subprocess.call(remaining, cwd=args.working_directory, env=env) |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 52 | |
| 53 | if __name__ == '__main__': |
| 54 | exit(main()) |