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

Utilize packaging.version.Version #220

Merged
merged 13 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
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
39 changes: 34 additions & 5 deletions ci_cd/tasks/update_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from invoke import task
from packaging.markers import default_environment
from packaging.requirements import InvalidRequirement, Requirement
from packaging.version import InvalidVersion, Version
from tomlkit.exceptions import TOMLKitError

from ci_cd.exceptions import InputError, UnableToResolve
Expand Down Expand Up @@ -44,6 +45,18 @@
LOGGER = logging.getLogger(__name__)


VALID_PACKAGE_NAME_PATTERN = r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$"
"""
Pattern to validate package names.

This is a valid non-normalized name, i.e., it can contain capital letters and
underscores, periods, and multiples of these, including minus characters.

See PEP 508 for more information, as well as the packaging documentation:
https://packaging.python.org/en/latest/specifications/name-normalization/
"""


def _format_and_update_dependency(
requirement: Requirement, raw_dependency_line: str, pyproject_path: Path
) -> None:
Expand Down Expand Up @@ -269,8 +282,7 @@
)
package_latest_version_line = out.stdout.split(sep="\n", maxsplit=1)[0]
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
match = re.match(
r"(?P<package>[a-zA-Z0-9-_]+) \((?P<version>[0-9]+(?:\.[0-9]+){0,2})\)",
package_latest_version_line,
r"(?P<package>\S+) \((?P<version>\S+)\)", package_latest_version_line
)
if match is None:
msg = (
Expand All @@ -285,7 +297,21 @@
error = True
continue

latest_version: str = match.group("version")
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
try:
latest_version = Version(match.group("version"))
except InvalidVersion as exc:
msg = (

Check warning on line 303 in ci_cd/tasks/update_deps.py

View check run for this annotation

Codecov / codecov/patch

ci_cd/tasks/update_deps.py#L302-L303

Added lines #L302 - L303 were not covered by tests
f"Could not parse version {match.group('version')!r} from 'pip index "
f"versions' output for line:\n {package_latest_version_line}.\n"
f"Exception: {exc}"
)
LOGGER.error(msg)
if fail_fast:
sys.exit(f"{Emoji.CROSS_MARK.value} {error_msg(msg)}")
print(error_msg(msg), file=sys.stderr, flush=True)
error = True
continue

Check warning on line 313 in ci_cd/tasks/update_deps.py

View check run for this annotation

Codecov / codecov/patch

ci_cd/tasks/update_deps.py#L308-L313

Added lines #L308 - L313 were not covered by tests
LOGGER.debug("Retrieved latest version: %r", latest_version)

# Here used to be a sanity check to ensure that the package name parsed from
# pyproject.toml matches the name returned from 'pip index versions'.
Expand All @@ -299,7 +325,7 @@
# Check whether pyproject.toml already uses the latest version
# This is expected if the latest version equals a specifier with any of the
# operators: ==, >=, or ~=.
split_latest_version = latest_version.split(".")
split_latest_version = latest_version.base_version.split(".")
_continue = False
for specifier in parsed_requirement.specifier:
if specifier.operator in ["==", ">=", "~="]:
Expand Down Expand Up @@ -365,7 +391,10 @@
current_version = specifier.version.split(".")
break
else:
CasperWA marked this conversation as resolved.
Show resolved Hide resolved
current_version = "0.0.0".split(".")
if latest_version.epoch == 0:
current_version = "0.0.0".split(".")
else:
current_version = f"{latest_version.epoch}!0.0.0".split(".")

if ignore_version(
current=current_version,
Expand Down
Loading