Alex Light | 4d8d83f | 2019-04-16 11:18:45 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Copyright 2019, The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Run a command using multiple cores in parallel. Stop when one exits zero and save the log from |
| 19 | that run. |
| 20 | """ |
| 21 | |
| 22 | import argparse |
| 23 | import concurrent.futures |
| 24 | import contextlib |
| 25 | import itertools |
| 26 | import os |
| 27 | import os.path |
| 28 | import shutil |
| 29 | import subprocess |
| 30 | import tempfile |
| 31 | |
| 32 | |
| 33 | def run_one(cmd, tmpfile): |
| 34 | """Run the command and log result to tmpfile. Return both the file name and returncode.""" |
| 35 | with open(tmpfile, "x") as fd: |
| 36 | return tmpfile, subprocess.run(cmd, stdout=fd).returncode |
| 37 | |
| 38 | @contextlib.contextmanager |
| 39 | def nowait(ppe): |
| 40 | """Run a ProcessPoolExecutor and shutdown without waiting.""" |
| 41 | try: |
| 42 | yield ppe |
| 43 | finally: |
| 44 | ppe.shutdown(False) |
| 45 | |
| 46 | def main(): |
| 47 | parser = argparse.ArgumentParser( |
| 48 | description="Run a command using multiple cores and save non-zero exit log" |
| 49 | ) |
| 50 | parser.add_argument("--jobs", "-j", type=int, help="max number of jobs. default 60", default=60) |
| 51 | parser.add_argument("cmd", help="command to run") |
| 52 | parser.add_argument("--out", type=str, help="where to put result", default="out_log") |
| 53 | args = parser.parse_args() |
| 54 | cnt = 0 |
| 55 | ids = itertools.count(0) |
| 56 | with tempfile.TemporaryDirectory() as td: |
| 57 | print("Temporary files in {}".format(td)) |
| 58 | with nowait(concurrent.futures.ProcessPoolExecutor(args.jobs)) as p: |
| 59 | fs = set() |
| 60 | while True: |
| 61 | for _, idx in zip(range(args.jobs - len(fs)), ids): |
| 62 | fs.add(p.submit(run_one, args.cmd, os.path.join(td, "run_log." + str(idx)))) |
| 63 | ws = concurrent.futures.wait(fs, return_when=concurrent.futures.FIRST_COMPLETED) |
| 64 | fs = ws.not_done |
| 65 | done = list(map(lambda a: a.result(), ws.done)) |
| 66 | cnt += len(done) |
| 67 | print("{} runs".format(cnt)) |
| 68 | failed = [d for d,r in done if r != 0] |
| 69 | succ = [d for d,r in done if r == 0] |
| 70 | for f in succ: |
| 71 | os.remove(f) |
| 72 | if len(failed) != 0: |
| 73 | if len(failed) != 1: |
| 74 | for f,i in zip(failed, range(len(failed))): |
| 75 | shutil.copyfile(f, args.out+"."+str(i)) |
| 76 | else: |
| 77 | shutil.copyfile(failed[0], args.out) |
| 78 | break |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main() |