# 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")



[((109, 130), 'os.chdir', 'os.chdir', (['sys.path[0]'], {}), '(sys.path[0])\n', (117, 130), False, 'import os, sys, json\n'), ((131, 152), 'sys.path.append', 'sys.path.append', (['""".."""'], {}), "('..')\n", (146, 152), False, 'import os, sys, json\n'), ((361, 389), 'os.path.exists', 'os.path.exists', (['GEN_FILE_DIR'], {}), '(GEN_FILE_DIR)\n', (375, 389), False, 'import os, sys, json\n'), ((395, 424), 'sys.path.append', 'sys.path.append', (['GEN_FILE_DIR'], {}), '(GEN_FILE_DIR)\n', (410, 424), False, 'import os, sys, json\n'), ((631, 642), 'sys.exit', 'sys.exit', (['(1)'], {}), '(1)\n', (639, 642), False, 'import os, sys, json\n'), ((689, 737), 'analyses_common.argparse_and_get_files', 'ac.argparse_and_get_files', (['"""Graph SLOC/SoC data"""'], {}), "('Graph SLOC/SoC data')\n", (714, 737), True, 'import analyses_common as ac\n'), ((920, 989), 'analyses_common.build_dict_two_lvl_cnt', 'ac.build_dict_two_lvl_cnt', (['json_files', 'dfc.JSON_ARC', 'dfc.JSON_CMP_STR'], {}), '(json_files, dfc.JSON_ARC, dfc.JSON_CMP_STR)\n', (945, 989), True, 'import analyses_common as ac\n'), ((1032, 1089), 'graph_dd_sloc_by_arch.get_sloc_avg_and_list_by_arch', 'get_sloc_avg_and_list_by_arch', (['cmp_by_arch'], {'verbose': '(False)'}), '(cmp_by_arch, verbose=False)\n', (1061, 1089), False, 'from graph_dd_sloc_by_arch import get_sloc_avg_and_list_by_arch\n'), ((1936, 2068), 'analyses_common.print_mean_median_std_dev_for_dict_of_lists', 'ac.print_mean_median_std_dev_for_dict_of_lists', (['soc_sloc_by_arch', '"""\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""")\n', (1982, 2068), True, 'import analyses_common as ac\n'), ((447, 488), 'os.path.join', 'os.path.join', (['GEN_FILE_DIR', '"""sloc_cnt.py"""'], {}), "(GEN_FILE_DIR, 'sloc_cnt.py')\n", (459, 488), False, 'import os, sys, json\n'), ((1240, 1260), 'json.load', 'json.load', (['json_file'], {}), '(json_file)\n', (1249, 1260), False, 'import os, sys, json\n'), ((1453, 1481), 'df_common.cmp_str_to_sloc', 'dfc.cmp_str_to_sloc', (['cmp_str'], {}), '(cmp_str)\n', (1472, 1481), True, 'import df_common as dfc\n'), ((254, 268), 'pathlib.Path', 'Path', (['__file__'], {}), '(__file__)\n', (258, 268), False, 'from pathlib import Path\n')]