Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Leonardo Sandoval | 579c737 | 2020-10-23 15:23:32 -0500 | [diff] [blame] | 3 | # Copyright (c) 2019-2020 Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 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.") |
Juan Pablo Conde | 2ecf186 | 2024-01-16 20:12:43 -0600 | [diff] [blame] | 19 | parser.add_argument("--unit-testing", action="store_true", default=False, |
| 20 | help="Use to indicate if it is a unit testing config or not") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 21 | parser.add_argument("args", nargs=argparse.REMAINDER, help="Run configuration") |
| 22 | opts = parser.parse_args() |
| 23 | |
| 24 | if len(opts.args) != 1: |
| 25 | raise Exception("Exactly one argument expected") |
| 26 | |
| 27 | # Obtain path to run_config directory |
| 28 | script_root = os.path.dirname(os.path.abspath(sys.argv[0])) |
Juan Pablo Conde | 2ecf186 | 2024-01-16 20:12:43 -0600 | [diff] [blame] | 29 | if (not opts.unit_testing): |
| 30 | run_config_dir = os.path.join(script_root, os.pardir, "run_config") |
| 31 | else: |
| 32 | run_config_dir = os.path.join(script_root, os.pardir, "run_config_tfut") |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 33 | |
| 34 | arg = opts.args[0] |
| 35 | run_config = arg.split(":")[-1] |
| 36 | if not run_config: |
| 37 | raise Exception("Couldn't extract run config from " + arg) |
| 38 | |
| 39 | if run_config == "nil": |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 40 | sys.exit(0) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 41 | |
| 42 | fragments = run_config.split("-") |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 43 | if 'bmcov' in fragments: |
| 44 | fragments.remove('bmcov') |
| 45 | exit_code = 0 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 46 | |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 47 | # Stems are fragments, except with everything after dot removed. |
| 48 | stems = list(map(lambda f: f.split(".")[0], fragments)) |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 49 | |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 50 | # Consider each fragment in turn |
| 51 | for frag_idx, chosen_fragment in enumerate(fragments): |
| 52 | # Choose all stems upto the current fragment |
| 53 | chosen = ["-".join(stems[0:i] + [chosen_fragment]) |
| 54 | for i in range(frag_idx + 1)] |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 55 | |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 56 | for i, fragment in enumerate(reversed(chosen)): |
| 57 | if opts.print_only: |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 58 | print(fragment) |
| 59 | else: |
Leonardo Sandoval | 72d4f43 | 2020-10-26 14:52:24 -0600 | [diff] [blame] | 60 | # Output only if a matching run config exists |
| 61 | if os.path.isfile(os.path.join(run_config_dir, fragment)): |
| 62 | # Stop looking for generic once a specific fragment is found |
| 63 | print(fragment) |
| 64 | break |
| 65 | else: |
| 66 | # Ignore if the first fragment doesn't exist, which is usually the |
| 67 | # platform name. Otherwise, print a warning for not finding matches for |
| 68 | # the fragment. |
| 69 | if (not opts.print_only) and (i > 0): |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 70 | print("warning: {}: no matches for fragment '{}'".format( |
| 71 | arg, fragment), file=sys.stderr) |
| 72 | exit_code = 1 |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 73 | sys.exit(exit_code) |