Creates temp symlink tree, runs program, and copies back outputs. Args: inputs: List of fake paths to real paths, which are used for symlink tree. program: List containing real path of program and its arguments. The execroot directory will be appended as the last argument.
(inputs, program, outputs)
| 23 | |
| 24 | |
| 25 | def run(inputs, program, outputs): |
| 26 | """Creates temp symlink tree, runs program, and copies back outputs. |
| 27 | |
| 28 | Args: |
| 29 | inputs: List of fake paths to real paths, which are used for symlink tree. |
| 30 | program: List containing real path of program and its arguments. The |
| 31 | execroot directory will be appended as the last argument. |
| 32 | outputs: List of fake outputted paths to copy back to real paths. |
| 33 | Returns: |
| 34 | 0 if succeeded or nonzero if failed. |
| 35 | """ |
| 36 | root = tempfile.mkdtemp() |
| 37 | try: |
| 38 | cwd = os.getcwd() |
| 39 | for fake, real in inputs: |
| 40 | parent = os.path.join(root, os.path.dirname(fake)) |
| 41 | if not os.path.exists(parent): |
| 42 | os.makedirs(parent) |
| 43 | # Use symlink if possible and not on Windows, since on Windows 10 |
| 44 | # symlinks exist but they require administrator privileges to use. |
| 45 | if hasattr(os, "symlink") and not os.name == "nt": |
| 46 | os.symlink(os.path.join(cwd, real), os.path.join(root, fake)) |
| 47 | else: |
| 48 | shutil.copyfile( |
| 49 | os.path.join(cwd, real), os.path.join(root, fake) |
| 50 | ) |
| 51 | if subprocess.call(program + [root]) != 0: |
| 52 | return 1 |
| 53 | for fake, real in outputs: |
| 54 | shutil.copyfile(os.path.join(root, fake), real) |
| 55 | return 0 |
| 56 | finally: |
| 57 | try: |
| 58 | shutil.rmtree(root) |
| 59 | except EnvironmentError: |
| 60 | # Ignore "file in use" errors on Windows; ok since it's just a tmpdir. |
| 61 | pass |
| 62 | |
| 63 | |
| 64 | def main(args): |