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

lib.shpypi: calculate versions for requiremens above/below given version #658

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions lib/shpypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,62 @@ def _split_requirement_to_min_max(self, requirement):
reqs = requirement.split(',')
for req in reqs:
operator, version = self._split_operator(req)
if operator in ['>=', '==', '>']:
if operator in ['>=', '==']:
result['min'] = version
if operator in ['<', '<=', '==']:
if operator in ['<=', '==']:
result['max'] = version
if operator == '>':
result['min'] = self._get_bounding_version(version, below=True)
if operator == '<':
result['max'] = self._get_bounding_version(version, above=True)

return result

def _get_bounding_version(self, version: str, below: bool = False, above: bool = False) -> str:
"""
calculate bounding version number to store requirements like
version < 2.0 --> bounding version will be 1.999
version < 2.2.0 --> bounding version will be 2.1.999

If below == above, version is returned unchanged

:param version: version number
:param below: True means bounding version below given version
:param above: True means bounding version above given version
:type version: str
:type below: bool
:type above: bool

:return: modified version number
:rtype: str
"""

def _to_num(vl: list) -> int:
""" convert version number in list form to 1000-based decimal """
vi = int(vl[0])
for d in vl[1:]:
vi = vi * 1000
vi = vi + int(d)
return vi

def _to_str(vi: int) -> str:
""" convert 1000-based decimal to reduced string """
vl = []
vs = str(vi)
while vs:
vl = [str(int(vs[-3:]))] + vl
vs = vs[:-3]
return ".".join(vl)

if above == below:
return version
vers_org = self._version_to_list(version)
vers_num = _to_num(vers_org)
if above:
vers_num += 1
if below:
vers_num -= 1
return _to_str(vers_num)


def _compare_versions(self, vers1, vers2, operator):
Expand Down
Loading