Skip to content

Commit

Permalink
Add upper bounds check
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Oct 10, 2024
1 parent 8c16ceb commit 0621810
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
110 changes: 110 additions & 0 deletions .ci/scripts/calc_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/python3

import argparse
import fileinput
import sys

from packaging.requirements import Requirement
from packaging.version import Version

try:
import tomllib
except ImportError:
import tomli as tomllib


def split_comment(line):
split_line = line.split("#", maxsplit=1)
try:
comment = " # " + split_line[1].strip()
except IndexError:
comment = ""
return split_line[0].strip(), comment


def to_upper_bound(req):
try:
requirement = Requirement(req)
except ValueError:
return f"# UNPARSABLE: {req}"
else:
for spec in requirement.specifier:
if spec.operator == "~=":
return f"# NO BETTER CONSTRAINT: {req}"
if spec.operator == "<=":
operator = "=="
max_version = spec.version
return f"{requirement.name}{operator}{max_version}"
if spec.operator == "<":
operator = "~="
version = Version(spec.version)
if version.micro != 0:
max_version = f"{version.major}.{version.minor}.{version.micro - 1}"
elif version.minor != 0:
max_version = f"{version.major}.{version.minor - 1}"
elif version.major != 0:
max_version = f"{version.major - 1}.0"
else:
return f"# NO BETTER CONSTRAINT: {req}"
return f"{requirement.name}{operator}{max_version}"
return f"# NO UPPER BOUND: {req}"


def to_lower_bound(req):
try:
requirement = Requirement(req)
except ValueError:
return f"# UNPARSABLE: {req}"
else:
for spec in requirement.specifier:
if spec.operator == ">=":
if requirement.name == "pulpcore":
# Currently an exception to allow for pulpcore bugfix releases.
# TODO Semver libraries should be allowed too.
operator = "~="
else:
operator = "=="
min_version = spec.version
return f"{requirement.name}{operator}{min_version}"
return f"# NO LOWER BOUND: {req}"


def main():
"""Calculate constraints for the lower bound of dependencies where possible."""
parser = argparse.ArgumentParser(
prog=sys.argv[0],
description="Calculate constraints for the lower or upper bound of dependencies where "
"possible.",
)
parser.add_argument("-u", "--upper", action="store_true")
parser.add_argument("filename", nargs="*")
args = parser.parse_args()

modifier = to_upper_bound if args.upper else to_lower_bound

req_files = [filename for filename in args.filename if not filename.endswith("pyproject.toml")]
pyp_files = [filename for filename in args.filename if filename.endswith("pyproject.toml")]
if req_files:
with fileinput.input(files=req_files) as req_file:
for line in req_file:
if line.strip().startswith("#"):
# Shortcut comment only lines
print(line.strip())
else:
req, comment = split_comment(line)
new_req = modifier(req)
print(new_req + comment)
for filename in pyp_files:
with open(filename, "rb") as fp:
pyproject = tomllib.load(fp)
for req in pyproject["project"]["dependencies"]:
new_req = modifier(req)
print(new_req)
for opt in pyproject["project"]["optional-dependencies"].values():
for req in opt:
new_req = modifier(req)
print(new_req)


if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
python: "3.11"
- image_tag: "latest"
python: "3.11"
upper_bounds: true
- image_tag: "3.61"
pulp_https: "true"
pulp_oauth2: "true"
Expand Down Expand Up @@ -64,6 +65,10 @@ jobs:
if [ "${{matrix.lower_bounds}}" ]
then
pip install dist/pulp_cli-*.whl pulp-glue/dist/pulp_glue-*.whl -r test_requirements.txt -c lower_bounds_constraints.lock
elif [ "${{matrix.upper_bounds}}" ]
then
.ci/scripts/calc_constraints.py pyproject.toml --upper > upper_bounds_constraints.lock
pip install dist/pulp_cli-*.whl pulp-glue/dist/pulp_glue-*.whl -r test_requirements.txt -c upper_bounds_constraints.lock
else
pip install dist/pulp_cli-*.whl pulp-glue/dist/pulp_glue-*.whl -r test_requirements.txt
fi
Expand Down

0 comments on commit 0621810

Please sign in to comment.