req-pincheck 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- req_pincheck-0.1.0/LICENSE +21 -0
- req_pincheck-0.1.0/PKG-INFO +88 -0
- req_pincheck-0.1.0/README.md +70 -0
- req_pincheck-0.1.0/pyproject.toml +28 -0
- req_pincheck-0.1.0/setup.cfg +4 -0
- req_pincheck-0.1.0/src/req_pincheck/__init__.py +6 -0
- req_pincheck-0.1.0/src/req_pincheck/__main__.py +5 -0
- req_pincheck-0.1.0/src/req_pincheck/cli.py +54 -0
- req_pincheck-0.1.0/src/req_pincheck/core.py +143 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/PKG-INFO +88 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/SOURCES.txt +14 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/dependency_links.txt +1 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/entry_points.txt +2 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/requires.txt +1 -0
- req_pincheck-0.1.0/src/req_pincheck.egg-info/top_level.txt +1 -0
- req_pincheck-0.1.0/tests/test_req_pincheck.py +78 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The req-pincheck authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: req-pincheck
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lint requirements.txt files for unpinned or loosely pinned dependencies, with CI-friendly exit codes.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: requirements,pip,pinning,dependencies,lint,ci,reproducibility
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
11
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: packaging>=22
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# req-pincheck
|
|
20
|
+
|
|
21
|
+
Reproducible builds die quietly when someone adds `requests` (no version) or
|
|
22
|
+
`django>=4` to a requirements file. `req-pincheck` lints `requirements*.txt`
|
|
23
|
+
files and fails CI when dependencies aren't pinned to the level you require.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install req-pincheck
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
req-pincheck requirements.txt # every dep must be pinned with == (default)
|
|
35
|
+
req-pincheck --level bounded requirements.txt # allow ranges, but require an upper bound (<, <=, ~=, ==)
|
|
36
|
+
req-pincheck --level any requirements.txt # just require *some* version specifier
|
|
37
|
+
req-pincheck --ignore pip --ignore setuptools requirements*.txt
|
|
38
|
+
req-pincheck --json requirements.txt
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Example output:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
requirements.txt:3: requests — unpinned (no version specifier)
|
|
45
|
+
requirements.txt:5: django>=4.2 — not an exact pin (needs ==)
|
|
46
|
+
requirements.txt:9: numpy==1.* — wildcard pin (needs exact version)
|
|
47
|
+
3 problem(s) in 1 file(s)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Levels
|
|
51
|
+
|
|
52
|
+
| level | passes | fails |
|
|
53
|
+
|-----------|--------------------------------|----------------------------|
|
|
54
|
+
| `exact` | `==1.2.3`, `===1.2.3` | everything else |
|
|
55
|
+
| `bounded` | `==`, `~=`, anything with `<`/`<=` | `>=1.0`, `!=1.1`, no spec |
|
|
56
|
+
| `any` | any specifier | no specifier |
|
|
57
|
+
|
|
58
|
+
### What it understands
|
|
59
|
+
|
|
60
|
+
- `-r other.txt` / `--requirement` includes (followed recursively, relative to the including file) and `-c` constraint files
|
|
61
|
+
- Environment markers (`foo==1.0; python_version < "3.10"`)
|
|
62
|
+
- Extras (`uvicorn[standard]==0.30.0`)
|
|
63
|
+
- Line continuations and `--hash=...` options
|
|
64
|
+
- Comments, blank lines and pip options (`--index-url`, etc.) are skipped
|
|
65
|
+
- Direct references (`pkg @ https://...`, `git+https://...`, local paths, `-e`) are reported as
|
|
66
|
+
`unversioned-url` — use `--allow-urls` to accept them
|
|
67
|
+
- Add `# pincheck: ignore` to a line to skip it
|
|
68
|
+
|
|
69
|
+
### Exit codes
|
|
70
|
+
|
|
71
|
+
| code | meaning |
|
|
72
|
+
|------|---------|
|
|
73
|
+
| 0 | everything pinned |
|
|
74
|
+
| 1 | at least one problem |
|
|
75
|
+
| 2 | a file could not be read |
|
|
76
|
+
|
|
77
|
+
## Python API
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from req_pincheck import check_file
|
|
81
|
+
|
|
82
|
+
for problem in check_file("requirements.txt", level="bounded"):
|
|
83
|
+
print(problem.path, problem.line, problem.name, problem.reason)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# req-pincheck
|
|
2
|
+
|
|
3
|
+
Reproducible builds die quietly when someone adds `requests` (no version) or
|
|
4
|
+
`django>=4` to a requirements file. `req-pincheck` lints `requirements*.txt`
|
|
5
|
+
files and fails CI when dependencies aren't pinned to the level you require.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install req-pincheck
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
req-pincheck requirements.txt # every dep must be pinned with == (default)
|
|
17
|
+
req-pincheck --level bounded requirements.txt # allow ranges, but require an upper bound (<, <=, ~=, ==)
|
|
18
|
+
req-pincheck --level any requirements.txt # just require *some* version specifier
|
|
19
|
+
req-pincheck --ignore pip --ignore setuptools requirements*.txt
|
|
20
|
+
req-pincheck --json requirements.txt
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Example output:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
requirements.txt:3: requests — unpinned (no version specifier)
|
|
27
|
+
requirements.txt:5: django>=4.2 — not an exact pin (needs ==)
|
|
28
|
+
requirements.txt:9: numpy==1.* — wildcard pin (needs exact version)
|
|
29
|
+
3 problem(s) in 1 file(s)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Levels
|
|
33
|
+
|
|
34
|
+
| level | passes | fails |
|
|
35
|
+
|-----------|--------------------------------|----------------------------|
|
|
36
|
+
| `exact` | `==1.2.3`, `===1.2.3` | everything else |
|
|
37
|
+
| `bounded` | `==`, `~=`, anything with `<`/`<=` | `>=1.0`, `!=1.1`, no spec |
|
|
38
|
+
| `any` | any specifier | no specifier |
|
|
39
|
+
|
|
40
|
+
### What it understands
|
|
41
|
+
|
|
42
|
+
- `-r other.txt` / `--requirement` includes (followed recursively, relative to the including file) and `-c` constraint files
|
|
43
|
+
- Environment markers (`foo==1.0; python_version < "3.10"`)
|
|
44
|
+
- Extras (`uvicorn[standard]==0.30.0`)
|
|
45
|
+
- Line continuations and `--hash=...` options
|
|
46
|
+
- Comments, blank lines and pip options (`--index-url`, etc.) are skipped
|
|
47
|
+
- Direct references (`pkg @ https://...`, `git+https://...`, local paths, `-e`) are reported as
|
|
48
|
+
`unversioned-url` — use `--allow-urls` to accept them
|
|
49
|
+
- Add `# pincheck: ignore` to a line to skip it
|
|
50
|
+
|
|
51
|
+
### Exit codes
|
|
52
|
+
|
|
53
|
+
| code | meaning |
|
|
54
|
+
|------|---------|
|
|
55
|
+
| 0 | everything pinned |
|
|
56
|
+
| 1 | at least one problem |
|
|
57
|
+
| 2 | a file could not be read |
|
|
58
|
+
|
|
59
|
+
## Python API
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from req_pincheck import check_file
|
|
63
|
+
|
|
64
|
+
for problem in check_file("requirements.txt", level="bounded"):
|
|
65
|
+
print(problem.path, problem.line, problem.name, problem.reason)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "req-pincheck"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Lint requirements.txt files for unpinned or loosely pinned dependencies, with CI-friendly exit codes."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["requirements", "pip", "pinning", "dependencies", "lint", "ci", "reproducibility"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Environment :: Console",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
19
|
+
"Topic :: Software Development :: Build Tools",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
]
|
|
22
|
+
dependencies = ["packaging>=22"]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
req-pincheck = "req_pincheck.cli:main"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
from dataclasses import asdict
|
|
7
|
+
from typing import List, Optional
|
|
8
|
+
|
|
9
|
+
from . import __version__
|
|
10
|
+
from .core import LEVELS, check_file
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
14
|
+
p = argparse.ArgumentParser(
|
|
15
|
+
prog="req-pincheck",
|
|
16
|
+
description="Fail when requirements files contain unpinned or loosely pinned dependencies.",
|
|
17
|
+
)
|
|
18
|
+
p.add_argument("files", nargs="*", default=["requirements.txt"],
|
|
19
|
+
help="requirements files to check (default: requirements.txt)")
|
|
20
|
+
p.add_argument("--level", choices=LEVELS, default="exact",
|
|
21
|
+
help="how strict pinning must be (default: exact)")
|
|
22
|
+
p.add_argument("--ignore", action="append", default=[], metavar="NAME",
|
|
23
|
+
help="package name to skip (repeatable)")
|
|
24
|
+
p.add_argument("--allow-urls", action="store_true",
|
|
25
|
+
help="accept direct URL / VCS / path / editable requirements")
|
|
26
|
+
p.add_argument("--json", action="store_true", help="print a JSON report")
|
|
27
|
+
p.add_argument("--quiet", "-q", action="store_true", help="print nothing; rely on the exit code")
|
|
28
|
+
p.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
29
|
+
args = p.parse_args(argv)
|
|
30
|
+
|
|
31
|
+
problems = []
|
|
32
|
+
seen: set = set()
|
|
33
|
+
try:
|
|
34
|
+
for f in args.files:
|
|
35
|
+
problems += check_file(f, args.level, args.ignore, args.allow_urls, seen)
|
|
36
|
+
except OSError as e:
|
|
37
|
+
print(f"req-pincheck: {e}", file=sys.stderr)
|
|
38
|
+
return 2
|
|
39
|
+
|
|
40
|
+
if args.json:
|
|
41
|
+
print(json.dumps({"ok": not problems, "problems": [asdict(x) for x in problems]}, indent=2))
|
|
42
|
+
elif not args.quiet:
|
|
43
|
+
for prob in problems:
|
|
44
|
+
print(prob)
|
|
45
|
+
if problems:
|
|
46
|
+
files = len({x.path for x in problems})
|
|
47
|
+
print(f"{len(problems)} problem(s) in {files} file(s)")
|
|
48
|
+
else:
|
|
49
|
+
print(f"✓ all requirements pinned (level: {args.level})")
|
|
50
|
+
return 1 if problems else 0
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if __name__ == "__main__": # pragma: no cover
|
|
54
|
+
sys.exit(main())
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Iterable, Iterator, List, Optional, Set, Tuple
|
|
7
|
+
|
|
8
|
+
from packaging.requirements import InvalidRequirement, Requirement
|
|
9
|
+
from packaging.utils import canonicalize_name
|
|
10
|
+
|
|
11
|
+
LEVELS = ("exact", "bounded", "any")
|
|
12
|
+
|
|
13
|
+
_INCLUDE_RE = re.compile(r"^(?:-r|--requirement|-c|--constraint)(?:\s+|=)(\S+)")
|
|
14
|
+
_EDITABLE_RE = re.compile(r"^(?:-e|--editable)(?:\s+|=)(\S+)")
|
|
15
|
+
_URL_RE = re.compile(r"^(?:[a-z+]+://|git\+|hg\+|svn\+|bzr\+|\.{0,2}/|file:)", re.I)
|
|
16
|
+
_IGNORE_MARK = re.compile(r"#\s*pincheck:\s*ignore", re.I)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class Problem:
|
|
21
|
+
path: str
|
|
22
|
+
line: int
|
|
23
|
+
name: str
|
|
24
|
+
spec: str
|
|
25
|
+
reason: str
|
|
26
|
+
|
|
27
|
+
def __str__(self) -> str:
|
|
28
|
+
return f"{self.path}:{self.line}: {self.spec} — {self.reason}"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _logical_lines(text: str) -> Iterator[Tuple[int, str, bool]]:
|
|
32
|
+
"""Yield (starting line number, content without comments, ignore-flag), joining continuations."""
|
|
33
|
+
buf: List[str] = []
|
|
34
|
+
start = 0
|
|
35
|
+
ignore = False
|
|
36
|
+
for lineno, raw in enumerate(text.splitlines(), 1):
|
|
37
|
+
if not buf:
|
|
38
|
+
start = lineno
|
|
39
|
+
if _IGNORE_MARK.search(raw):
|
|
40
|
+
ignore = True
|
|
41
|
+
# strip comments: '#' at line start or preceded by whitespace
|
|
42
|
+
line = re.sub(r"(^|\s)#.*$", "", raw).rstrip()
|
|
43
|
+
if line.endswith("\\"):
|
|
44
|
+
buf.append(line[:-1])
|
|
45
|
+
continue
|
|
46
|
+
buf.append(line)
|
|
47
|
+
joined = " ".join(part.strip() for part in buf).strip()
|
|
48
|
+
buf = []
|
|
49
|
+
if joined:
|
|
50
|
+
yield start, joined, ignore
|
|
51
|
+
ignore = False
|
|
52
|
+
if buf:
|
|
53
|
+
joined = " ".join(part.strip() for part in buf).strip()
|
|
54
|
+
if joined:
|
|
55
|
+
yield start, joined, ignore
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _strip_options(line: str) -> str:
|
|
59
|
+
"""Remove per-requirement pip options such as --hash=... from a requirement line."""
|
|
60
|
+
return re.split(r"\s+--?[a-zA-Z]", " " + line, maxsplit=1)[0].strip() if " -" in line else line
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def check_requirement(req: Requirement, level: str = "exact") -> Optional[str]:
|
|
64
|
+
"""Return a problem description for a parsed requirement, or None if it passes `level`."""
|
|
65
|
+
if level not in LEVELS:
|
|
66
|
+
raise ValueError(f"level must be one of {LEVELS}")
|
|
67
|
+
if req.url:
|
|
68
|
+
return "unversioned-url"
|
|
69
|
+
specs = list(req.specifier)
|
|
70
|
+
if not specs:
|
|
71
|
+
return "unpinned (no version specifier)"
|
|
72
|
+
if level == "any":
|
|
73
|
+
return None
|
|
74
|
+
exact = [s for s in specs if s.operator in ("==", "===")]
|
|
75
|
+
if exact:
|
|
76
|
+
if any(s.operator == "==" and s.version.endswith(".*") for s in exact):
|
|
77
|
+
if level == "exact":
|
|
78
|
+
return "wildcard pin (needs exact version)"
|
|
79
|
+
return None
|
|
80
|
+
return None
|
|
81
|
+
if level == "exact":
|
|
82
|
+
return "not an exact pin (needs ==)"
|
|
83
|
+
# bounded
|
|
84
|
+
if any(s.operator in ("<", "<=", "~=") for s in specs):
|
|
85
|
+
return None
|
|
86
|
+
return "no upper bound (needs <, <=, ~= or ==)"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def check_file(
|
|
90
|
+
path: str,
|
|
91
|
+
level: str = "exact",
|
|
92
|
+
ignore: Iterable[str] = (),
|
|
93
|
+
allow_urls: bool = False,
|
|
94
|
+
_seen: Optional[Set[str]] = None,
|
|
95
|
+
) -> List[Problem]:
|
|
96
|
+
"""Check a requirements file (following -r/-c includes) and return problems found."""
|
|
97
|
+
if level not in LEVELS:
|
|
98
|
+
raise ValueError(f"level must be one of {LEVELS}")
|
|
99
|
+
ignored = {canonicalize_name(n) for n in ignore}
|
|
100
|
+
seen = _seen if _seen is not None else set()
|
|
101
|
+
real = os.path.realpath(path)
|
|
102
|
+
if real in seen:
|
|
103
|
+
return []
|
|
104
|
+
seen.add(real)
|
|
105
|
+
|
|
106
|
+
with open(path, encoding="utf-8") as fh:
|
|
107
|
+
text = fh.read()
|
|
108
|
+
|
|
109
|
+
problems: List[Problem] = []
|
|
110
|
+
base = os.path.dirname(path)
|
|
111
|
+
for lineno, line, skip in _logical_lines(text):
|
|
112
|
+
inc = _INCLUDE_RE.match(line)
|
|
113
|
+
if inc:
|
|
114
|
+
target = inc.group(1)
|
|
115
|
+
if not _URL_RE.match(target) or target.startswith((".", "/")):
|
|
116
|
+
target_path = target if os.path.isabs(target) else os.path.join(base, target)
|
|
117
|
+
problems += check_file(target_path, level, ignored, allow_urls, seen)
|
|
118
|
+
continue
|
|
119
|
+
ed = _EDITABLE_RE.match(line)
|
|
120
|
+
if ed:
|
|
121
|
+
if not (allow_urls or skip):
|
|
122
|
+
problems.append(Problem(path, lineno, ed.group(1), line, "unversioned-url (editable)"))
|
|
123
|
+
continue
|
|
124
|
+
if line.startswith("-"):
|
|
125
|
+
continue # other pip options: --index-url, --find-links, --pre, ...
|
|
126
|
+
if _URL_RE.match(line):
|
|
127
|
+
if not (allow_urls or skip):
|
|
128
|
+
problems.append(Problem(path, lineno, line, line, "unversioned-url"))
|
|
129
|
+
continue
|
|
130
|
+
spec_text = _strip_options(line)
|
|
131
|
+
try:
|
|
132
|
+
req = Requirement(spec_text)
|
|
133
|
+
except InvalidRequirement as e:
|
|
134
|
+
problems.append(Problem(path, lineno, spec_text, spec_text, f"unparseable ({e})"))
|
|
135
|
+
continue
|
|
136
|
+
if skip or canonicalize_name(req.name) in ignored:
|
|
137
|
+
continue
|
|
138
|
+
reason = check_requirement(req, level)
|
|
139
|
+
if reason == "unversioned-url" and allow_urls:
|
|
140
|
+
continue
|
|
141
|
+
if reason:
|
|
142
|
+
problems.append(Problem(path, lineno, req.name, spec_text.split(";")[0].strip(), reason))
|
|
143
|
+
return problems
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: req-pincheck
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lint requirements.txt files for unpinned or loosely pinned dependencies, with CI-friendly exit codes.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Keywords: requirements,pip,pinning,dependencies,lint,ci,reproducibility
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Environment :: Console
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
11
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: packaging>=22
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# req-pincheck
|
|
20
|
+
|
|
21
|
+
Reproducible builds die quietly when someone adds `requests` (no version) or
|
|
22
|
+
`django>=4` to a requirements file. `req-pincheck` lints `requirements*.txt`
|
|
23
|
+
files and fails CI when dependencies aren't pinned to the level you require.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install req-pincheck
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
req-pincheck requirements.txt # every dep must be pinned with == (default)
|
|
35
|
+
req-pincheck --level bounded requirements.txt # allow ranges, but require an upper bound (<, <=, ~=, ==)
|
|
36
|
+
req-pincheck --level any requirements.txt # just require *some* version specifier
|
|
37
|
+
req-pincheck --ignore pip --ignore setuptools requirements*.txt
|
|
38
|
+
req-pincheck --json requirements.txt
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Example output:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
requirements.txt:3: requests — unpinned (no version specifier)
|
|
45
|
+
requirements.txt:5: django>=4.2 — not an exact pin (needs ==)
|
|
46
|
+
requirements.txt:9: numpy==1.* — wildcard pin (needs exact version)
|
|
47
|
+
3 problem(s) in 1 file(s)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Levels
|
|
51
|
+
|
|
52
|
+
| level | passes | fails |
|
|
53
|
+
|-----------|--------------------------------|----------------------------|
|
|
54
|
+
| `exact` | `==1.2.3`, `===1.2.3` | everything else |
|
|
55
|
+
| `bounded` | `==`, `~=`, anything with `<`/`<=` | `>=1.0`, `!=1.1`, no spec |
|
|
56
|
+
| `any` | any specifier | no specifier |
|
|
57
|
+
|
|
58
|
+
### What it understands
|
|
59
|
+
|
|
60
|
+
- `-r other.txt` / `--requirement` includes (followed recursively, relative to the including file) and `-c` constraint files
|
|
61
|
+
- Environment markers (`foo==1.0; python_version < "3.10"`)
|
|
62
|
+
- Extras (`uvicorn[standard]==0.30.0`)
|
|
63
|
+
- Line continuations and `--hash=...` options
|
|
64
|
+
- Comments, blank lines and pip options (`--index-url`, etc.) are skipped
|
|
65
|
+
- Direct references (`pkg @ https://...`, `git+https://...`, local paths, `-e`) are reported as
|
|
66
|
+
`unversioned-url` — use `--allow-urls` to accept them
|
|
67
|
+
- Add `# pincheck: ignore` to a line to skip it
|
|
68
|
+
|
|
69
|
+
### Exit codes
|
|
70
|
+
|
|
71
|
+
| code | meaning |
|
|
72
|
+
|------|---------|
|
|
73
|
+
| 0 | everything pinned |
|
|
74
|
+
| 1 | at least one problem |
|
|
75
|
+
| 2 | a file could not be read |
|
|
76
|
+
|
|
77
|
+
## Python API
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from req_pincheck import check_file
|
|
81
|
+
|
|
82
|
+
for problem in check_file("requirements.txt", level="bounded"):
|
|
83
|
+
print(problem.path, problem.line, problem.name, problem.reason)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/req_pincheck/__init__.py
|
|
5
|
+
src/req_pincheck/__main__.py
|
|
6
|
+
src/req_pincheck/cli.py
|
|
7
|
+
src/req_pincheck/core.py
|
|
8
|
+
src/req_pincheck.egg-info/PKG-INFO
|
|
9
|
+
src/req_pincheck.egg-info/SOURCES.txt
|
|
10
|
+
src/req_pincheck.egg-info/dependency_links.txt
|
|
11
|
+
src/req_pincheck.egg-info/entry_points.txt
|
|
12
|
+
src/req_pincheck.egg-info/requires.txt
|
|
13
|
+
src/req_pincheck.egg-info/top_level.txt
|
|
14
|
+
tests/test_req_pincheck.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
packaging>=22
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
req_pincheck
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from packaging.requirements import Requirement
|
|
5
|
+
|
|
6
|
+
from req_pincheck import check_file, check_requirement
|
|
7
|
+
from req_pincheck.cli import main
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@pytest.mark.parametrize(
|
|
11
|
+
"spec,level,ok",
|
|
12
|
+
[
|
|
13
|
+
("a==1.0", "exact", True),
|
|
14
|
+
("a===1.0", "exact", True),
|
|
15
|
+
("a==1.*", "exact", False),
|
|
16
|
+
("a==1.*", "bounded", True),
|
|
17
|
+
("a>=1", "exact", False),
|
|
18
|
+
("a>=1", "bounded", False),
|
|
19
|
+
("a>=1,<2", "bounded", True),
|
|
20
|
+
("a~=1.2", "bounded", True),
|
|
21
|
+
("a!=1.1", "bounded", False),
|
|
22
|
+
("a>=1", "any", True),
|
|
23
|
+
("a", "any", False),
|
|
24
|
+
("a[extra]==1.0; python_version<'3.10'", "exact", True),
|
|
25
|
+
("a @ https://example.com/a.whl", "any", False),
|
|
26
|
+
],
|
|
27
|
+
)
|
|
28
|
+
def test_check_requirement(spec, level, ok):
|
|
29
|
+
assert (check_requirement(Requirement(spec), level) is None) is ok
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_check_file(tmp_path):
|
|
33
|
+
(tmp_path / "base.txt").write_text("six==1.16.0\nloose>=1\n")
|
|
34
|
+
(tmp_path / "requirements.txt").write_text(
|
|
35
|
+
"# top comment\n"
|
|
36
|
+
"--index-url https://pypi.org/simple\n"
|
|
37
|
+
"-r base.txt\n"
|
|
38
|
+
"requests\n"
|
|
39
|
+
"django>=4.2 # web\n"
|
|
40
|
+
"numpy==1.26.4 \\\n"
|
|
41
|
+
" --hash=sha256:abc \\\n"
|
|
42
|
+
" --hash=sha256:def\n"
|
|
43
|
+
"uvicorn[standard]==0.30.0 ; python_version >= '3.9'\n"
|
|
44
|
+
"skipme # pincheck: ignore\n"
|
|
45
|
+
"git+https://github.com/x/y.git\n"
|
|
46
|
+
"-e ./local\n"
|
|
47
|
+
"Pip\n"
|
|
48
|
+
)
|
|
49
|
+
probs = check_file(str(tmp_path / "requirements.txt"), ignore=["pip"])
|
|
50
|
+
got = sorted((p.path.rsplit("/", 1)[-1], p.line, p.name) for p in probs)
|
|
51
|
+
assert got == [
|
|
52
|
+
("base.txt", 2, "loose"),
|
|
53
|
+
("requirements.txt", 4, "requests"),
|
|
54
|
+
("requirements.txt", 5, "django"),
|
|
55
|
+
("requirements.txt", 11, "git+https://github.com/x/y.git"),
|
|
56
|
+
("requirements.txt", 12, "./local"),
|
|
57
|
+
]
|
|
58
|
+
probs = check_file(str(tmp_path / "requirements.txt"), ignore=["pip"], allow_urls=True, level="any")
|
|
59
|
+
assert [p.name for p in probs] == ["requests"]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_include_cycle(tmp_path):
|
|
63
|
+
(tmp_path / "a.txt").write_text("-r b.txt\nx==1\n")
|
|
64
|
+
(tmp_path / "b.txt").write_text("-r a.txt\ny\n")
|
|
65
|
+
assert [p.name for p in check_file(str(tmp_path / "a.txt"))] == ["y"]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_cli(tmp_path, capsys):
|
|
69
|
+
f = tmp_path / "r.txt"
|
|
70
|
+
f.write_text("a==1\n")
|
|
71
|
+
assert main([str(f)]) == 0
|
|
72
|
+
f.write_text("a==1\nb>=2\n")
|
|
73
|
+
assert main([str(f)]) == 1
|
|
74
|
+
capsys.readouterr()
|
|
75
|
+
assert main([str(f), "--json"]) == 1
|
|
76
|
+
data = json.loads(capsys.readouterr().out)
|
|
77
|
+
assert data["ok"] is False and data["problems"][0]["name"] == "b"
|
|
78
|
+
assert main([str(tmp_path / "missing.txt")]) == 2
|