-
Notifications
You must be signed in to change notification settings - Fork 4
/
display-structs.py
119 lines (87 loc) · 3.71 KB
/
display-structs.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import sys
import msgpack
from treelib import Node, Tree
def read_binary_file(filename):
with open(filename, 'rb') as f:
return f.read()
def print_evolves(metadata_dir, ecall=None):
for filename in os.listdir(metadata_dir):
metadata_path = metadata_dir + "/" + filename
metadata = msgpack.unpackb(read_binary_file(metadata_path), raw=False, strict_map_key=False)
if metadata["info"]["struct"]["ecall"] != ecall:
continue
for evolve in metadata["info"]["struct"]["evolves"]:
evolve_from = evolve["from"].decode()
evolve_to = evolve["to"].decode()
evolve_parent = evolve["parent"].decode()
print(evolve["parent_id"], "->", evolve["id"])
print(evolve_from, "->", evolve_to)
if evolve_from != evolve_parent:
print(evolve_from, "->", evolve_to)
if evolve_from != evolve_parent:
print()
print("ALERT: from != parent")
print("ALERT:", evolve_from, "!=", evolve_parent)
def print_id_tree(metadata_dir, ecall=None):
tree = Tree()
ids = [id[5:] for id in os.listdir(metadata_dir)]
ids.sort()
devnull = open(os.devnull, 'w')
stder = sys.stderr
sys.stderr = devnull
for node_id in ids:
metadata_path = metadata_dir + "/node_" + node_id
metadata = msgpack.unpackb(read_binary_file(metadata_path), raw=False, strict_map_key=False)
if metadata["info"]["struct"]["ecall"] != ecall:
continue
parent_id = int(metadata["info"]["parent"])
node_id = int(node_id)
node_struct = metadata["info"]["struct"]["data"].decode()
if not tree:
tree.create_node(parent_id, parent_id)
if node_id not in tree:
tree.create_node(node_struct, node_id, parent=parent_id)
sys.stder = stder
tree.show()
def print_evolve_tree(metadata_dir, ecall=None):
evolve_tree = Tree()
ids = [id[5:] for id in os.listdir(metadata_dir)]
ids.sort()
for filename in ids: # os.listdir(metadata_dir)
metadata_path = metadata_dir + "/node_" + filename
print(metadata_path)
metadata = msgpack.unpackb(read_binary_file(metadata_path), raw=False, strict_map_key=False)
if metadata["info"]["struct"]["ecall"] != ecall:
continue
print(metadata["info"]["parent"], int(filename))
# print(filename)
for evolve in metadata["info"]["struct"]["evolves"]:
# evolve_from = evolve["from"].decode()
# evolve_to = evolve["to"].decode()
evolve_from = int(evolve["parent_id"])
evolve_to = int(evolve["id"])
evolve_parent = evolve["parent"].decode()
print(evolve_from, "->", evolve_to)
if not evolve_tree:
print("Adding Root:", evolve_from)
evolve_tree.create_node(evolve_from, evolve_from)
# if evolve_from not in evolve_tree:
# evolve_tree.show()
if evolve_to not in evolve_tree.subtree(evolve_from):
print("Adding Child:", evolve_to)
evolve_tree.create_node(evolve_to, evolve_to, parent=evolve_from)
evolve_tree.show()
def main(workdir, ecall=None):
metadata_dir = workdir + "/metadata"
# print_evolves(metadata_dir, ecall)
# print_evolve_tree(metadata_dir, ecall)
print_id_tree(metadata_dir, ecall)
if __name__ == "__main__":
if len(sys.argv) == 2:
main(sys.argv[1])
elif len(sys.argv) == 3:
main(sys.argv[1], ecall=int(sys.argv[2]))
else:
print("Missing arguments. Usage:\n\n\t%s </path/to/workdir>\n" % sys.argv[0])
sys.exit()