# External deps
import os, sys, json
from pathlib import Path
from typing import Dict, List

# Internal deps
os.chdir(sys.path[0])
sys.path.append("..")
import df_common as dfc
import analyses_common as ac

# Generated files directory
GEN_FILE_DIR = str(Path(__file__).resolve().parent.parent) + os.sep + "generated_files" # TODO: ugly parent.parent pathing
if os.path.exists(GEN_FILE_DIR):
    sys.path.append(GEN_FILE_DIR)
    if os.path.exists(os.path.join(GEN_FILE_DIR, "sloc_cnt.py")):
        from sloc_cnt import DRIVER_NAME_TO_SLOC
else:
    print("Error: no SLOC file! Run \'df_analyze.py\' with \'--linux-src-dir\'")
    sys.exit(1)

if __name__ == "__main__":

    json_files = ac.argparse_and_get_files("Graph SLOC/SoC data")
    soc_sloc_by_arch: Dict[str, List[int]] = {}

    print("Gathering SLOC average by arch...")
    from graph_dd_sloc_by_arch import get_sloc_avg_and_list_by_arch
    cmp_by_arch = ac.build_dict_two_lvl_cnt(json_files, dfc.JSON_ARC, dfc.JSON_CMP_STR)
    avg_sloc_by_arch, sloc_list_by_arch = get_sloc_avg_and_list_by_arch(cmp_by_arch, verbose = False)

    # Collection
    print("Iterating DTBs/SoCs...")
    for dtb_json in json_files:

        with open(dtb_json) as json_file:
            data = json.load(json_file)

        soc_sloc = 0
        arch = data[dfc.JSON_ARC]
        cmp_strs = data[dfc.JSON_CMP_STR]

        # Total SLOC for this SoC
        for cmp_str in cmp_strs:
            driver_sloc = dfc.cmp_str_to_sloc(cmp_str)
            if not driver_sloc: # Closed-source driver
                driver_sloc = avg_sloc_by_arch[arch]
            soc_sloc += driver_sloc
            #print("{}: {}".format(cmp_str, driver_sloc))

        if arch not in soc_sloc_by_arch:
            soc_sloc_by_arch[arch] = []
        else:
            soc_sloc_by_arch[arch].append(soc_sloc)

        print("{} ({}): {}".format(dtb_json.split(os.sep)[-1], arch, soc_sloc))

    # Final stats
    ac.print_mean_median_std_dev_for_dict_of_lists(soc_sloc_by_arch,
        "\nSloc Per Soc, format: [arch : (mean, median, std_dev)]\n")



[((7, 0, 7, 21), 'os.chdir', 'os.chdir', ({(7, 9, 7, 20): 'sys.path[0]'}, {}), '(sys.path[0])', False, 'import os, sys, json\n'), ((8, 0, 8, 21), 'sys.path.append', 'sys.path.append', ({(8, 16, 8, 20): '""".."""'}, {}), "('..')", False, 'import os, sys, json\n'), ((14, 3, 14, 31), 'os.path.exists', 'os.path.exists', ({(14, 18, 14, 30): 'GEN_FILE_DIR'}, {}), '(GEN_FILE_DIR)', False, 'import os, sys, json\n'), ((15, 4, 15, 33), 'sys.path.append', 'sys.path.append', ({(15, 20, 15, 32): 'GEN_FILE_DIR'}, {}), '(GEN_FILE_DIR)', False, 'import os, sys, json\n'), ((20, 4, 20, 15), 'sys.exit', 'sys.exit', ({(20, 13, 20, 14): '(1)'}, {}), '(1)', False, 'import os, sys, json\n'), ((24, 17, 24, 65), 'analyses_common.argparse_and_get_files', 'ac.argparse_and_get_files', ({(24, 43, 24, 64): '"""Graph SLOC/SoC data"""'}, {}), "('Graph SLOC/SoC data')", True, 'import analyses_common as ac\n'), ((29, 18, 29, 87), 'analyses_common.build_dict_two_lvl_cnt', 'ac.build_dict_two_lvl_cnt', ({(29, 44, 29, 54): 'json_files', (29, 56, 29, 68): 'dfc.JSON_ARC', (29, 70, 29, 86): 'dfc.JSON_CMP_STR'}, {}), '(json_files, dfc.JSON_ARC, dfc.JSON_CMP_STR)', True, 'import analyses_common as ac\n'), ((30, 42, 30, 101), 'graph_dd_sloc_by_arch.get_sloc_avg_and_list_by_arch', 'get_sloc_avg_and_list_by_arch', (), '', False, 'from graph_dd_sloc_by_arch import get_sloc_avg_and_list_by_arch\n'), ((59, 4, 60, 69), 'analyses_common.print_mean_median_std_dev_for_dict_of_lists', 'ac.print_mean_median_std_dev_for_dict_of_lists', ({(59, 51, 59, 67): 'soc_sloc_by_arch', (60, 8, 60, 68): '"""\nSloc Per Soc, format: [arch : (mean, median, std_dev)]\n"""'}, {}), '(soc_sloc_by_arch,\n    """\nSloc Per Soc, format: [arch : (mean, median, std_dev)]\n""")', True, 'import analyses_common as ac\n'), ((16, 22, 16, 63), 'os.path.join', 'os.path.join', ({(16, 35, 16, 47): 'GEN_FILE_DIR', (16, 49, 16, 62): '"""sloc_cnt.py"""'}, {}), "(GEN_FILE_DIR, 'sloc_cnt.py')", False, 'import os, sys, json\n'), ((37, 19, 37, 39), 'json.load', 'json.load', ({(37, 29, 37, 38): 'json_file'}, {}), '(json_file)', False, 'import os, sys, json\n'), ((45, 26, 45, 54), 'df_common.cmp_str_to_sloc', 'dfc.cmp_str_to_sloc', ({(45, 46, 45, 53): 'cmp_str'}, {}), '(cmp_str)', True, 'import df_common as dfc\n'), ((13, 19, 13, 33), 'pathlib.Path', 'Path', ({(13, 24, 13, 32): '__file__'}, {}), '(__file__)', False, 'from pathlib import Path\n')]