Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | # Output to stdout the chosen run configuration fragments for a given run |
| 9 | # configuration. With -p, the script prints all fragments considered without |
| 10 | # validating whether it exists. |
| 11 | |
| 12 | import argparse |
| 13 | import os |
| 14 | import sys |
| 15 | |
| 16 | parser = argparse.ArgumentParser(description="Choose run configurations") |
| 17 | parser.add_argument("--print-only", "-p", action="store_true", default=False, |
| 18 | help="Print only; don't check for matching run configs.") |
| 19 | parser.add_argument("args", nargs=argparse.REMAINDER, help="Run configuration") |
| 20 | opts = parser.parse_args() |
| 21 | |
| 22 | if len(opts.args) != 1: |
| 23 | raise Exception("Exactly one argument expected") |
| 24 | |
| 25 | # Obtain path to run_config directory |
| 26 | script_root = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 27 | run_config_dir = os.path.join(script_root, os.pardir, "run_config") |
| 28 | |
| 29 | arg = opts.args[0] |
| 30 | run_config = arg.split(":")[-1] |
| 31 | if not run_config: |
| 32 | raise Exception("Couldn't extract run config from " + arg) |
| 33 | |
| 34 | if run_config == "nil": |
| 35 | sys.exit(0) |
| 36 | |
| 37 | fragments = run_config.split("-") |
| 38 | exit_code = 0 |
| 39 | |
| 40 | # Stems are fragments, except with everything after dot removed. |
| 41 | stems = list(map(lambda f: f.split(".")[0], fragments)) |
| 42 | |
| 43 | # Consider each fragment in turn |
| 44 | for frag_idx, chosen_fragment in enumerate(fragments): |
| 45 | # Choose all stems upto the current fragment |
| 46 | chosen = ["-".join(stems[0:i] + [chosen_fragment]) |
| 47 | for i in range(frag_idx + 1)] |
| 48 | |
| 49 | for i, fragment in enumerate(reversed(chosen)): |
| 50 | if opts.print_only: |
| 51 | print(fragment) |
| 52 | else: |
| 53 | # Output only if a matching run config exists |
| 54 | if os.path.isfile(os.path.join(run_config_dir, fragment)): |
| 55 | # Stop looking for generic once a specific fragment is found |
| 56 | print(fragment) |
| 57 | break |
| 58 | else: |
| 59 | # Ignore if the first fragment doesn't exist, which is usually the |
| 60 | # platform name. Otherwise, print a warning for not finding matches for |
| 61 | # the fragment. |
| 62 | if (not opts.print_only) and (i > 0): |
| 63 | print("warning: {}: no matches for fragment '{}'".format( |
| 64 | arg, fragment), file=sys.stderr) |
| 65 | exit_code = 1 |
| 66 | |
| 67 | sys.exit(exit_code) |