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 | |
Louis Dionne | ab950ac | 2020-03-20 19:28:36 -0400 | [diff] [blame] | 15 | import argparse |
| 16 | import os |
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() |
| 23 | parser.add_argument('--codesign_identity', type=str, required=False) |
| 24 | parser.add_argument('--working_directory', type=str, required=True) |
| 25 | parser.add_argument('--dependencies', type=str, nargs='*', required=True) |
| 26 | parser.add_argument('--env', type=str, nargs='*', required=True) |
| 27 | (args, remaining) = parser.parse_known_args(sys.argv[1:]) |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 28 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 29 | if len(remaining) < 2: |
| 30 | sys.stderr.write('Missing actual commands to run') |
| 31 | exit(1) |
| 32 | remaining = remaining[1:] # Skip the '--' |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 33 | |
| 34 | # Do any necessary codesigning. |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 35 | if args.codesign_identity: |
| 36 | exe = remaining[0] |
| 37 | rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={}) |
| 38 | if rc != 0: |
| 39 | sys.stderr.write('Failed to codesign: ' + exe) |
| 40 | return rc |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 41 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 42 | # Extract environment variables into a dictionary |
Louis Dionne | ab950ac | 2020-03-20 19:28:36 -0400 | [diff] [blame] | 43 | 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] | 44 | |
| 45 | # Ensure the file dependencies exist |
| 46 | for file in args.dependencies: |
| 47 | if not os.path.exists(file): |
| 48 | sys.stderr.write('Missing file {} marked as a dependency of a test'.format(file)) |
| 49 | exit(1) |
| 50 | |
| 51 | # Run the executable with the given environment in the given working directory |
| 52 | return subprocess.call(remaining, cwd=args.working_directory, env=env) |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 53 | |
| 54 | if __name__ == '__main__': |
| 55 | exit(main()) |