Revert "gen_run_config_candidates.py: simplify logic in fragment extraction"

This reverts commit 0f7c772915f076bfe9919b986f52ece3f9d54f98.

Signed-off-by: Leonardo Sandoval <leonardo.sandoval@linaro.org>
Change-Id: I944e745b74ff0102b95b79b7e4bd95e7c0778837
diff --git a/script/gen_run_config_candidates.py b/script/gen_run_config_candidates.py
index 6a1e4ae..5a52f99 100755
--- a/script/gen_run_config_candidates.py
+++ b/script/gen_run_config_candidates.py
@@ -22,8 +22,6 @@
 if len(opts.args) != 1:
     raise Exception("Exactly one argument expected")
 
-exit_code = 0
-
 # Obtain path to run_config directory
 script_root = os.path.dirname(os.path.abspath(sys.argv[0]))
 run_config_dir = os.path.join(script_root, os.pardir, "run_config")
@@ -34,34 +32,37 @@
     raise Exception("Couldn't extract run config from " + arg)
 
 if run_config == "nil":
-    sys.exit(exit_code)
+    sys.exit(0)
 
 fragments = run_config.split("-")
+if 'bmcov' in fragments:
+    fragments.remove('bmcov')
+exit_code = 0
 
-ignored_fragments      = ['bmcov']
-not_prefixed_fragments = ['debug']
+# Stems are fragments, except with everything after dot removed.
+stems = list(map(lambda f: f.split(".")[0], fragments))
 
-for f in fragments[1:]:
-    if f in ignored_fragments:
-        # these fragments are ignored
-        continue
-    elif f in not_prefixed_fragments:
-        # these fragments are NOT prefixed by first fragment
-        fragment = f
-    else:
-        # for the rest of the cases, prefix first fragment
-        fragment = "-".join([fragments[0],f])
+# Consider each fragment in turn
+for frag_idx, chosen_fragment in enumerate(fragments):
+    # Choose all stems upto the current fragment
+    chosen = ["-".join(stems[0:i] + [chosen_fragment])
+            for i in range(frag_idx + 1)]
 
-    if opts.print_only:
-        print(fragment)
-    else:
-        # Output only if a matching run config exists
-        if os.path.isfile(os.path.join(run_config_dir, fragment)):
-            # Stop looking for generic once a specific fragment is found
+    for i, fragment in enumerate(reversed(chosen)):
+        if opts.print_only:
             print(fragment)
         else:
+            # Output only if a matching run config exists
+            if os.path.isfile(os.path.join(run_config_dir, fragment)):
+                # Stop looking for generic once a specific fragment is found
+                print(fragment)
+                break
+    else:
+        # Ignore if the first fragment doesn't exist, which is usually the
+        # platform name. Otherwise, print a warning for not finding matches for
+        # the fragment.
+        if (not opts.print_only) and (i > 0):
             print("warning: {}: no matches for fragment '{}'".format(
                 arg, fragment), file=sys.stderr)
             exit_code = 1
-
 sys.exit(exit_code)