|
5 | 5 | #
|
6 | 6 | # For more info visit https://github.com/pulp/plugin_template
|
7 | 7 |
|
| 8 | +import tomllib |
8 | 9 | import warnings
|
9 |
| -from pkg_resources import Requirement |
| 10 | +from packaging.requirements import Requirement |
10 | 11 |
|
11 | 12 |
|
12 | 13 | CHECK_MATRIX = [
|
| 14 | + ("pyproject.toml", True, True, True), |
13 | 15 | ("requirements.txt", True, True, True),
|
14 | 16 | ("dev_requirements.txt", False, True, False),
|
15 | 17 | ("ci_requirements.txt", False, True, True),
|
|
20 | 22 | ("clitest_requirements.txt", False, True, True),
|
21 | 23 | ]
|
22 | 24 |
|
23 |
| -errors = [] |
24 | 25 |
|
25 |
| -for filename, check_upperbound, check_prereleases, check_r in CHECK_MATRIX: |
26 |
| - try: |
| 26 | +def iterate_file(filename): |
| 27 | + if filename == "pyproject.toml": |
| 28 | + with open(filename, "rb") as fd: |
| 29 | + pyproject_toml = tomllib.load(fd) |
| 30 | + if "project" in pyproject_toml: |
| 31 | + for nr, line in enumerate(pyproject_toml["project"]["dependencies"]): |
| 32 | + yield nr, line |
| 33 | + else: |
27 | 34 | with open(filename, "r") as fd:
|
28 | 35 | for nr, line in enumerate(fd.readlines()):
|
29 | 36 | line = line.strip()
|
30 | 37 | if not line or line.startswith("#"):
|
31 | 38 | continue
|
| 39 | + if "#" in line: |
| 40 | + line = line.split("#", maxsplit=1)[0] |
| 41 | + yield nr, line.strip() |
| 42 | + |
| 43 | + |
| 44 | +def main(): |
| 45 | + errors = [] |
| 46 | + |
| 47 | + for filename, check_upperbound, check_prereleases, check_r in CHECK_MATRIX: |
| 48 | + try: |
| 49 | + for nr, line in iterate_file(filename): |
32 | 50 | try:
|
33 |
| - req = Requirement.parse(line) |
| 51 | + req = Requirement(line) |
34 | 52 | except ValueError:
|
35 | 53 | if line.startswith("git+"):
|
36 | 54 | # The single exception...
|
|
49 | 67 | and req.name != "pulp-python-client"
|
50 | 68 | ):
|
51 | 69 | errors.append(f"{filename}:{nr}: Prerelease versions found in {line}.")
|
52 |
| - ops = [op for op, ver in req.specs] |
53 |
| - spec = str(req.specs) |
| 70 | + ops = [spec.operator for spec in req.specifier] |
54 | 71 | if "~=" in ops:
|
55 | 72 | warnings.warn(f"{filename}:{nr}: Please avoid using ~= on {req.name}!")
|
56 | 73 | elif "<" not in ops and "<=" not in ops and "==" not in ops:
|
57 | 74 | if check_upperbound:
|
58 | 75 | errors.append(f"{filename}:{nr}: Upper bound missing in {line}.")
|
59 |
| - except FileNotFoundError: |
60 |
| - # skip this test for plugins that don't use this requirements.txt |
61 |
| - pass |
| 76 | + except FileNotFoundError: |
| 77 | + # skip this test for plugins that don't use this requirements.txt |
| 78 | + pass |
| 79 | + |
| 80 | + if errors: |
| 81 | + print("Dependency issues found:") |
| 82 | + print("\n".join(errors)) |
| 83 | + exit(1) |
| 84 | + |
62 | 85 |
|
63 |
| -if errors: |
64 |
| - print("Dependency issues found:") |
65 |
| - print("\n".join(errors)) |
66 |
| - exit(1) |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments