|
| 1 | +# WARNING: DO NOT EDIT! |
| 2 | +# |
| 3 | +# This file was generated by plugin_template, and is managed by it. Please use |
| 4 | +# './plugin-template --github pulp_python' to update this file. |
| 5 | +# |
| 6 | +# For more info visit https://github.com/pulp/plugin_template |
| 7 | + |
| 8 | +import argparse |
| 9 | +import fileinput |
| 10 | +import urllib.request |
| 11 | +import sys |
| 12 | +from packaging.requirements import Requirement |
| 13 | +from packaging.version import Version |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +CORE_TEMPLATE_URL = "https://raw.githubusercontent.com/pulp/pulpcore/main/template_config.yml" |
| 18 | + |
| 19 | + |
| 20 | +def fetch_pulpcore_upper_bound(requirement): |
| 21 | + with urllib.request.urlopen(CORE_TEMPLATE_URL) as f: |
| 22 | + template = yaml.safe_load(f.read()) |
| 23 | + supported_versions = template["supported_release_branches"] |
| 24 | + supported_versions.append(template["latest_release_branch"]) |
| 25 | + applicable_versions = sorted( |
| 26 | + requirement.specifier.filter((Version(v) for v in supported_versions)) |
| 27 | + ) |
| 28 | + if len(applicable_versions) == 0: |
| 29 | + raise Exception("No supported pulpcore version in required range.") |
| 30 | + return f"{requirement.name}~={applicable_versions[-1]}" |
| 31 | + |
| 32 | + |
| 33 | +def split_comment(line): |
| 34 | + split_line = line.split("#", maxsplit=1) |
| 35 | + try: |
| 36 | + comment = " # " + split_line[1].strip() |
| 37 | + except IndexError: |
| 38 | + comment = "" |
| 39 | + return split_line[0].strip(), comment |
| 40 | + |
| 41 | + |
| 42 | +def to_upper_bound(req): |
| 43 | + try: |
| 44 | + requirement = Requirement(req) |
| 45 | + except ValueError: |
| 46 | + return f"# UNPARSABLE: {req}" |
| 47 | + else: |
| 48 | + if requirement.name == "pulpcore": |
| 49 | + # An exception to allow for pulpcore deprecation policy. |
| 50 | + return fetch_pulpcore_upper_bound(requirement) |
| 51 | + for spec in requirement.specifier: |
| 52 | + if spec.operator == "~=": |
| 53 | + return f"# NO BETTER CONSTRAINT: {req}" |
| 54 | + if spec.operator == "<=": |
| 55 | + operator = "==" |
| 56 | + max_version = spec.version |
| 57 | + return f"{requirement.name}{operator}{max_version}" |
| 58 | + if spec.operator == "<": |
| 59 | + operator = "~=" |
| 60 | + version = Version(spec.version) |
| 61 | + if version.micro != 0: |
| 62 | + max_version = f"{version.major}.{version.minor}.{version.micro-1}" |
| 63 | + elif version.minor != 0: |
| 64 | + max_version = f"{version.major}.{version.minor-1}" |
| 65 | + else: |
| 66 | + return f"# NO BETTER CONSTRAINT: {req}" |
| 67 | + return f"{requirement.name}{operator}{max_version}" |
| 68 | + return f"# NO UPPER BOUND: {req}" |
| 69 | + |
| 70 | + |
| 71 | +def to_lower_bound(req): |
| 72 | + try: |
| 73 | + requirement = Requirement(req) |
| 74 | + except ValueError: |
| 75 | + return f"# UNPARSABLE: {req}" |
| 76 | + else: |
| 77 | + for spec in requirement.specifier: |
| 78 | + if spec.operator == ">=": |
| 79 | + if requirement.name == "pulpcore": |
| 80 | + # Currently an exception to allow for pulpcore bugfix releases. |
| 81 | + # TODO Semver libraries should be allowed too. |
| 82 | + operator = "~=" |
| 83 | + else: |
| 84 | + operator = "==" |
| 85 | + min_version = spec.version |
| 86 | + return f"{requirement.name}{operator}{min_version}" |
| 87 | + return f"# NO LOWER BOUND: {req}" |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + """Calculate constraints for the lower bound of dependencies where possible.""" |
| 92 | + parser = argparse.ArgumentParser( |
| 93 | + prog=sys.argv[0], |
| 94 | + description="Calculate constraints for the lower or upper bound of dependencies where " |
| 95 | + "possible.", |
| 96 | + ) |
| 97 | + parser.add_argument("-u", "--upper", action="store_true") |
| 98 | + parser.add_argument("filename", nargs="*") |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + with fileinput.input(files=args.filename) as req_file: |
| 102 | + for line in req_file: |
| 103 | + if line.strip().startswith("#"): |
| 104 | + # Shortcut comment only lines |
| 105 | + print(line.strip()) |
| 106 | + else: |
| 107 | + req, comment = split_comment(line) |
| 108 | + if args.upper: |
| 109 | + new_req = to_upper_bound(req) |
| 110 | + else: |
| 111 | + new_req = to_lower_bound(req) |
| 112 | + print(new_req + comment) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments