-
Notifications
You must be signed in to change notification settings - Fork 4
/
package_list.py
165 lines (129 loc) · 4.7 KB
/
package_list.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
162
163
164
165
from rez.version import VersionRange
from rez.utils.formatting import PackageRequest
class VersionConflict(Exception):
pass
class PackageList:
def __init__(self, pkgs: "list[PackageRequest]"):
if not pkgs:
self._pkgs = []
elif isinstance(pkgs[0], str):
self._pkgs = [PackageRequest(x) for x in pkgs]
elif isinstance(pkgs[0], PackageRequest):
self._pkgs = [x for x in pkgs]
else:
raise RuntimeError(
f"PackageList constructor takes a list of str or PackageRequest, not {type(pkgs[0])}"
)
def has_conflicts_with(self, rhs):
for p in self._pkgs:
for r in rhs._pkgs:
if p.conflicts_with(r):
return f"{p} <-!-> {r}"
return False
def is_empty(self):
return len(self._pkgs) != 0
def get_conflicts(self, rhs):
conflicts = []
for p in self._pkgs:
for r in rhs._pkgs:
if p.conflicts_with(r):
conflicts.append[(p, r)]
return conflicts
def conflicts_with(self, r: PackageRequest):
for p in self._pkgs:
if p.conflicts_with(r):
return True
return False
def additive_merged(self, rhs: "PackageList") -> "PackageList":
"""
Merge self with PackageList rhs, adding any packages from rhs that
are not present in self
"""
d = dict(zip([p.name for p in self._pkgs], self._pkgs))
for p in rhs:
if p.name in d.keys():
m = p.merged(d[p.name])
if m is None:
raise VersionConflict(f"Cannot merge {p} with {d[p.name]}")
d[p.name] = PackageRequest(f"{m.name}-{m.range}")
else:
d[p.name] = p
return PackageList([p for _, p in d.items()])
def merged(self, rhs: "PackageList") -> "PackageList":
"""
Merge self with PackageList rhs, ignoring any packages from rhs that
are not present in both lists
"""
d = dict(zip([p.name for p in self._pkgs], self._pkgs))
result = []
for p in rhs:
if p.name in d.keys():
m = p.merged(d[p.name])
if m is None:
raise VersionConflict(f"Cannot merge {p} with {d[p.name]}")
result.append(PackageRequest(f"{m.name}-{m.range}"))
return PackageList(result)
def merged_into(self, rhs: "PackageList") -> "PackageList":
"""
Merge self with PackageList rhs, ignoring any packages from rhs that
are not present in self, and preserving any packages in self that are not
present in rhs
"""
d = dict(zip([p.name for p in rhs._pkgs], rhs._pkgs))
result = []
for p in self._pkgs:
if p.name in d.keys():
m = p.merged(d[p.name])
if m is None:
raise VersionConflict(f"Cannot merge {p} with {d[p.name]}")
result.append(PackageRequest(f"{m.name}-{m.range}"))
else:
result.append(p)
return PackageList(result)
def constrained(self, rhs: PackageRequest):
result = []
for p in self._pkgs:
if rhs.name == p.name:
result.append(PackageRequest(f"{p.name}-{p.range.intersection(rhs.range)}"))
else:
result.append(p)
return PackageList(result)
def add_constraint(self, rhs: PackageRequest):
result = []
found = False
for p in self._pkgs:
if rhs.name == p.name:
m = p.merged(rhs)
if m is None:
raise VersionConflict(f"Cannot merge {p} with {rhs}")
result.append(PackageRequest(f"{m.name}-{m.range}"))
found = True
else:
result.append(p)
if not found:
result.append(rhs)
self._pkgs = PackageList(result)
def __iter__(self):
for pkg in self._pkgs:
yield pkg
def __str__(self):
s = "["
first = True
for p in self._pkgs:
if not first:
s = f"{s}, "
first = False
if p.range.is_any():
s = f"{s}\"{p.name}\""
else:
s = f"{s}\"{p.name}-{p.range}\""
s += "]"
return s
def __len__(self):
return len(self._pkgs)
def __getitem__(self, i):
return self._pkgs[i]
def __add__(self, rhs):
return PackageList(self._pkgs + rhs._pkgs)
def __hash__(self):
return hash(str(self._pkgs))