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 |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 17 | import shutil |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 18 | import subprocess |
| 19 | import sys |
| 20 | |
| 21 | |
| 22 | def main(): |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 23 | parser = argparse.ArgumentParser() |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 24 | parser.add_argument('--execdir', type=str, required=True) |
Louis Dionne | 00fddf4 | 2020-04-03 17:50:39 -0400 | [diff] [blame] | 25 | parser.add_argument('--codesign_identity', type=str, required=False, default=None) |
| 26 | parser.add_argument('--dependencies', type=str, nargs='*', required=False, default=[]) |
| 27 | parser.add_argument('--env', type=str, nargs='*', required=False, default=dict()) |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 28 | (args, remaining) = parser.parse_known_args(sys.argv[1:]) |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 29 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 30 | if len(remaining) < 2: |
| 31 | sys.stderr.write('Missing actual commands to run') |
| 32 | exit(1) |
Louis Dionne | ac78e5b | 2020-04-17 16:43:35 -0400 | [diff] [blame^] | 33 | commandLine = remaining[1:] # Skip the '--' |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 34 | |
| 35 | # Do any necessary codesigning. |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 36 | if args.codesign_identity: |
Louis Dionne | ac78e5b | 2020-04-17 16:43:35 -0400 | [diff] [blame^] | 37 | exe = commandLine[0] |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 38 | rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={}) |
| 39 | if rc != 0: |
| 40 | sys.stderr.write('Failed to codesign: ' + exe) |
| 41 | return rc |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 42 | |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 43 | # Extract environment variables into a dictionary |
Louis Dionne | ab950ac | 2020-03-20 19:28:36 -0400 | [diff] [blame] | 44 | 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] | 45 | |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 46 | # Create the execution directory, and make sure we remove it at the end. |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 47 | try: |
Louis Dionne | e37be2e | 2020-04-14 16:20:00 -0400 | [diff] [blame] | 48 | os.makedirs(args.execdir) |
Louis Dionne | a6d477a | 2020-03-20 18:11:38 -0400 | [diff] [blame] | 49 | |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 50 | # Ensure the file dependencies exist and copy them to the execution directory. |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 51 | for dep in args.dependencies: |
| 52 | if not os.path.exists(dep): |
| 53 | sys.stderr.write('Missing file or directory "{}" marked as a dependency of a test'.format(dep)) |
| 54 | exit(1) |
| 55 | if os.path.isdir(dep): |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 56 | shutil.copytree(dep, os.path.join(args.execdir, os.path.basename(dep)), symlinks=True) |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 57 | else: |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 58 | shutil.copy2(dep, args.execdir) |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 59 | |
Louis Dionne | ac78e5b | 2020-04-17 16:43:35 -0400 | [diff] [blame^] | 60 | # Run the command line with the given environment in the execution directory. |
| 61 | return subprocess.call(subprocess.list2cmdline(commandLine), cwd=args.execdir, env=env, shell=True) |
Louis Dionne | 2022e66 | 2020-04-01 18:48:22 -0400 | [diff] [blame] | 62 | finally: |
Louis Dionne | 53129a6 | 2020-04-07 15:42:00 -0400 | [diff] [blame] | 63 | shutil.rmtree(args.execdir) |
| 64 | |
Vedant Kumar | e56ddc5 | 2019-09-05 21:24:23 +0000 | [diff] [blame] | 65 | |
| 66 | if __name__ == '__main__': |
| 67 | exit(main()) |