-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdependency_resolution.py
133 lines (107 loc) · 3.43 KB
/
dependency_resolution.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
from enum import Enum
from io import StringIO
from app.package_version import *
from app.dependency import *
from app.reverse_dependency import *
from app.package_registry import *
class DependencyResolution:
def __init__(self, version, kind, hidden=False):
self.version = version
self.kind = kind
self.parent = None
self.hidden = hidden
self.children = []
@classmethod
def runtime(cls, version, hidden=False):
return cls(version, DependencyKind.RUNTIME, hidden)
@classmethod
def development(cls, version, hidden=False):
return cls(version, DependencyKind.DEVELOPMENT, hidden)
@property
def is_runtime(self):
return self.kind == DependencyKind.RUNTIME
@property
def is_development(self):
return self.kind == DependencyKind.DEVELOPMENT
@property
def is_root(self):
return self.parent is None
@property
def is_leaf(self):
return self.children == []
@property
def is_tree(self):
return not self.is_leaf
@property
def found(self):
return not isinstance(self.version, PackageVersionNotFound)
@property
def name(self):
return self.version.name
@property
def number(self):
return self.version.number
def dependencies(self, runtime_only=False):
if runtime_only:
return self.version.runtime_dependencies
else:
return (self.version.runtime_dependencies +
self.version.development_dependencies)
def has_dependencies(self, runtime_only=False):
if runtime_only:
return len(self.version.runtime_dependencies) > 0
else:
return (len(self.version.runtime_dependencies) > 0 or
len(self.version.development_dependencies) > 0)
def add_child(self, child):
child.parent = self
self.children.append(child)
return self
def add_children(self, children):
for i in children:
self.add_child(i)
return self
def hide(self):
self.hidden = True
def reverse_dependencies(self):
collected = {}
self.__reverse_dependencies(collected)
return collected.values()
def __reverse_dependencies(self, collected):
for child in self.children:
v = child.version
if v not in collected:
collected[v] = ReverseDependency(v)
if child.parent:
collected[v].add_reference(child.parent, child.kind)
child.__reverse_dependencies(collected)
def __repr__(self, level=0):
io = StringIO()
self.__print(io, level)
return io.getvalue()
def __print(self, io, level):
self.__print_header(io, level)
for child in self.children:
child.__print(io, level + 1)
def __print_header(self, io, level):
header = '{0}{1} [{2}] {3}'.format(
self.__indentation(level),
self.__bullet(),
str(self.kind),
repr(self.version)
)
print(header, file=io)
def __bullet(self):
if self.hidden:
return '•'
elif not self.found:
return '!'
elif self.is_leaf:
return '='
else:
return '+'
@staticmethod
def __indentation(level):
if level == 0:
return ''
return ('⎮ ' * (level - 1)) + '⎮--'