-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.py
168 lines (136 loc) · 5.4 KB
/
tree.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from re import sub
from packaging import version
class Node:
def __init__(self, package, version, tags, parent, children, depth):
self.package = package
self.version = version[1:] # remove "v" from v1.2.3
self.tags = tags
self.parent = parent
self.children = []
self.depth = depth
self.previous = None
def __str__(self, level = 0):
str_rep = "\t" * level + f"{self.package} {self.version} {self.tags}" + "\n"
for child in self.children:
str_rep += child.__str__(level + 1)
return str_rep
def __repr__(self):
return f"{self.package} {self.version} {self.tags}"
class Tree:
COLOURS = {
"GREEN" :'\033[92m',
"RED" : '\033[91m',
"ENDC" : '\033[0m'
}
def __init__(self, cargo_raw, project = None, root = None):
self.project = project
self.root = root
self.size = 0
self.build_from_cargo(cargo_raw)
def bfs_hash_it(self, hmap):
queue = [self.root]
while queue:
p = queue.pop(0)
package_name = p.package
p_version = p.version
depth = p.depth
if package_name not in hmap and depth != 0:
hmap[package_name] = p_version
if package_name in hmap:
h_version = hmap[package_name] #tie breaker is highest package
if version.parse(p_version) > version.parse(h_version):
hmap[package_name]=p_version
children = p.children
if children:
for child in children:
queue.append(child)
def bfs_update(self, hmap, newhmap):
queue = [self.root]
while queue:
p = queue.pop(0)
package_name = p.package
current_version = p.version
if package_name in hmap:
anchor_version = hmap[package_name]
if version.parse(anchor_version) > version.parse(current_version):
p.version= anchor_version
p.previous = current_version
if package_name not in newhmap:
newhmap[package_name] =anchor_version
children = p.children
if children:
for child in children:
queue.append(child)
@classmethod
def get_depth(cls, str):
return len(str.rstrip()) - len(str.lstrip())
def build_from_cargo(self, cargo_raw):
stripped_cargo_raw = sub("[^\w\s\.\(\)\*\-]+", "", cargo_raw)
stripped_cargo_raw = stripped_cargo_raw.strip()
tree_body = stripped_cargo_raw.split("\n")
ptr = None
kinds = []
for line in tree_body:
line = line.rstrip()
dep = line.lstrip().split(" ")
depth = Tree.get_depth(line)
if len(dep) == 1:
kinds.append((depth, dep[0]))
else:
package_name, version, *tags = dep
node = Node(package_name, version, tags, "", [], depth)
if not self.root:
self.project = package_name
self.root = node
ptr = self.root
else:
if depth > ptr.depth:
node.parent = ptr
ptr.children.append(node)
ptr = node
elif depth == ptr.depth:
node.parent = ptr.parent
ptr.parent.children.append(node)
ptr = node
else:
while ptr.depth > depth:
ptr = ptr.parent
node.parent = ptr.parent
ptr.parent.children.append(node)
ptr = node
if kinds:
curr_kind = kinds[-1]
if node.depth > curr_kind[0]:
node.tags.append(curr_kind[1])
else:
kinds.pop(-1)
def __str__(self):
return str(self.root)
@classmethod
def to_ascii(cls, node, curr_id, prefix = ""):
def format_tree(node, curr_id, prefix = "", ls = []):
connector = "└── "
if node.depth != 0:
if node != node.parent.children[-1]:
connector = "├── "
st = f"{prefix}{connector}"
if(node.previous):
st += Tree.COLOURS["GREEN"] +"+"+ repr(node) + Tree.COLOURS["ENDC"] + "\n"
prev_st = repr(node).split(" ")
prev_st[1] = node.previous
st += " " + f"{prefix}" + Tree.COLOURS["RED"] + "".rjust(4) + "-"+f"{node.package} {node.previous} {node.tags}"+ Tree.COLOURS["ENDC"]
else:
st += repr(node)
ls.append(st + "\n")
children = node.children
for child in children:
new_prefix = prefix
if node.depth != 0:
if node == node.parent.children[-1]:
new_prefix += " "
else:
new_prefix += "│ "
format_tree(child, curr_id, new_prefix, ls)
return ls
tree = format_tree(node, curr_id, "", [])
return tree