1#!/usr/bin/env python 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 12It can perform code signing, forward arguments to the program, and return the 13program's error code. 14""" 15 16import argparse 17import subprocess 18import sys 19 20 21def main(): 22 parser = argparse.ArgumentParser() 23 parser.add_argument('--execdir', type=str, required=True) 24 parser.add_argument('--codesign_identity', type=str, required=False, default=None) 25 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict()) 26 (args, remaining) = parser.parse_known_args(sys.argv[1:]) 27 28 if len(remaining) < 2: 29 sys.stderr.write('Missing actual commands to run') 30 exit(1) 31 commandLine = remaining[1:] # Skip the '--' 32 33 # Do any necessary codesigning. 34 if args.codesign_identity: 35 exe = commandLine[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 40 41 # Extract environment variables into a dictionary 42 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)} 43 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) 46 47 48if __name__ == '__main__': 49 exit(main()) 50