示例#1
0
def seq_process(code_path, preprocess):
    nodes = Node_extract(code_path, preprocess)
    dd = to_dict(nodes)
    num_list = dd[1]
    node_list = dd[2]
    seq = Sequence_generate(node_list, num_list)
    return seq
示例#2
0
def find_module(line_number):
    if os.path.exists(dir_path + '/jsons/AST.json') and os.path.exists(
            dir_path + '/jsons/file_path.json'):
        Input_id = line_number
        Input_line = int(Input_id[0:8])
        Input_file = int(Input_id[8:16])
        max = 0
        lines = 0
        output_list = []
        count = 0
        with open(dir_path + '/jsons/file_path.json') as f1:
            file_path_list = json.load(f1)
        f1.close()
        if Input_file >= len(file_path_list):
            raise SystemExit("The file number is too large")
        file_name = file_path_list[Input_file]
        node_list = Node_extract(file_name, True)[2]
        node_list_new = to_init_dict(node_list, Input_file)[1]
        print("###############The Result################")
        print(str(file_name) + ":" + str(Input_line))
        for i in range(1, len(node_list_new)):
            line_range = node_list_new[i]['coord']
            if line_range != 'null':
                if line_range[0] != 'null' and line_range[1] != 'null':
                    first_line = line_range[0]
                    last_line = line_range[1]
                    line_number1 = int(first_line[0:8])
                    line_number2 = int(last_line[0:8])
                    if line_number2 > max:
                        max = line_number2
                    if line_number1 != line_number2:
                        if line_number1 <= Input_line and Input_line <= line_number2:
                            print("This module's node type is: %s " %
                                  (node_list_new[i]['_nodetype']))
                            print("This module's coordinate is: %s " %
                                  (node_list_new[i]['coord']))
                            print("This module's name is: %s " %
                                  (node_list_new[i]['node_name']))
                            count = count + 1
                            if lines <= line_number2 - line_number1:
                                lines = line_number2 - line_number1
                                output_list = node_list_new[i]['coord']
        if count == 0:
            print("No module satisfied")
        if Input_line > max:
            raise SystemExit(
                "The line number is too large \n#########################################"
            )
        print("#########################################")
        return output_list
示例#3
0
def node_graph(code_path, preprocess, same_list):
    """Show the syntax tree and display the same part of tow trees"""
    node_list = Node_extract(code_path, preprocess)
    num_list = []
    name = re.findall(RE_C, code_path)
    for x in range(1, len(node_list)):
        num = node_list[x]['_nodetype'].find('-')
        num_list.append(num)
        cut = re.findall(RE_AZ, node_list[x]['_nodetype'])
        if len(cut) > 0:
            node_list[x]['_nodetype'] = cut[0]
        else:
            node_list[x]['_nodetype'] = ''
    num_list.insert(0, 0)
    if same_list == None:
        num_cp = num_list
        dot = Digraph(comment=name, format="pdf")
        dot.node('0', 'TranslationUnitDecl')
        for y in range(1, max(num_cp) + 1, 2):
            if y == 1:
                for x in range(len(node_list)):
                    if num_list[x] == y:
                        dot.node(str(x), node_list[x]['_nodetype'])
                        dot.edge('0', str(x))
            if y > 1:
                for x in range(len(num_cp)):
                    if num_list[x] == y:
                        dot.node(str(x), node_list[x]['_nodetype'])
                        for z in range(x, 0, -1):
                            if num_list[z] == y - 2:
                                dot.edge(str(z), str(x))
                                break
    else:
        num_cp = num_list
        same_cp = same_list
        dot = Digraph(comment=name, format="pdf")
        if 0 in same_cp:
            dot.node('0', 'TranslationUnitDecl', _attributes={"color": "red"})
        else:
            dot.node('0', 'TranslationUnitDecl')
        for y in range(1, max(num_cp) + 1, 2):
            if y == 1:
                for x in range(len(node_list)):
                    if num_list[x] == y:
                        if x in same_cp:
                            dot.node(str(x),
                                     node_list[x]['_nodetype'],
                                     _attributes={"color": "red"})
                            dot.edge('0', str(x))
                        else:
                            dot.node(str(x), node_list[x]['_nodetype'])
                            dot.edge('0', str(x))
            if y > 1:
                for x in range(len(num_cp)):
                    if num_list[x] == y:
                        if x in same_cp:
                            dot.node(str(x),
                                     node_list[x]['_nodetype'],
                                     _attributes={"color": "red"})
                            for z in range(x, 0, -1):
                                if num_list[z] == y - 2:
                                    dot.edge(str(z), str(x))
                                    break
                        else:
                            dot.node(str(x), node_list[x]['_nodetype'])
                            for z in range(x, 0, -1):
                                if num_list[z] == y - 2:
                                    dot.edge(str(z), str(x))
                                    break
    save_name = './output/' + str(name[0])
    dot.view(save_name)
