brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 6b4d615 Raw
73 lines · python
1#!/usr/bin/env python2# ===----------------------------------------------------------------------===##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-exception7#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 the13program's error code.14"""15 16import argparse17import os18import platform19import subprocess20 21 22def main():23    parser = argparse.ArgumentParser()24    parser.add_argument("--execdir", type=str, required=True)25    parser.add_argument("--codesign_identity", type=str, required=False, default=None)26    parser.add_argument("--env", type=str, nargs="*", required=False, default=[])27    parser.add_argument(28        "--prepend_env", type=str, nargs="*", required=False, default=[]29    )30    parser.add_argument("command", nargs=argparse.ONE_OR_MORE)31    args = parser.parse_args()32    commandLine = args.command33 34    # HACK:35    # If an argument is a file that ends in `.tmp.exe`, assume it is the name36    # of an executable generated by a test file. We call these test-executables37    # below. This allows us to do custom processing like codesigning test-executables.38    # It's also possible for there to be no such executable, for example in the case39    # of a .sh.cpp test.40    isTestExe = lambda exe: exe.endswith(".tmp.exe") and os.path.exists(exe)41 42    # Do any necessary codesigning of test-executables found in the command line.43    if args.codesign_identity:44        for exe in filter(isTestExe, commandLine):45            codesign = ["codesign", "-f", "-s", args.codesign_identity, exe]46            subprocess.check_call(codesign, env={})47 48    # Extract environment variables into a dictionary49    env = {k: v for (k, v) in map(lambda s: s.split("=", 1), args.env)}50 51    # Set environment variables where we prepend the given value to the52    # existing environment variable.53    for (k, v) in map(lambda s: s.split("=", 1), args.prepend_env):54        if k in os.environ:55            v = v + os.pathsep + os.environ[k]56        env[k] = v57 58    if platform.system() == "Windows":59        # Pass some extra variables through on Windows:60        # COMSPEC is needed for running subprocesses via std::system().61        if "COMSPEC" in os.environ:62            env["COMSPEC"] = os.environ.get("COMSPEC")63        # TEMP is needed for placing temp files in a sensible directory.64        if "TEMP" in os.environ:65            env["TEMP"] = os.environ.get("TEMP")66 67    # Run the command line with the given environment in the execution directory.68    return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)69 70 71if __name__ == "__main__":72    exit(main())73