-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcombo_tree.py
More file actions
283 lines (222 loc) · 11.4 KB
/
Copy pathcombo_tree.py
File metadata and controls
283 lines (222 loc) · 11.4 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from __future__ import print_function
from combo_core import *
from combo_core.manifest import *
from combo_core.version import *
class CircularDependency(ComboException):
pass
class NotAllowedDependency(ComboException):
pass
class UndecidedTable(dict):
def __init__(self, dependencies=list()):
super(dict, self).__init__()
for dep in dependencies:
eliminators = [eliminator for eliminator in dependencies if self.is_eliminator(dep, eliminator)]
if any(eliminators):
self[dep] = {
'eliminators': eliminators,
'criticals': self.find_critical(dep, eliminators),
'alive': True,
'incompatible_eliminated': set()
}
def __str__(self):
result = ''
for undecided, details in self.items():
result += 'Dependency {}: alive={}, incompatible_eliminated={}, eliminators={}, criticals={}'.format(
str(undecided), details['alive'], [str(x) for x in details['incompatible_eliminated']],
[str(x) for x in details['eliminators']], [str(x) for x in details['criticals']]) + '\n'
return result
def get(self, dep):
if dep not in self.keys():
raise KeyError('Dependency {} is not undecided'.format(dep))
return self[dep]
def is_alive(self, dep):
if dep not in self.keys():
return True
return self[dep]['alive']
@staticmethod
def is_eliminator(undecided, eliminator):
return eliminator.name == undecided.name and eliminator.version > undecided.version
@staticmethod
def find_critical(undecided, all_eliminators):
return list(filter(lambda elm: not VersionNumber.compatible(elm.version, undecided.version), all_eliminators))
class ComboTree:
def __init__(self, dependency_importer):
self.original_nodes = dict()
self.manifests = dict()
self._importer = dependency_importer
self._undecideds = UndecidedTable()
self._dependencies = list()
self._head = dict()
self._done = False
def ready(self):
return self._done
def build(self, base_manifest):
""" Build the tree from the given manifest data recursively """
def build_tree(head_value, sons=list(), path=list()):
"""
In this step, every dependency of the current manifest will be cloned, followed by his own dependencies.
This function will continue recursively.
Additionally, while cloning a dependencies tree is going to be built.
:param head_value: The content of the current node of the tree
:param sons: A list of the sons (dependencies) that should be added next
:param path: The previous dependencies that has led here, used to identify circular dependencies
"""
tree_head = {'value': head_value}
next_path = path + [head_value]
if head_value.name in [dep.name for dep in path]:
higher_dep = xfilter(lambda dep: dep.name == head_value.name, path)
raise CircularDependency('Dependency {} eventually requires {}'.format(higher_dep, head_value),
' -> '.join(map(str, next_path)))
for dep in sons:
add_dep_node_flag = False
combo_dependency = ComboDep(dep['name'], dep['version'])
dependency_values = [dep_node['value'] for dep_node in self.original_nodes.values()]
if combo_dependency not in dependency_values:
add_dep_node_flag = True
cached_clone = self._importer.clone(combo_dependency)
else:
cached_clone = self._importer.get_cached_path(combo_dependency)
# Clone the recursive dependencies of the current dependency
dependency_manifest = Manifest(cached_clone, combo_dependency)
if not dependency_manifest.valid_as_lib():
raise NotAllowedDependency('Dependency {} cannot be used as a library'.format(combo_dependency))
self._add_manifest(combo_dependency, dependency_manifest)
next_sons = dependency_manifest.sons()
tree_head[combo_dependency] = build_tree(combo_dependency, next_sons, next_path)
if add_dep_node_flag:
self._add_node(tree_head[combo_dependency])
return tree_head
# clone everything - recursive clone, keeping both older and newer versions. Create a tree in the process.
self._head = build_tree(ComboRoot(), base_manifest.sons())
self._dependencies = self._extract_values()
def values(self):
return self._dependencies
def disconnect_outdated_versions(self):
""" Remove all irrelevant nodes from the tree using the following algorithm: """
while not self._is_slashed():
'''
1. create_undecided_table:
Iterate all dependencies. Mark undecided if there is a newer version somewhere.
For each undecided dependency, mark it's eliminators (newer versions of the same project).
Out of the eliminators, save the critical eliminators of it,
which are the eliminators with an incompatible version.
Additionally, save an 'alive' flag which always starts as True,
and 'incompatible_eliminated' which starts as an empty set.
undecided_table_example = {
"A-0.1": {
"eliminators": ["A-0.2", "A-1.0"], "criticals": ["A-1.0"],
"alive": True, "incompatible_eliminated": set()
},
"A-0.2": {
"eliminators": ["A-1.0"], "criticals": ["A-1.0"],
"alive": True, "incompatible_eliminated": set()
},
"B-0.1": {
"eliminators": ["B-0.2"], "criticals": [],
"alive": True, "incompatible_eliminated": set()
}
}
'''
self._undecideds = UndecidedTable(self._dependencies)
'''
2. mark_deads - Go through the tree:
if is_undecided:
pass # Don't go through the node's sons
else:
for each undecided:
if node_is_eliminator:
mark_as_dead
if node_is_critical_eliminator:
save_as_incompatible_eliminated
step_in() # Recursive
'''
self._mark_deads()
''' 3. step_in_alive_undecideds - Perform step 3 on every alive "undecided" from the undecided table '''
self._step_in_undecided()
'''
4. remove_deads_from_tree:
Go through the tree, if a node is marked as "dead" on the undecided table,
remove the node from the tree (this will remove his "sons" as well).
If a "directly" removed node is "incompatible_eliminated",
this means there is an error since the older version was connected to the tree
and it is removed because of an incompatible version.
If a "incompatible_eliminated" node was removed "indirectly",
this is fine because the "incompatible_eliminated" node wasn't required anyway.
'''
self._slash_deads()
self._done = True
def _extract_values(self, head=None):
head = head or self._head
values_set = set()
for son in self._get_sons(head):
values_set.add(son['value'])
values_set |= self._extract_values(son)
return values_set
def _is_slashed(self):
instances = {dep.name: len(list(filter(lambda x: x.name == dep.name, self._dependencies)))
for dep in self._dependencies}
return all(count == 1 for count in instances.values())
def __str__(self):
def recursive_str(head=None, indentation=0):
def new_line(indent):
return '\n' + '\t' * indent
head = head or self._head
separator = ',' + new_line(indentation + 1)
sons = separator.join(recursive_str(son, indentation + 1) for son in self._get_sons(head))
wrapped = new_line(indentation) + '{' + new_line(indentation + 1) + sons + new_line(indentation) + '}'
return str(head['value']) + ': ' + (wrapped if sons else '{}')
return recursive_str() + '\n'
def _add_node(self, dependency_node):
if dependency_node not in self.original_nodes.values():
self.original_nodes[dependency_node['value']] = dependency_node
@staticmethod
def _get_sons(head):
# Only the dict values are relevant
return [head[key] for key in head.keys() if key != 'value']
def _mark_deads(self, head=None):
head = head or self._head
if head['value'] in self._undecideds:
return
# If a node is eliminated, mark it as dead
for undecided in self._undecideds.values():
if head['value'] in undecided['eliminators']:
undecided['alive'] = False
# If the eliminator is critical, this means that if the undecided is relevant there is a problem
if head['value'] in undecided['criticals']:
undecided['incompatible_eliminated'].add(head['value'])
for son in self._get_sons(head):
# Continue recursively
self._mark_deads(son)
def _step_in_undecided(self):
# Iterate all alive undecided
for key, undecided in self._undecideds.items():
if undecided['alive']:
# Perform the "mark deads" step of each node of the current dependency
for node in self.original_nodes.values():
if node['value'] == key:
self._mark_deads(node)
def _slash_deads(self):
def recursive_slash(head=None):
head = head or self._head
pop_list = list()
for son in self._get_sons(head):
if self._undecideds.is_alive(son['value']):
recursive_slash(son)
else:
# If we explicitly need to remove an incompatible node, this means there is a problem
if son['value'] in self._undecideds:
incompatible_eliminator = self._undecideds.get(son['value'])['incompatible_eliminated']
if incompatible_eliminator:
raise IncompatibleVersions('Dependency {} could not be replaced by {}'.format(
son['value'], ', '.join(map(str, incompatible_eliminator))))
pop_list.append(son['value'])
for son_to_pop in pop_list:
head.pop(son_to_pop)
recursive_slash()
self._dependencies = self._extract_values()
def _add_manifest(self, dep, manifest):
if dep not in self.manifests.keys():
self.manifests[dep] = manifest
else:
if self.manifests[dep] != manifest:
raise ValueError('Different manifests found for dependency {}'.format(str(dep)))