blob: e9f9859807b386a46d0dab7c6fedbd83e3630936 [file] [log] [blame]
Vedant Kumare56ddc52019-09-05 21:24:23 +00001#===----------------------------------------------------------------------===##
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
11It can perform code signing, forward arguments to the program, and return the
12program's error code.
13"""
14
Louis Dionneab950ac2020-03-20 19:28:36 -040015import argparse
16import os
Louis Dionne2341acb2020-04-17 16:43:35 -040017import pipes
Louis Dionne2022e662020-04-01 18:48:22 -040018import shutil
Vedant Kumare56ddc52019-09-05 21:24:23 +000019import subprocess
20import sys
21
22
23def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040024 parser = argparse.ArgumentParser()
Louis Dionne53129a62020-04-07 15:42:00 -040025 parser.add_argument('--execdir', type=str, required=True)
Louis Dionne00fddf42020-04-03 17:50:39 -040026 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
27 parser.add_argument('--dependencies', type=str, nargs='*', required=False, default=[])
28 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Louis Dionnea6d477a2020-03-20 18:11:38 -040029 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumare56ddc52019-09-05 21:24:23 +000030
Louis Dionnea6d477a2020-03-20 18:11:38 -040031 if len(remaining) < 2:
32 sys.stderr.write('Missing actual commands to run')
33 exit(1)
34 remaining = remaining[1:] # Skip the '--'
Vedant Kumare56ddc52019-09-05 21:24:23 +000035
36 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040037 if args.codesign_identity:
38 exe = remaining[0]
39 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
40 if rc != 0:
41 sys.stderr.write('Failed to codesign: ' + exe)
42 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000043
Louis Dionnea6d477a2020-03-20 18:11:38 -040044 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040045 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040046
Louis Dionne53129a62020-04-07 15:42:00 -040047 # Create the execution directory, and make sure we remove it at the end.
Louis Dionne2022e662020-04-01 18:48:22 -040048 try:
Louis Dionnee37be2e2020-04-14 16:20:00 -040049 os.makedirs(args.execdir)
Louis Dionnea6d477a2020-03-20 18:11:38 -040050
Louis Dionne53129a62020-04-07 15:42:00 -040051 # Ensure the file dependencies exist and copy them to the execution directory.
Louis Dionne2022e662020-04-01 18:48:22 -040052 for dep in args.dependencies:
53 if not os.path.exists(dep):
54 sys.stderr.write('Missing file or directory "{}" marked as a dependency of a test'.format(dep))
55 exit(1)
56 if os.path.isdir(dep):
Louis Dionne53129a62020-04-07 15:42:00 -040057 shutil.copytree(dep, os.path.join(args.execdir, os.path.basename(dep)), symlinks=True)
Louis Dionne2022e662020-04-01 18:48:22 -040058 else:
Louis Dionne53129a62020-04-07 15:42:00 -040059 shutil.copy2(dep, args.execdir)
Louis Dionne2022e662020-04-01 18:48:22 -040060
Louis Dionne2341acb2020-04-17 16:43:35 -040061 # Run the command line with the given environment in the execution directory.
62 commandLine = (pipes.quote(x) for x in remaining)
63 return subprocess.call(' '.join(commandLine), cwd=args.execdir, env=env, shell=True)
Louis Dionne2022e662020-04-01 18:48:22 -040064 finally:
Louis Dionne53129a62020-04-07 15:42:00 -040065 shutil.rmtree(args.execdir)
66
Vedant Kumare56ddc52019-09-05 21:24:23 +000067
68if __name__ == '__main__':
69 exit(main())