blob: 5a52f9907c097bed16573320a29bacd74063a10a [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
Leonardo Sandoval579c7372020-10-23 15:23:32 -05003# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
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
12import argparse
13import os
14import sys
15
16parser = argparse.ArgumentParser(description="Choose run configurations")
17parser.add_argument("--print-only", "-p", action="store_true", default=False,
18 help="Print only; don't check for matching run configs.")
19parser.add_argument("args", nargs=argparse.REMAINDER, help="Run configuration")
20opts = parser.parse_args()
21
22if len(opts.args) != 1:
23 raise Exception("Exactly one argument expected")
24
25# Obtain path to run_config directory
26script_root = os.path.dirname(os.path.abspath(sys.argv[0]))
27run_config_dir = os.path.join(script_root, os.pardir, "run_config")
28
29arg = opts.args[0]
30run_config = arg.split(":")[-1]
31if not run_config:
32 raise Exception("Couldn't extract run config from " + arg)
33
34if run_config == "nil":
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060035 sys.exit(0)
Fathi Boudra422bf772019-12-02 11:10:16 +020036
37fragments = run_config.split("-")
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060038if 'bmcov' in fragments:
39 fragments.remove('bmcov')
40exit_code = 0
Fathi Boudra422bf772019-12-02 11:10:16 +020041
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060042# Stems are fragments, except with everything after dot removed.
43stems = list(map(lambda f: f.split(".")[0], fragments))
Fathi Boudra422bf772019-12-02 11:10:16 +020044
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060045# Consider each fragment in turn
46for frag_idx, chosen_fragment in enumerate(fragments):
47 # Choose all stems upto the current fragment
48 chosen = ["-".join(stems[0:i] + [chosen_fragment])
49 for i in range(frag_idx + 1)]
Fathi Boudra422bf772019-12-02 11:10:16 +020050
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060051 for i, fragment in enumerate(reversed(chosen)):
52 if opts.print_only:
Fathi Boudra422bf772019-12-02 11:10:16 +020053 print(fragment)
54 else:
Leonardo Sandoval72d4f432020-10-26 14:52:24 -060055 # Output only if a matching run config exists
56 if os.path.isfile(os.path.join(run_config_dir, fragment)):
57 # Stop looking for generic once a specific fragment is found
58 print(fragment)
59 break
60 else:
61 # Ignore if the first fragment doesn't exist, which is usually the
62 # platform name. Otherwise, print a warning for not finding matches for
63 # the fragment.
64 if (not opts.print_only) and (i > 0):
Fathi Boudra422bf772019-12-02 11:10:16 +020065 print("warning: {}: no matches for fragment '{}'".format(
66 arg, fragment), file=sys.stderr)
67 exit_code = 1
Fathi Boudra422bf772019-12-02 11:10:16 +020068sys.exit(exit_code)