-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparsed_to_networkx.py
49 lines (36 loc) · 1.35 KB
/
parsed_to_networkx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
import csv
import networkx as nx
import pickle as pkl
from src.graph.utils import *
def write_graph(graph, output_dir, func_name):
graph_path = output_dir + '/' + func_name + '.gpickle'
triple_path = output_dir + '/' + func_name + '.triples'
vector_path = output_dir + '/' + func_name + '.vec'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print("Writing graph: %s" % graph_path)
nx.write_gpickle(graph, graph_path)
trips = tripleize(graph)
print("Writing triples: %s" % triple_path)
pkl.dump(trips, open(triple_path, 'wb'))
vec = vectorize(graph)
print("Writing vector: %s" % vector_path)
pkl.dump(vec, open(vector_path, 'wb'))
def print_usage():
print("Usage: python parsed_to_networkx.py <directory> <output_dir>")
if __name__ == "__main__":
if len(sys.argv) != 3:
print_usage()
exit()
parsed_nodes_file = sys.argv[1]
output_dir = sys.argv[2]
base_dir = parsed_nodes_file[:-len('nodes.csv')]
parsed_edges_file = base_dir + 'edges.csv'
print("Nodes: %s" % parsed_nodes_file)
print("Edges: %s" % parsed_edges_file)
print("Output: %s" % output_dir)
graphs = joern_to_networkx(parsed_nodes_file, parsed_edges_file)
for g in graphs:
write_graph(g['graph'], output_dir + '/' + base_dir, g['name'])