|
| 1 | +#!/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import fileinput |
| 5 | +import sys |
| 6 | + |
| 7 | +from packaging.requirements import Requirement |
| 8 | +from packaging.version import Version |
| 9 | + |
| 10 | +try: |
| 11 | + import tomllib |
| 12 | +except ImportError: |
| 13 | + import tomli as tomllib |
| 14 | + |
| 15 | + |
| 16 | +def split_comment(line): |
| 17 | + split_line = line.split("#", maxsplit=1) |
| 18 | + try: |
| 19 | + comment = " # " + split_line[1].strip() |
| 20 | + except IndexError: |
| 21 | + comment = "" |
| 22 | + return split_line[0].strip(), comment |
| 23 | + |
| 24 | + |
| 25 | +def to_upper_bound(req): |
| 26 | + try: |
| 27 | + requirement = Requirement(req) |
| 28 | + except ValueError: |
| 29 | + return f"# UNPARSABLE: {req}" |
| 30 | + else: |
| 31 | + for spec in requirement.specifier: |
| 32 | + if spec.operator == "~=": |
| 33 | + return f"# NO BETTER CONSTRAINT: {req}" |
| 34 | + if spec.operator == "<=": |
| 35 | + operator = "==" |
| 36 | + max_version = spec.version |
| 37 | + return f"{requirement.name}{operator}{max_version}" |
| 38 | + if spec.operator == "<": |
| 39 | + operator = "~=" |
| 40 | + version = Version(spec.version) |
| 41 | + if version.micro != 0: |
| 42 | + max_version = f"{version.major}.{version.minor}.{version.micro - 1}" |
| 43 | + elif version.minor != 0: |
| 44 | + max_version = f"{version.major}.{version.minor - 1}" |
| 45 | + elif version.major != 0: |
| 46 | + max_version = f"{version.major - 1}.0" |
| 47 | + else: |
| 48 | + return f"# NO BETTER CONSTRAINT: {req}" |
| 49 | + return f"{requirement.name}{operator}{max_version}" |
| 50 | + return f"# NO UPPER BOUND: {req}" |
| 51 | + |
| 52 | + |
| 53 | +def to_lower_bound(req): |
| 54 | + try: |
| 55 | + requirement = Requirement(req) |
| 56 | + except ValueError: |
| 57 | + return f"# UNPARSABLE: {req}" |
| 58 | + else: |
| 59 | + for spec in requirement.specifier: |
| 60 | + if spec.operator == ">=": |
| 61 | + if requirement.name == "pulpcore": |
| 62 | + # Currently an exception to allow for pulpcore bugfix releases. |
| 63 | + # TODO Semver libraries should be allowed too. |
| 64 | + operator = "~=" |
| 65 | + else: |
| 66 | + operator = "==" |
| 67 | + min_version = spec.version |
| 68 | + return f"{requirement.name}{operator}{min_version}" |
| 69 | + return f"# NO LOWER BOUND: {req}" |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + """Calculate constraints for the lower bound of dependencies where possible.""" |
| 74 | + parser = argparse.ArgumentParser( |
| 75 | + prog=sys.argv[0], |
| 76 | + description="Calculate constraints for the lower or upper bound of dependencies where " |
| 77 | + "possible.", |
| 78 | + ) |
| 79 | + parser.add_argument("-u", "--upper", action="store_true") |
| 80 | + parser.add_argument("filename", nargs="*") |
| 81 | + args = parser.parse_args() |
| 82 | + |
| 83 | + modifier = to_upper_bound if args.upper else to_lower_bound |
| 84 | + |
| 85 | + req_files = [filename for filename in args.filename if not filename.endswith("pyproject.toml")] |
| 86 | + pyp_files = [filename for filename in args.filename if filename.endswith("pyproject.toml")] |
| 87 | + if req_files: |
| 88 | + with fileinput.input(files=req_files) as req_file: |
| 89 | + for line in req_file: |
| 90 | + if line.strip().startswith("#"): |
| 91 | + # Shortcut comment only lines |
| 92 | + print(line.strip()) |
| 93 | + else: |
| 94 | + req, comment = split_comment(line) |
| 95 | + new_req = modifier(req) |
| 96 | + print(new_req + comment) |
| 97 | + for filename in pyp_files: |
| 98 | + with open(filename, "rb") as fp: |
| 99 | + pyproject = tomllib.load(fp) |
| 100 | + for req in pyproject["project"]["dependencies"]: |
| 101 | + new_req = modifier(req) |
| 102 | + print(new_req) |
| 103 | + optional_dependencies = pyproject["project"].get("optional-dependencies") |
| 104 | + if optional_dependencies: |
| 105 | + for opt in optional_dependencies.values(): |
| 106 | + for req in opt: |
| 107 | + new_req = modifier(req) |
| 108 | + print(new_req) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
0 commit comments