Skip to content

Commit

Permalink
Merge pull request #104 from albertyw/update-compare-versions
Browse files Browse the repository at this point in the history
Fix finding updated docker versions
  • Loading branch information
albertyw committed Oct 14, 2023
2 parents da2814b + be01c03 commit 52b86dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions req_update/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Copied and simplified from
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER = r'^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)$' # NOQA
SEMVER = re.compile(r'^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)$') # NOQA


class Node(Updater):
Expand Down Expand Up @@ -129,7 +129,7 @@ def generate_package_version(version: str) -> str:
Given a version, generate a version specifier that allows updates
within the most recent non-zero version for semver versions
"""
match = re.match(SEMVER, version)
match = SEMVER.match(version)
if not match:
return version
versions = match.groupdict()
Expand Down
9 changes: 4 additions & 5 deletions req_update/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
PYTHON_PACKAGE_OPERATOR_REGEX = r'(?P<operator>[<=>]+)'
PYTHON_PACKAGE_VERSION_REGEX = r'(?P<version>(\d+!)?(\d+)(\.\d+)+([\.\-\_])?((a(lpha)?|b(eta)?|c|r(c|ev)?|pre(view)?)\d*)?(\.?(post|dev)\d*)?)' # noqa
PYTHON_PACKAGE_SPACER_REGEX = r'(?P<spacer>([ ]+\#)?)'
PYTHON_REQUIREMENTS_LINE_REGEX = r'^%s%s%s%s' % (
PYTHON_REQUIREMENTS_LINE_REGEX = re.compile('^%s%s%s%s' % (
PYTHON_PACKAGE_NAME_REGEX,
PYTHON_PACKAGE_OPERATOR_REGEX,
PYTHON_PACKAGE_VERSION_REGEX,
PYTHON_PACKAGE_SPACER_REGEX,
)
))
REQUIREMENTS_FILES = [
'requirements.txt',
'requirements-test.txt',
Expand Down Expand Up @@ -136,7 +136,7 @@ def write_dependency_update_lines(
"""
dependency = dependency.replace('_', '-')
for i, line in enumerate(lines):
match = re.match(PYTHON_REQUIREMENTS_LINE_REGEX, line)
match = PYTHON_REQUIREMENTS_LINE_REGEX.match(line)
if not match:
continue
dependency_file = match.group('name').replace('_', '-').lower()
Expand All @@ -152,8 +152,7 @@ def write_dependency_update_lines(
spacer = ' ' * (spacing - 1) + '#'
else:
spacer = ''
new_line = re.sub(
PYTHON_REQUIREMENTS_LINE_REGEX,
new_line = PYTHON_REQUIREMENTS_LINE_REGEX.sub(
r'\g<1>\g<2>%s%s' % (version, spacer),
line,
)
Expand Down
7 changes: 5 additions & 2 deletions req_update/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def setUp(self) -> None:
self.util = util.Util()

def test_ints(self) -> None:
self.assertFalse(self.util.compare_versions('a10', 'a12'))
self.assertFalse(self.util.compare_versions('10a', '12a'))
self.assertTrue(self.util.compare_versions('a10', 'a12'))
self.assertTrue(self.util.compare_versions('10a', '12a'))

def test_regex(self) -> None:
tests: dict[str, list[str]] = {
Expand All @@ -197,6 +197,9 @@ def test_regex(self) -> None:
'1.21': ['1.19', '1.2'],
'18-slim': ['16-slim', '15-slim'],
'3.11-slim-bookworm': ['3.10-slim-bookworm', '3.9-slim-bookworm'],
'2.12.0': ['1.17.7'],
'v2.12.0': ['v1.17.7'],
'v2.12.0-alpine': ['v1.17.7-alpine'],
}
for newest, olders in tests.items():
for older in olders:
Expand Down
14 changes: 8 additions & 6 deletions req_update/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,19 @@ def compare_versions(self, current: str, proposed: str) -> bool:
valid upgrade if the version structure matches and the version numbers
are greater.
"""
structure_regex = r"[0-9]+"
current_structure = re.sub(structure_regex, "", current)
proposed_structure = re.sub(structure_regex, "", proposed)
structure_regex = re.compile(r"[0-9]+")
current_structure = structure_regex.sub("", current)
proposed_structure = structure_regex.sub("", proposed)
if current_structure != proposed_structure:
return False
num_regex = r"(\.|^)([0-9]+)(?![a-zA-Z])"
current_nums = [found[1] for found in re.findall(num_regex, current)]
proposed_nums = [found[1] for found in re.findall(num_regex, proposed)]
num_regex = re.compile(r"\d+")
current_nums = num_regex.findall(current)
proposed_nums = num_regex.findall(proposed)
for compares in zip(current_nums, proposed_nums):
if int(compares[0]) < int(compares[1]):
return True
if int(compares[0]) > int(compares[1]):
return False
return False

def check_major_version_update(
Expand Down

0 comments on commit 52b86dd

Please sign in to comment.