import os
import sys
from matplotlib import pyplot as plt
import networkx as nx

import hashlib
import json
import pickle


if __name__ == '__main__':
    done_index = 0
    file_name_list = os.listdir('../A2C/')
    res_file = "../sample.jsonl"
    for file_name in file_name_list:
        file_path = '../A2C/' + file_name
        testpath = '../store/' + file_name + '.ida'
        if os.path.exists(testpath) and os.path.splitext(file_path)[-1].lower() == '.exe':
            fr = open(testpath, 'r')
            data1 = pickle.load(fr)
            for graph in data1.raw_graph_list:
                for i in range(len(graph.old_g.node)):
                    if len(graph.old_g.node[i]['externs']) != 0:
                        print graph.old_g.node[i]['externs']
            # for i in range(len(data1.raw_graph_list)):







            # fr = open(testpath, 'r')
            # data1 = pickle.load(fr)
            # # funtion num
            # function_number = len(data1.raw_graph_list)
            # if function_number == 0:
            #     continue
            # # function_edges
            # function_edge_start = []
            # function_edge_end = []
            # for item in data1.raw_graph_list[0].old_g.edges:
            #     function_edge_start.append(item[0])
            #     function_edge_end.append(item[1])
            # function_edges = [function_edge_start, function_edge_end]
            # fun_name_temp = []
            # # function hsah
            # acfg_list = []

            # for i in range(len(data1.raw_graph_list)):
            #
            #     # function name
            #     fun_name_temp.append(data1.raw_graph_list[i].funcname)
            #     # block features
            #     temp_G = data1.raw_graph_list[i].old_g
            #     # block_number
            #     block_number = len(temp_G.node)
            #     # block_features
            #     acfg_list_item_feature = []
            #     for temp in range(len(temp_G.node)):
            #         block_features = []
            #         # call
            #         block_features.append(temp_G.node[temp]['numCalls'])
            #         # transfer
            #         block_features.append(temp_G.node[temp]['numTIs'])
            #         # arithmetic
            #         block_features.append(temp_G.node[temp]['numAs'])
            #         # logic
            #         block_features.append(temp_G.node[temp]['numLIs'])
            #         # compare
            #         block_features.append(temp_G.node[temp]['numCom'])
            #         # move
            #         block_features.append(temp_G.node[temp]['numMov'])
            #         # termination
            #         block_features.append(temp_G.node[temp]['numTerm'])
            #         # date declaration
            #         block_features.append(temp_G.node[temp]['numDD'])
            #         # total instructions
            #         block_features.append(temp_G.node[temp]['numIns'])
            #         # string or integer constants
            #         block_features.append(
            #             len(temp_G.node[temp]['strings']) if len(temp_G.node[temp]['strings']) != 0 else len(
            #                 temp_G.node[temp]['consts']))
            #         # offspring
            #         block_features.append(temp_G.node[temp]['offs'])
            #         acfg_list_item_feature.append(block_features)
            #     edge_list_start = []
            #     edge_list_end = []
            #     for item in temp_G.edges:
            #         edge_list_start.append(item[0])
            #         edge_list_end.append(item[1])
            #     block_edges = [edge_list_start, edge_list_end]
            #     acfg_list_item = {"block_number": block_number, "block_edges": block_edges,
            #                       "block_features": acfg_list_item_feature}
            #     acfg_list.append(acfg_list_item)

