catchdev 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.
- catchdev-0.1.0/LICENSE +21 -0
- catchdev-0.1.0/PKG-INFO +136 -0
- catchdev-0.1.0/README.md +123 -0
- catchdev-0.1.0/pyproject.toml +18 -0
- catchdev-0.1.0/setup.cfg +4 -0
- catchdev-0.1.0/src/catchdev/__init__.py +3 -0
- catchdev-0.1.0/src/catchdev/cli.py +75 -0
- catchdev-0.1.0/src/catchdev/depcheck.py +122 -0
- catchdev-0.1.0/src/catchdev/differ.py +242 -0
- catchdev-0.1.0/src/catchdev/mapper.py +328 -0
- catchdev-0.1.0/src/catchdev/report.py +162 -0
- catchdev-0.1.0/src/catchdev/runner.py +271 -0
- catchdev-0.1.0/src/catchdev.egg-info/PKG-INFO +136 -0
- catchdev-0.1.0/src/catchdev.egg-info/SOURCES.txt +17 -0
- catchdev-0.1.0/src/catchdev.egg-info/dependency_links.txt +1 -0
- catchdev-0.1.0/src/catchdev.egg-info/entry_points.txt +2 -0
- catchdev-0.1.0/src/catchdev.egg-info/requires.txt +3 -0
- catchdev-0.1.0/src/catchdev.egg-info/top_level.txt +1 -0
- catchdev-0.1.0/tests/test_catchdev.py +176 -0
catchdev-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 catchdev contributors
|
|
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.
|
catchdev-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: catchdev
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tells you what your AI coding agent just broke
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: httpx
|
|
10
|
+
Requires-Dist: rich
|
|
11
|
+
Requires-Dist: click
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# catchdev
|
|
15
|
+
|
|
16
|
+
**Tells you what your AI coding agent just broke.**
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
That GIF is real: it's catch flagging the `SECRET_KEY_FALLBACKS` session bug
|
|
21
|
+
that lived in Flask 3.1.0 for ~4 months before anyone noticed. catch ran the
|
|
22
|
+
affected tests, re-ran the failures against the pre-diff baseline, and pointed
|
|
23
|
+
at the exact regression. [Reproduce it yourself.](demo/README.md)
|
|
24
|
+
|
|
25
|
+
## 10-second install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install catchdev
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cd some-ai-generated-project
|
|
35
|
+
# make some changes (or let an agent make them)
|
|
36
|
+
git add .
|
|
37
|
+
catch # check staged changes
|
|
38
|
+
catch --commit HEAD~1 # check the last commit
|
|
39
|
+
catch --all-tests # run the full suite instead of affected tests
|
|
40
|
+
catch --skip-deps # skip PyPI dependency risk checks
|
|
41
|
+
catch --fail-on-suspicious # also block on suspicious (not just dangerous) deps
|
|
42
|
+
catch --output report.md # write a markdown report (for PR comments)
|
|
43
|
+
echo $? # 1 = problems found, 0 = clean
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
In a GitHub Actions PR, catch automatically diffs against
|
|
47
|
+
`origin/$GITHUB_BASE_REF` instead of `HEAD~1` (squash merges break `HEAD~1`).
|
|
48
|
+
|
|
49
|
+
## How it works
|
|
50
|
+
|
|
51
|
+
1. **differ** — parses `git diff --unified=0` into changed files, changed line
|
|
52
|
+
numbers, and newly added imports / dependency entries.
|
|
53
|
+
2. **mapper** — builds (and caches in `.catch_cache.json`) an AST import graph
|
|
54
|
+
of the project, then walks it backwards from each changed file until it hits
|
|
55
|
+
test files. When possible it narrows down to the specific test functions
|
|
56
|
+
that call the functions your diff touched.
|
|
57
|
+
3. **runner** — runs the affected tests with pytest, then re-runs any failures
|
|
58
|
+
against the pre-diff baseline in a temporary git worktree to distinguish
|
|
59
|
+
NEW failures from pre-existing ones.
|
|
60
|
+
4. **depcheck** — queries PyPI for every new dependency: does it exist, how old
|
|
61
|
+
is it, how many downloads, and did you mean a similarly-named popular
|
|
62
|
+
package? (Hallucinated package names — "slopsquatting" — are a real supply
|
|
63
|
+
chain risk with AI-generated code.)
|
|
64
|
+
5. **report** — pretty terminal output and an exit code you can wire into a
|
|
65
|
+
hook or CI.
|
|
66
|
+
|
|
67
|
+
## Why not just...?
|
|
68
|
+
|
|
69
|
+
| | catch | LLM review (CodeRabbit etc.) | static gates (dont-break, Solumbe) |
|
|
70
|
+
|---|---|---|---|
|
|
71
|
+
| Actually runs your tests | ✓ | ✗ | ✗ |
|
|
72
|
+
| Tells new failures from pre-existing | ✓ | ✗ | ✗ |
|
|
73
|
+
| Deterministic (same answer every run) | ✓ | ✗ | ✓ |
|
|
74
|
+
| Catches hallucinated packages | ✓ (live registry check) | unreliable | partial |
|
|
75
|
+
| Cost per run | free | tokens/subscription | free |
|
|
76
|
+
| Works as a hard CI gate | ✓ exit code | ✗ | ✓ |
|
|
77
|
+
|
|
78
|
+
## Use it everywhere
|
|
79
|
+
|
|
80
|
+
**GitHub Action** (posts the report as a PR comment):
|
|
81
|
+
|
|
82
|
+
```yaml
|
|
83
|
+
# .github/workflows/catch.yml
|
|
84
|
+
name: Catch
|
|
85
|
+
on: [pull_request]
|
|
86
|
+
jobs:
|
|
87
|
+
catch:
|
|
88
|
+
runs-on: ubuntu-latest
|
|
89
|
+
permissions:
|
|
90
|
+
issues: write
|
|
91
|
+
pull-requests: write
|
|
92
|
+
steps:
|
|
93
|
+
- uses: actions/checkout@v4
|
|
94
|
+
with:
|
|
95
|
+
fetch-depth: 0
|
|
96
|
+
- uses: actions/setup-python@v5
|
|
97
|
+
with:
|
|
98
|
+
python-version: "3.12"
|
|
99
|
+
- run: pip install -e .
|
|
100
|
+
- uses: yourusername/catchdev@v1
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
catch runs catch on its own PRs — see
|
|
104
|
+
[.github/workflows/catch.yml](.github/workflows/catch.yml) in this repo.
|
|
105
|
+
|
|
106
|
+
**Pre-commit / pre-push hook** — add to `.pre-commit-config.yaml`:
|
|
107
|
+
|
|
108
|
+
```yaml
|
|
109
|
+
- repo: https://github.com/yourusername/catchdev
|
|
110
|
+
rev: v0.1.0
|
|
111
|
+
hooks:
|
|
112
|
+
- id: catch
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Running tests on every commit annoys people; pre-push is the sweet spot:
|
|
116
|
+
|
|
117
|
+
```yaml
|
|
118
|
+
- id: catch
|
|
119
|
+
stages: [pre-push]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Claude Code skill** — your agent checks its own work before committing.
|
|
123
|
+
Copy [skills/catch/SKILL.md](skills/catch/SKILL.md) into
|
|
124
|
+
`~/.claude/skills/catch/` (or drop the packaged `catch.skill` from Releases
|
|
125
|
+
into your skills directory). After that, Claude runs `catch --staged` before
|
|
126
|
+
every commit and fixes what it breaks instead of shipping it.
|
|
127
|
+
|
|
128
|
+
## Exit codes
|
|
129
|
+
|
|
130
|
+
- `0` — all affected tests pass, no dangerous dependencies
|
|
131
|
+
- `1` — new test failures OR dangerous dependencies
|
|
132
|
+
- `2` — catch itself couldn't run (not a git repo, etc.)
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
MIT
|
catchdev-0.1.0/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# catchdev
|
|
2
|
+
|
|
3
|
+
**Tells you what your AI coding agent just broke.**
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
That GIF is real: it's catch flagging the `SECRET_KEY_FALLBACKS` session bug
|
|
8
|
+
that lived in Flask 3.1.0 for ~4 months before anyone noticed. catch ran the
|
|
9
|
+
affected tests, re-ran the failures against the pre-diff baseline, and pointed
|
|
10
|
+
at the exact regression. [Reproduce it yourself.](demo/README.md)
|
|
11
|
+
|
|
12
|
+
## 10-second install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install catchdev
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
cd some-ai-generated-project
|
|
22
|
+
# make some changes (or let an agent make them)
|
|
23
|
+
git add .
|
|
24
|
+
catch # check staged changes
|
|
25
|
+
catch --commit HEAD~1 # check the last commit
|
|
26
|
+
catch --all-tests # run the full suite instead of affected tests
|
|
27
|
+
catch --skip-deps # skip PyPI dependency risk checks
|
|
28
|
+
catch --fail-on-suspicious # also block on suspicious (not just dangerous) deps
|
|
29
|
+
catch --output report.md # write a markdown report (for PR comments)
|
|
30
|
+
echo $? # 1 = problems found, 0 = clean
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
In a GitHub Actions PR, catch automatically diffs against
|
|
34
|
+
`origin/$GITHUB_BASE_REF` instead of `HEAD~1` (squash merges break `HEAD~1`).
|
|
35
|
+
|
|
36
|
+
## How it works
|
|
37
|
+
|
|
38
|
+
1. **differ** — parses `git diff --unified=0` into changed files, changed line
|
|
39
|
+
numbers, and newly added imports / dependency entries.
|
|
40
|
+
2. **mapper** — builds (and caches in `.catch_cache.json`) an AST import graph
|
|
41
|
+
of the project, then walks it backwards from each changed file until it hits
|
|
42
|
+
test files. When possible it narrows down to the specific test functions
|
|
43
|
+
that call the functions your diff touched.
|
|
44
|
+
3. **runner** — runs the affected tests with pytest, then re-runs any failures
|
|
45
|
+
against the pre-diff baseline in a temporary git worktree to distinguish
|
|
46
|
+
NEW failures from pre-existing ones.
|
|
47
|
+
4. **depcheck** — queries PyPI for every new dependency: does it exist, how old
|
|
48
|
+
is it, how many downloads, and did you mean a similarly-named popular
|
|
49
|
+
package? (Hallucinated package names — "slopsquatting" — are a real supply
|
|
50
|
+
chain risk with AI-generated code.)
|
|
51
|
+
5. **report** — pretty terminal output and an exit code you can wire into a
|
|
52
|
+
hook or CI.
|
|
53
|
+
|
|
54
|
+
## Why not just...?
|
|
55
|
+
|
|
56
|
+
| | catch | LLM review (CodeRabbit etc.) | static gates (dont-break, Solumbe) |
|
|
57
|
+
|---|---|---|---|
|
|
58
|
+
| Actually runs your tests | ✓ | ✗ | ✗ |
|
|
59
|
+
| Tells new failures from pre-existing | ✓ | ✗ | ✗ |
|
|
60
|
+
| Deterministic (same answer every run) | ✓ | ✗ | ✓ |
|
|
61
|
+
| Catches hallucinated packages | ✓ (live registry check) | unreliable | partial |
|
|
62
|
+
| Cost per run | free | tokens/subscription | free |
|
|
63
|
+
| Works as a hard CI gate | ✓ exit code | ✗ | ✓ |
|
|
64
|
+
|
|
65
|
+
## Use it everywhere
|
|
66
|
+
|
|
67
|
+
**GitHub Action** (posts the report as a PR comment):
|
|
68
|
+
|
|
69
|
+
```yaml
|
|
70
|
+
# .github/workflows/catch.yml
|
|
71
|
+
name: Catch
|
|
72
|
+
on: [pull_request]
|
|
73
|
+
jobs:
|
|
74
|
+
catch:
|
|
75
|
+
runs-on: ubuntu-latest
|
|
76
|
+
permissions:
|
|
77
|
+
issues: write
|
|
78
|
+
pull-requests: write
|
|
79
|
+
steps:
|
|
80
|
+
- uses: actions/checkout@v4
|
|
81
|
+
with:
|
|
82
|
+
fetch-depth: 0
|
|
83
|
+
- uses: actions/setup-python@v5
|
|
84
|
+
with:
|
|
85
|
+
python-version: "3.12"
|
|
86
|
+
- run: pip install -e .
|
|
87
|
+
- uses: yourusername/catchdev@v1
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
catch runs catch on its own PRs — see
|
|
91
|
+
[.github/workflows/catch.yml](.github/workflows/catch.yml) in this repo.
|
|
92
|
+
|
|
93
|
+
**Pre-commit / pre-push hook** — add to `.pre-commit-config.yaml`:
|
|
94
|
+
|
|
95
|
+
```yaml
|
|
96
|
+
- repo: https://github.com/yourusername/catchdev
|
|
97
|
+
rev: v0.1.0
|
|
98
|
+
hooks:
|
|
99
|
+
- id: catch
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Running tests on every commit annoys people; pre-push is the sweet spot:
|
|
103
|
+
|
|
104
|
+
```yaml
|
|
105
|
+
- id: catch
|
|
106
|
+
stages: [pre-push]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Claude Code skill** — your agent checks its own work before committing.
|
|
110
|
+
Copy [skills/catch/SKILL.md](skills/catch/SKILL.md) into
|
|
111
|
+
`~/.claude/skills/catch/` (or drop the packaged `catch.skill` from Releases
|
|
112
|
+
into your skills directory). After that, Claude runs `catch --staged` before
|
|
113
|
+
every commit and fixes what it breaks instead of shipping it.
|
|
114
|
+
|
|
115
|
+
## Exit codes
|
|
116
|
+
|
|
117
|
+
- `0` — all affected tests pass, no dangerous dependencies
|
|
118
|
+
- `1` — new test failures OR dangerous dependencies
|
|
119
|
+
- `2` — catch itself couldn't run (not a git repo, etc.)
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "catchdev"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Tells you what your AI coding agent just broke"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = {text = "MIT"}
|
|
7
|
+
requires-python = ">=3.9"
|
|
8
|
+
dependencies = ["httpx", "rich", "click"]
|
|
9
|
+
|
|
10
|
+
[project.scripts]
|
|
11
|
+
catch = "catchdev.cli:main"
|
|
12
|
+
|
|
13
|
+
[build-system]
|
|
14
|
+
requires = ["setuptools"]
|
|
15
|
+
build-backend = "setuptools.build_meta"
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
where = ["src"]
|
catchdev-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""catch — Tells you what your AI coding agent just broke."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
import click
|
|
8
|
+
|
|
9
|
+
from . import depcheck, differ, mapper, report, runner
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@click.command()
|
|
13
|
+
@click.option("--staged", is_flag=True, default=False,
|
|
14
|
+
help="Check staged changes (default)")
|
|
15
|
+
@click.option("--commit", default=None,
|
|
16
|
+
help="Check changes since a specific commit (e.g., HEAD~1)")
|
|
17
|
+
@click.option("--all-tests", is_flag=True, default=False,
|
|
18
|
+
help="Run all tests, not just affected ones")
|
|
19
|
+
@click.option("--skip-deps", is_flag=True, default=False,
|
|
20
|
+
help="Skip dependency checking")
|
|
21
|
+
@click.option("--fail-on-suspicious", is_flag=True, default=False,
|
|
22
|
+
help="Exit 1 on suspicious (not just dangerous) dependencies")
|
|
23
|
+
@click.option("--output", default=None, metavar="FILE",
|
|
24
|
+
help="Also write the report as markdown to FILE "
|
|
25
|
+
"(e.g. report.md for PR comments)")
|
|
26
|
+
def main(staged: bool, commit: str | None, all_tests: bool, skip_deps: bool,
|
|
27
|
+
fail_on_suspicious: bool, output: str | None) -> None:
|
|
28
|
+
"""Tells you what your AI coding agent just broke."""
|
|
29
|
+
# In CI pull requests, diff against the PR base, not HEAD~1
|
|
30
|
+
# (squash merges break HEAD~1). GITHUB_BASE_REF is set by GitHub Actions.
|
|
31
|
+
base_ref_env = os.environ.get("GITHUB_BASE_REF")
|
|
32
|
+
if commit is None and base_ref_env and not staged:
|
|
33
|
+
commit = f"origin/{base_ref_env}"
|
|
34
|
+
|
|
35
|
+
# 1. Get the diff
|
|
36
|
+
try:
|
|
37
|
+
diff = differ.get_diff(staged=staged or not commit, commit=commit)
|
|
38
|
+
except differ.GitError as e:
|
|
39
|
+
report.err_console.print(f"[red]✗ {e}[/red]")
|
|
40
|
+
sys.exit(2)
|
|
41
|
+
|
|
42
|
+
if not diff.files:
|
|
43
|
+
report.console.print("[green]✓ no changes detected[/green]")
|
|
44
|
+
sys.exit(0)
|
|
45
|
+
|
|
46
|
+
# 2. Map to affected tests
|
|
47
|
+
affected = mapper.find_affected_tests(diff)
|
|
48
|
+
|
|
49
|
+
# 3. Run tests (baseline = the ref we diffed against)
|
|
50
|
+
results = runner.run_tests(
|
|
51
|
+
affected, all_tests=all_tests, base_ref=commit or "HEAD"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# 4. Check dependencies
|
|
55
|
+
dep_risks = [] if skip_deps else depcheck.check_dependencies(
|
|
56
|
+
diff.new_dependencies
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# 5. Report (terminal + optional markdown file)
|
|
60
|
+
report.print_report(diff, affected, results, dep_risks,
|
|
61
|
+
fail_on_suspicious=fail_on_suspicious)
|
|
62
|
+
if output:
|
|
63
|
+
with open(output, "w", encoding="utf-8") as f:
|
|
64
|
+
f.write(report.render_markdown(
|
|
65
|
+
diff, affected, results, dep_risks,
|
|
66
|
+
fail_on_suspicious=fail_on_suspicious,
|
|
67
|
+
))
|
|
68
|
+
|
|
69
|
+
# 6. Exit code
|
|
70
|
+
sys.exit(report.get_exit_code(results, dep_risks,
|
|
71
|
+
fail_on_suspicious=fail_on_suspicious))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
if __name__ == "__main__":
|
|
75
|
+
main()
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""Check new dependencies against PyPI.
|
|
2
|
+
|
|
3
|
+
Catches hallucinated packages (404 on PyPI), brand-new low-usage packages
|
|
4
|
+
(typosquat risk), and suggests the popular package you probably meant.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import difflib
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from datetime import datetime, timezone
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
PYPI_URL = "https://pypi.org/pypi/{package}/json"
|
|
15
|
+
STATS_URL = "https://pypistats.org/api/packages/{package}/recent"
|
|
16
|
+
|
|
17
|
+
POPULAR_PACKAGES = [
|
|
18
|
+
"requests", "numpy", "pandas", "flask", "django", "pyjwt", "sqlalchemy",
|
|
19
|
+
"click", "rich", "httpx", "pytest", "boto3", "scipy", "matplotlib",
|
|
20
|
+
"pillow", "beautifulsoup4", "cryptography", "paramiko", "aiohttp",
|
|
21
|
+
"fastapi", "pydantic", "urllib3", "certifi", "setuptools", "wheel",
|
|
22
|
+
"torch", "tensorflow", "scikit-learn", "transformers", "openai",
|
|
23
|
+
"anthropic", "redis", "celery", "gunicorn", "uvicorn", "starlette",
|
|
24
|
+
"jinja2", "pyyaml", "lxml", "psycopg2", "pymysql", "pymongo",
|
|
25
|
+
"python-dateutil", "tqdm", "six", "attrs", "packaging",
|
|
26
|
+
"typing-extensions", "colorama", "toml", "pytest-cov", "mock",
|
|
27
|
+
"responses", "faker", "factory-boy", "django-rest-framework",
|
|
28
|
+
"djangorestframework", "arrow", "pendulum", "typer", "fire",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class DepRisk:
|
|
34
|
+
package: str
|
|
35
|
+
exists: bool | None # None = couldn't verify (network error)
|
|
36
|
+
age_days: int | None = None
|
|
37
|
+
weekly_downloads: int | None = None
|
|
38
|
+
risk_level: str = "safe" # "safe" | "suspicious" | "dangerous"
|
|
39
|
+
similar_packages: list[str] = field(default_factory=list)
|
|
40
|
+
reason: str = ""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _classify(exists: bool | None, age_days: int | None,
|
|
44
|
+
weekly_downloads: int | None) -> tuple[str, str]:
|
|
45
|
+
if exists is False:
|
|
46
|
+
return "dangerous", "package does not exist on PyPI (likely hallucinated)"
|
|
47
|
+
if exists is None:
|
|
48
|
+
return "suspicious", "could not verify package on PyPI (network error)"
|
|
49
|
+
if age_days is not None and weekly_downloads is not None:
|
|
50
|
+
if age_days < 30 and weekly_downloads < 100:
|
|
51
|
+
return ("dangerous",
|
|
52
|
+
f"registered {age_days} days ago with "
|
|
53
|
+
f"{weekly_downloads} weekly downloads")
|
|
54
|
+
if age_days < 90 and weekly_downloads < 1000:
|
|
55
|
+
return ("suspicious",
|
|
56
|
+
f"registered {age_days} days ago with "
|
|
57
|
+
f"{weekly_downloads} weekly downloads")
|
|
58
|
+
parts = []
|
|
59
|
+
if age_days is not None:
|
|
60
|
+
parts.append(f"on PyPI for {age_days} days")
|
|
61
|
+
if weekly_downloads is not None:
|
|
62
|
+
parts.append(f"{weekly_downloads} weekly downloads")
|
|
63
|
+
return "safe", ", ".join(parts) or "established package"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _similar(package: str) -> list[str]:
|
|
67
|
+
return difflib.get_close_matches(
|
|
68
|
+
package.lower(), POPULAR_PACKAGES, n=3, cutoff=0.72
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _parse_time(value: str) -> datetime | None:
|
|
73
|
+
try:
|
|
74
|
+
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
75
|
+
except ValueError:
|
|
76
|
+
return None
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def check_dependencies(packages: list[str], timeout: float = 10.0) -> list[DepRisk]:
|
|
80
|
+
risks: list[DepRisk] = []
|
|
81
|
+
if not packages:
|
|
82
|
+
return risks
|
|
83
|
+
with httpx.Client(timeout=timeout, follow_redirects=True) as client:
|
|
84
|
+
for pkg in packages:
|
|
85
|
+
risk = DepRisk(package=pkg, exists=None)
|
|
86
|
+
try:
|
|
87
|
+
resp = client.get(PYPI_URL.format(package=pkg))
|
|
88
|
+
if resp.status_code == 404:
|
|
89
|
+
risk.exists = False
|
|
90
|
+
elif resp.status_code == 200:
|
|
91
|
+
risk.exists = True
|
|
92
|
+
data = resp.json()
|
|
93
|
+
earliest: datetime | None = None
|
|
94
|
+
for releases in data.get("releases", {}).values():
|
|
95
|
+
for f in releases:
|
|
96
|
+
t = _parse_time(f.get("upload_time", ""))
|
|
97
|
+
if t and (earliest is None or t < earliest):
|
|
98
|
+
earliest = t
|
|
99
|
+
if earliest:
|
|
100
|
+
risk.age_days = max(
|
|
101
|
+
0, (datetime.now(timezone.utc) - earliest).days
|
|
102
|
+
)
|
|
103
|
+
except httpx.HTTPError:
|
|
104
|
+
risk.exists = None
|
|
105
|
+
|
|
106
|
+
if risk.exists:
|
|
107
|
+
try:
|
|
108
|
+
sresp = client.get(STATS_URL.format(package=pkg.lower()))
|
|
109
|
+
if sresp.status_code == 200:
|
|
110
|
+
risk.weekly_downloads = (
|
|
111
|
+
sresp.json().get("data", {}).get("last_week")
|
|
112
|
+
)
|
|
113
|
+
except (httpx.HTTPError, ValueError):
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
risk.risk_level, risk.reason = _classify(
|
|
117
|
+
risk.exists, risk.age_days, risk.weekly_downloads
|
|
118
|
+
)
|
|
119
|
+
if risk.risk_level != "safe" or risk.exists is False:
|
|
120
|
+
risk.similar_packages = _similar(pkg)
|
|
121
|
+
risks.append(risk)
|
|
122
|
+
return risks
|