Skip to content

Commit 5227a3d

Browse files
committed
add native github ranges support
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 97f31ad commit 5227a3d

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/univers/version_range.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,56 @@ def from_native(cls, string):
899899
return cls(constraints=constraints)
900900

901901

902+
vers_by_github_native_comparators = {
903+
"==": "=",
904+
"<=": "<=",
905+
">=": ">=",
906+
"<": "<",
907+
">": ">",
908+
}
909+
910+
911+
def build_constraint_from_github_advisory_string(scheme: str, string: str):
912+
"""
913+
Return a VersionConstraint built from a single github-native version
914+
relationship ``string``.
915+
916+
>>> vr = build_github_constraint_from_string("gem","<= 2.24")
917+
>>> assert str(vr) == "<=2.24", str(vr)
918+
>>> vr = build_github_constraint_from_string("pypi","< 9.0")
919+
>>> assert str(vr) == "<9.0", str(vr)
920+
"""
921+
vrc = RANGE_CLASS_BY_SCHEMES.get(scheme)
922+
comparator, version = split_req(
923+
string=string,
924+
comparators=vers_by_github_native_comparators,
925+
)
926+
version = vrc.version_class(version)
927+
return VersionConstraint(comparator=comparator, version=version)
928+
929+
930+
def build_range_from_github_advisory_constraint(scheme: str, string: str):
931+
"""
932+
Return a VersionRange built from a ``string`` single github-native
933+
version relationship string.
934+
935+
For example::
936+
937+
>>> vr = build_range_from_github_advisory_constraint("maven", ">= 2.13.0, < 2.16.0")
938+
>>> assert str(vr) == "vers:maven/>=2.13.0|<2.16.0"
939+
>>> vr = build_range_from_github_advisory_constraint("gem", ">= 2.13.0, < 2.16.0")
940+
>>> assert str(vr) == "vers:gem/>=2.13.0|<2.16.0"
941+
>>> vr = build_range_from_github_advisory_constraint("pypi","< 9.0")
942+
>>> assert str(vr) == "vers:pypi/<9.0"
943+
"""
944+
constraint_strings = string.split(",")
945+
constraints = []
946+
vrc = RANGE_CLASS_BY_SCHEMES.get(scheme)
947+
for constraint in constraint_strings:
948+
constraints.append(build_github_constraint_from_string(scheme, constraint))
949+
return vrc(constraints=constraints)
950+
951+
902952
def is_even(s):
903953
"""
904954
Return True if the string "s" is an even number and False if this is an odd

0 commit comments

Comments
 (0)