|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import subprocess |
| 5 | +from collections import defaultdict |
| 6 | +import re |
| 7 | +from copy import deepcopy |
| 8 | +import os |
| 9 | + |
| 10 | +cy_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 11 | + |
| 12 | +# from https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 |
| 13 | +def deep_merge(a: dict, b: dict) -> dict: |
| 14 | + """Merge two dicts and return a singular dict""" |
| 15 | + result = deepcopy(a) |
| 16 | + for bk, bv in b.items(): |
| 17 | + av = result.get(bk) |
| 18 | + if isinstance(av, dict) and isinstance(bv, dict): |
| 19 | + result[bk] = deep_merge(av, bv) |
| 20 | + else: |
| 21 | + result[bk] = deepcopy(bv) |
| 22 | + return result |
| 23 | + |
| 24 | +if __name__ == "__main__": |
| 25 | + parser = argparse.ArgumentParser(description='Pretty print all configs given a filelist of scala files') |
| 26 | + parser.add_argument('FILE', type=str, help='Filelist of scala files to search within') |
| 27 | + parser.add_argument('-l', '--levels', default=0, type=int, help='Number of levels to recursively look for configs') |
| 28 | + args = parser.parse_args() |
| 29 | + |
| 30 | + files = [] |
| 31 | + with open(args.FILE, 'r') as f: |
| 32 | + files = f.read().splitlines() |
| 33 | + |
| 34 | + cmd = ['grep', '-o', r"class \+.* \+extends \+Config"] + files |
| 35 | + r = subprocess.run(cmd, check=True, capture_output=True) |
| 36 | + |
| 37 | + base_file_path_dict = defaultdict(list) |
| 38 | + for l in r.stdout.decode("UTF-8").splitlines(): |
| 39 | + match = re.match(r"^(.*):class +([a-zA-Z_$][a-zA-Z\d_$]*).* +extends", l) |
| 40 | + if match: |
| 41 | + base_file_path_dict[match.group(1)].append(match.group(2)) |
| 42 | + |
| 43 | + levels = [] |
| 44 | + for level in range(args.levels): |
| 45 | + if level == 0: |
| 46 | + # use the base |
| 47 | + dict_to_use = base_file_path_dict |
| 48 | + else: |
| 49 | + # use the level-1 dict |
| 50 | + assert len(levels) > 0 |
| 51 | + dict_to_use = levels[-1] |
| 52 | + |
| 53 | + file_path_dict = defaultdict(list) |
| 54 | + |
| 55 | + for configs in dict_to_use.values(): |
| 56 | + for config in configs: |
| 57 | + cmd = ['grep', '-o', r"class \+.* \+extends \+" + f"{config}"] + files |
| 58 | + r = subprocess.run(cmd, capture_output=True) |
| 59 | + |
| 60 | + for l in r.stdout.decode("UTF-8").splitlines(): |
| 61 | + match = re.match(r"^(.*):class +([a-zA-Z_$][a-zA-Z\d_$]*).* +extends", l) |
| 62 | + if match: |
| 63 | + file_path_dict[match.group(1)].append(match.group(2)) |
| 64 | + |
| 65 | + levels.append(file_path_dict) |
| 66 | + |
| 67 | + final_dict = base_file_path_dict |
| 68 | + for dct in levels: |
| 69 | + final_dict = deep_merge(final_dict, dct) |
| 70 | + |
| 71 | + print(f"Finding all one-line config. fragments (up to {args.levels} levels)\n") |
| 72 | + for k, v in final_dict.items(): |
| 73 | + print(f"{k.replace(cy_path, 'chipyard')}:") |
| 74 | + for e in v: |
| 75 | + print(f" {e}") |
| 76 | + print("") |
0 commit comments