-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters