Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add requirement calculator for artifacts in pypi #219

Merged
merged 13 commits into from
Apr 21, 2020
Merged
1 change: 1 addition & 0 deletions news/219.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``requirementslib.models.metadata`` module with ``get_package``, ``get_package_version``, and ``get_package_from_requirement`` interfaces.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ markers =
atomic = true
not_skip = __init__.py
line_length = 90
indent = ' '
multi_line_output = 3
known_third_party = appdirs,attr,cached_property,chardet,distlib,environ,hypothesis,invoke,orderedmultidict,packaging,parver,pep517,pip_shims,plette,pyparsing,pytest,requests,setuptools,six,tomlkit,towncrier,urllib3,vistir
known_third_party = appdirs,attr,cached_property,chardet,dateutil,distlib,environ,hypothesis,invoke,orderedmultidict,packaging,parver,pep517,pip_shims,plette,pyparsing,pytest,requests,setuptools,six,tomlkit,towncrier,urllib3,vistir
known_first_party = requirementslib,tests
combine_as_imports=True
include_trailing_comma = True
use_parentheses=True
force_grid_wrap=0

[flake8]
Expand Down
23 changes: 23 additions & 0 deletions src/requirementslib/models/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def _get_specs(specset):
return sorted(result, key=operator.itemgetter(1))


# TODO: Rename this to something meaningful
def _group_by_op(specs):
# type: (Union[Set[Specifier], SpecifierSet]) -> Iterator
specs = [_get_specs(x) for x in list(specs)]
Expand All @@ -208,6 +209,7 @@ def _group_by_op(specs):
return grouping


# TODO: rename this to something meaningful
def normalize_specifier_set(specs):
# type: (Union[str, SpecifierSet]) -> Optional[Set[Specifier]]
"""Given a specifier set, a string, or an iterable, normalize the specifiers
Expand Down Expand Up @@ -238,6 +240,8 @@ def normalize_specifier_set(specs):
return normalize_specifier_set(SpecifierSet(",".join(spec_list)))


# TODO: Check if this is used by anything public otherwise make it private
# And rename it to something meaningful
def get_sorted_version_string(version_set):
# type: (Set[AnyStr]) -> AnyStr
version_list = sorted(
Expand All @@ -247,6 +251,9 @@ def get_sorted_version_string(version_set):
return version


# TODO: Rename this to something meaningful
# TODO: Add a deprecation decorator and deprecate this -- i'm sure it's used
# in other libraries
@lru_cache(maxsize=1024)
def cleanup_pyspecs(specs, joiner="or"):
specs = normalize_specifier_set(specs)
Expand Down Expand Up @@ -292,6 +299,7 @@ def cleanup_pyspecs(specs, joiner="or"):
return sorted([(k[0], v) for k, v in results.items()], key=operator.itemgetter(1))


# TODO: Rename this to something meaningful
@lru_cache(maxsize=1024)
def fix_version_tuple(version_tuple):
# type: (Tuple[AnyStr, AnyStr]) -> Tuple[AnyStr, AnyStr]
Expand All @@ -306,6 +314,7 @@ def fix_version_tuple(version_tuple):
return (op, version)


# TODO: Rename this to something meaningful, deprecate it (See prior function)
@lru_cache(maxsize=128)
def get_versions(specset, group_by_operator=True):
# type: (Union[Set[Specifier], SpecifierSet], bool) -> List[Tuple[STRING_TYPE, STRING_TYPE]]
Expand Down Expand Up @@ -594,6 +603,7 @@ def get_specset(marker_list):
return specifiers


# TODO: Refactor this (reduce complexity)
def parse_marker_dict(marker_dict):
op = marker_dict["op"]
lhs = marker_dict["lhs"]
Expand Down Expand Up @@ -710,3 +720,16 @@ def marker_from_specifier(spec):
marker_segments.append(format_pyversion(marker_segment))
marker_str = " and ".join(marker_segments).replace('"', "'")
return Marker(marker_str)


def merge_markers(m1, m2):
# type: (Marker, Marker) -> Optional[Marker]
if not all((m1, m2)):
return next(iter(v for v in (m1, m2) if v), None)
m1 = _ensure_marker(m1)
m2 = _ensure_marker(m2)
_markers = [] # type: List[Marker]
for marker in (m1, m2):
_markers.append(str(marker))
marker_str = " and ".join([normalize_marker_str(m) for m in _markers if m])
return _ensure_marker(normalize_marker_str(marker_str))
Loading