示例#4
0
                    line_number2 = int(last_line[0:8])
                    if line_number2 > max:
                        max = line_number2
                    if line_number1 != line_number2:
                        if line_number1 <= Input_line and Input_line <= line_number2:
                            print("This module's node type is: %s " %
                                  (node_list_new[i]['_nodetype']))
                            print("This module's coordinate is: %s " %
                                  (node_list_new[i]['coord']))
                            print("This module's name is: %s " %
                                  (node_list_new[i]['node_name']))
                            count = count + 1
                            if lines <= line_number2 - line_number1:
                                lines = line_number2 - line_number1
                                output_list = node_list_new[i]['coord']
        if count == 0:
            print("No module satisfied")
        if Input_line > max:
            raise SystemExit(
                "The line number is too large \n#########################################"
            )
        print("#########################################")
        return output_list


if __name__ == "__main__":
    code_path = '/home/jinzhenghui/ovs/vswitchd/bridge.c'
    node_list = Node_extract(code_path, True)
    node_list_new = to_dict(node_list[0], 0)[0]
    print(node_list_new)
示例#5
0
 code_path_list = []
 names = []
 json_files_dir = dir_path + '/jsons'
 if not os.path.exists(json_files_dir):
     os.mkdir(json_files_dir)
 # json_file1 is a nested dict
 json_file1 = json_files_dir + '/AST.json'
 # json file2 is the trace of each node
 json_file2 = json_files_dir + '/trace.json'
 # json file3 restore the name of each module
 json_file3 = json_files_dir + '/Module_names.json'
 # json file4 restore the unnested dict
 json_file4 = json_files_dir + '/uAST.json'
 if '.c' in code_path or '.cpp' in code_path or '.h' in code_path:
     code_path_list.append(code_path)
     node_list, name_all, node_list1 = Node_extract(
         code_path, sel)
     node_list.insert(0, code_path)
     names.append(name_all)
     to_json(node_list, json_file1, json_file2, False)
     uAST = to_init_dict(node_list1, 0)[1]
     print('The total amount of the nodes is {}'.format(
         len(node_list1)))
     with open(json_file3, 'w+') as f:
         json.dump(names, f, ensure_ascii=False, indent=4)
     f.close()
     with open(json_file4, 'w+') as f1:
         json.dump(uAST, f1, ensure_ascii=False, indent=4)
     f1.close()
     print("The json file path: " + json_files_dir)
 else:
     json_list = []
示例#6
0
         raise SystemExit("Usage: python main.py --view c_file_dir True/False ")
 elif args[0] == '--tojson':
     if len(args[1:]) == 2:
         if args[2] != 'True' and args[2] != 'False':
             raise SystemExit("Please use 'True' or 'False' to choose whether you need to remove the headers")
         else:
             if args[2] == 'True':
                 sel = True
             else:
                 sel = False
         if os.path.exists(args[1]):
             code_path = args[1]
             code_path_list = []
             if '.c' in code_path or '.cpp' in code_path:
                 code_path_list.append(code_path)
                 node_list = Node_extract(code_path, sel)
                 print ('The total amount of the nodes is {}'.format(len(node_list)))
                 json_files_dir = dir_path + '/jsons'
                 if not os.path.exists(json_files_dir):
                     os.mkdir(json_files_dir)
                 json_file1 = json_files_dir + '/AST.json'
                 json_file2 = json_files_dir + '/trace.json'
                 to_json(node_list, json_file1, json_file2, False)
                 print ("The json file path: "+json_files_dir)
             else:
                 json_list = []
                 node_len = 0
                 g = os.walk(code_path)
                 for path, di, filelist in g:
                     for filename in filelist:
                         k = os.path.join(path, filename)
示例#7
0
         if len(args[1:]) == 1:
             if os.path.exists(args[1]):
                 code_path_a = args[1]
                 sel = True
                 seq1 = AST_Compare.seq_process(code_path_a, sel)
                 node_graph(code_path_a, sel, None)
             else:
                 raise SystemExit("Error: Could not find c/c++ file")
         else:
             raise SystemExit("Usage: python main.py --view c_file_dir")
     elif args[0] == '--tojson':
         if len(args[1:]) == 1:
             if os.path.exists(args[1]):
                 code_path_a = args[1]
                 sel = True
                 node_list = Node_extract(code_path_a, sel)
                 json_files_dir = dir_path + '/jsons'
                 if not os.path.exists(json_files_dir):
                     os.mkdir(json_files_dir)
                 json_file = json_files_dir + '/output.json'
                 to_json(node_list, json_file)
                 print("The json file path: " + json_files_dir)
             else:
                 raise SystemExit("Error: Could not find c/c++ file")
         else:
             raise SystemExit("Usage: python main.py --tojson c_file_dir")
     else:
         raise SystemExit(
             "Usage: python main.py --compare c_file_dir1 c_file_dir2 \n       python main.py --view c_file_dir \n       python main.py --tojson c_file_dir "
         )
 else:
