-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
term.py
192 lines (155 loc) · 6.93 KB
/
term.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
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
from __future__ import annotations
import functools
from typing import TYPE_CHECKING
from poetry.mixology.set_relation import SetRelation
if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency
from poetry.core.semver.version_constraint import VersionConstraint
class Term:
"""
A statement about a package which is true or false for a given selection of
package versions.
See https://github.com/dart-lang/pub/tree/master/doc/solver.md#term.
"""
def __init__(self, dependency: Dependency, is_positive: bool) -> None:
self._dependency = dependency
self._positive = is_positive
self.relation = functools.lru_cache(maxsize=None)(self._relation)
self.intersect = functools.lru_cache(maxsize=None)(self._intersect)
@property
def inverse(self) -> Term:
return Term(self._dependency, not self.is_positive())
@property
def dependency(self) -> Dependency:
return self._dependency
@property
def constraint(self) -> VersionConstraint:
return self._dependency.constraint
def is_positive(self) -> bool:
return self._positive
def satisfies(self, other: Term) -> bool:
"""
Returns whether this term satisfies another.
"""
return (
self.dependency.complete_name == other.dependency.complete_name
and self.relation(other) == SetRelation.SUBSET
)
def _relation(self, other: Term) -> str:
"""
Returns the relationship between the package versions
allowed by this term and another.
"""
if self.dependency.complete_name != other.dependency.complete_name:
raise ValueError(f"{other} should refer to {self.dependency.complete_name}")
other_constraint = other.constraint
if other.is_positive():
if self.is_positive():
if not self._compatible_dependency(other.dependency):
return SetRelation.DISJOINT
# foo ^1.5.0 is a subset of foo ^1.0.0
if other_constraint.allows_all(self.constraint):
return SetRelation.SUBSET
# foo ^2.0.0 is disjoint with foo ^1.0.0
if not self.constraint.allows_any(other_constraint):
return SetRelation.DISJOINT
return SetRelation.OVERLAPPING
else:
if not self._compatible_dependency(other.dependency):
return SetRelation.OVERLAPPING
# not foo ^1.0.0 is disjoint with foo ^1.5.0
if self.constraint.allows_all(other_constraint):
return SetRelation.DISJOINT
# not foo ^1.5.0 overlaps foo ^1.0.0
# not foo ^2.0.0 is a superset of foo ^1.5.0
return SetRelation.OVERLAPPING
else:
if self.is_positive():
if not self._compatible_dependency(other.dependency):
return SetRelation.SUBSET
# foo ^2.0.0 is a subset of not foo ^1.0.0
if not other_constraint.allows_any(self.constraint):
return SetRelation.SUBSET
# foo ^1.5.0 is disjoint with not foo ^1.0.0
if other_constraint.allows_all(self.constraint):
return SetRelation.DISJOINT
# foo ^1.0.0 overlaps not foo ^1.5.0
return SetRelation.OVERLAPPING
else:
if not self._compatible_dependency(other.dependency):
return SetRelation.OVERLAPPING
# not foo ^1.0.0 is a subset of not foo ^1.5.0
if self.constraint.allows_all(other_constraint):
return SetRelation.SUBSET
# not foo ^2.0.0 overlaps not foo ^1.0.0
# not foo ^1.5.0 is a superset of not foo ^1.0.0
return SetRelation.OVERLAPPING
def _intersect(self, other: Term) -> Term | None:
"""
Returns a Term that represents the packages
allowed by both this term and another
"""
if self.dependency.complete_name != other.dependency.complete_name:
raise ValueError(f"{other} should refer to {self.dependency.complete_name}")
if self._compatible_dependency(other.dependency):
if self.is_positive() != other.is_positive():
# foo ^1.0.0 ∩ not foo ^1.5.0 → foo >=1.0.0 <1.5.0
positive = self if self.is_positive() else other
negative = other if self.is_positive() else self
return self._non_empty_term(
positive.constraint.difference(negative.constraint), True, other
)
elif self.is_positive():
# foo ^1.0.0 ∩ foo >=1.5.0 <3.0.0 → foo ^1.5.0
return self._non_empty_term(
self.constraint.intersect(other.constraint), True, other
)
else:
# not foo ^1.0.0 ∩ not foo >=1.5.0 <3.0.0 → not foo >=1.0.0 <3.0.0
return self._non_empty_term(
self.constraint.union(other.constraint), False, other
)
elif self.is_positive() != other.is_positive():
return self if self.is_positive() else other
else:
return None
def difference(self, other: Term) -> Term | None:
"""
Returns a Term that represents packages
allowed by this term and not by the other
"""
return self.intersect(other.inverse)
@staticmethod
def _is_direct_origin(dependency: Dependency) -> bool:
return dependency.source_type in ["directory", "file", "url", "git"]
def _compatible_dependency(self, other: Dependency) -> bool:
return (
self.dependency.is_root
or other.is_root
or other.is_same_package_as(self.dependency)
or (
# we do this here to indicate direct origin dependencies are
# compatible with NVR dependencies
self.dependency.complete_name == other.complete_name
and self._is_direct_origin(self.dependency)
!= self._is_direct_origin(other)
)
)
def _non_empty_term(
self, constraint: VersionConstraint, is_positive: bool, other: Term
) -> Term | None:
if constraint.is_empty():
return None
# when creating a new term prefer direct-reference dependencies
dependency = (
other.dependency
if not self._is_direct_origin(self.dependency)
and self._is_direct_origin(other.dependency)
else self.dependency
)
return Term(dependency.with_constraint(constraint), is_positive)
def __str__(self) -> str:
prefix = "not " if not self.is_positive() else ""
return f"{prefix}{self._dependency}"
def __repr__(self) -> str:
return f"<Term {self!s}>"