示例#8
0
def node_graph(code_path, preprocess, same_list):
    """Show the syntax tree and display the same part of tow trees"""
    if '.c' not in code_path or '.cpp' not in code_path:
        code_path_list = []
        g = os.walk(code_path)
        for path, di, filelist in g:
            for filename in filelist:
                k = os.path.join(path, filename)
                if '.c' in k or '.cpp' in k:
                    code_path_list.append(k)
        dot = Digraph("G", format="pdf")
        dot.node('Start', 'Start')
        for i in range(len(code_path_list)):
            node_list = Node_extract(code_path_list[i], preprocess)
            num_list = []
            name = re.findall(RE_C, code_path_list[i])
            for x in range(1, len(node_list)):
                num = node_list[x]['_nodetype'].find('-')
                num_list.append(num)
                cut = re.findall(RE_AZ, node_list[x]['_nodetype'])
                if len(cut) > 0:
                    node_list[x]['_nodetype'] = cut[0]
                else:
                    node_list[x]['_nodetype'] = ''
            num_list.insert(0, 0)
            if same_list == None:
                num_cp = num_list
                sub = Digraph(comment=name)
                sub.node(str(i) + 'a0', str(name))
                for y in range(1, max(num_cp) + 1, 2):
                    if y == 1:
                        for x in range(len(node_list)):
                            if num_list[x] == y:
                                sub.node(
                                    str(i) + 'b' + str(x),
                                    node_list[x]['_nodetype'])
                                sub.edge(str(i) + 'a0', str(i) + 'b' + str(x))
                    if y > 1:
                        for x in range(len(num_cp)):
                            if num_list[x] == y:
                                sub.node(
                                    str(i) + 'b' + str(x),
                                    node_list[x]['_nodetype'])
                                for z in range(x, 0, -1):
                                    if num_list[z] == y - 2:
                                        sub.edge(
                                            str(i) + 'b' + str(z),
                                            str(i) + 'b' + str(x))
                                        break
            dot.subgraph(sub)
            dot.edge('Start', str(i) + 'a0')
        save_name = './output/combine'
        dot.view(save_name)
        if os.path.getsize(save_name + '.pdf') > 200000:
            raise SystemExit("Error: The graph is too large for evince")
    else:
        node_list = Node_extract(code_path, preprocess)
        num_list = []
        name = re.findall(RE_C, code_path)
        for x in range(1, len(node_list)):
            num = node_list[x]['_nodetype'].find('-')
            num_list.append(num)
            cut = re.findall(RE_AZ, node_list[x]['_nodetype'])
            if len(cut) > 0:
                node_list[x]['_nodetype'] = cut[0]
            else:
                node_list[x]['_nodetype'] = ''
        num_list.insert(0, 0)
        if same_list == None:
            num_cp = num_list
            dot = Digraph(comment=name, format="pdf")
            dot.node('0', 'TranslationUnitDecl')
            for y in range(1, max(num_cp) + 1, 2):
                if y == 1:
                    for x in range(len(node_list)):
                        if num_list[x] == y:
                            dot.node(str(x), node_list[x]['_nodetype'])
                            dot.edge('0', str(x))
                if y > 1:
                    for x in range(len(num_cp)):
                        if num_list[x] == y:
                            dot.node(str(x), node_list[x]['_nodetype'])
                            for z in range(x, 0, -1):
                                if num_list[z] == y - 2:
                                    dot.edge(str(z), str(x))
                                    break
        else:
            num_cp = num_list
            same_cp = same_list
            dot = Digraph(comment=name, format="pdf")
            if 0 in same_cp:
                dot.node('0',
                         'TranslationUnitDecl',
                         _attributes={"color": "red"})
            else:
                dot.node('0', 'TranslationUnitDecl')
            for y in range(1, max(num_cp) + 1, 2):
                if y == 1:
                    for x in range(len(node_list)):
                        if num_list[x] == y:
                            if x in same_cp:
                                dot.node(str(x),
                                         node_list[x]['_nodetype'],
                                         _attributes={"color": "red"})
                                dot.edge('0', str(x))
                            else:
                                dot.node(str(x), node_list[x]['_nodetype'])
                                dot.edge('0', str(x))
                if y > 1:
                    for x in range(len(num_cp)):
                        if num_list[x] == y:
                            if x in same_cp:
                                dot.node(str(x),
                                         node_list[x]['_nodetype'],
                                         _attributes={"color": "red"})
                                for z in range(x, 0, -1):
                                    if num_list[z] == y - 2:
                                        dot.edge(str(z), str(x))
                                        break
                            else:
                                dot.node(str(x), node_list[x]['_nodetype'])
                                for z in range(x, 0, -1):
                                    if num_list[z] == y - 2:
                                        dot.edge(str(z), str(x))
                                        break
        save_name = './output/' + str(name[0])
        dot.view(save_name)
        if os.path.getsize(save_name + '.pdf') > 200000:
            raise SystemExit("Error: The graph is too large for evince")