Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pipx install pydoctor-cli
pip install pydoctor-cli
```

> Install `pydoctor-cli`, not `pydoctor`: that is an unrelated project (Twisted's API doc generator) that installs a command with the same name.

PyDoctor scans your **project's** Python, wherever PyDoctor itself is installed. It uses the active virtualenv, or a `.venv`/`venv` in the project, and tells you which interpreter it chose. To choose one yourself:

```bash
pydoctor diagnose --python .venv/bin/python # or set PYDOCTOR_PYTHON
```

📖 Full documentation: **https://pydoctor-docs.thally.app**

---

## ⚡ Usage
Expand All @@ -75,6 +85,19 @@ pydoctor fix

## Release History

### v2.2.0
Results you can trust: every fix in this release removes a case where PyDoctor reported a problem as fine, or got a severity wrong.

- **Scans the right environment:** facts come from your project's interpreter, not PyDoctor's. Under pipx or `uv tool`, and in uv environments (which have no pip), 2.1.0 saw 0 packages and reported everything as OK. Works without pip (package metadata, `uv pip list --outdated`). New `--python` / `PYDOCTOR_PYTHON`; the report shows which interpreter was scanned and why.
- **Fails loudly:** `ENV_PACKAGES_UNAVAILABLE` (error) when the environment can't be read, `ENV_DEPS_NOT_INSTALLED` when the scanned Python has none of your dependencies, and outdated checks that can't run say so instead of "all up to date".
- **Virtualenv detection works:** it was always "active". The `diagnose` no-venv prompt now appears, and never blocks CI or piped runs.
- **Real CVSS scores:** severity is computed from the CVSS v3 vector. Previously a 3.1 (Low) and a 9.8 (Critical) vulnerability were both shown as critical. GitHub's `MODERATE` is now medium.
- **Right fix version:** for advisories with one fix per release line, `fix` targets your line (Django 5.1.2 → 5.1.4, not 4.2.17).
- **CI guard finds real leaks:** scans every committed text file, adds Stripe, OpenAI, Anthropic, Slack, Google, private-key, database-URL and `SECRET_KEY` rules, flags committed `.env` files, and checks workflows for `pull_request_target` "pwn requests", script injection and `write-all` permissions. Findings have line numbers; add `# pydoctor: ignore` to silence one. Zero findings on Flask, Django, requests, httpx, FastAPI, DRF and pip.
- **A score that moves:** a package's advisories count once, categories have diminishing returns, and any critical issue keeps the score below "Good". Expect different (usually higher) scores than 2.1.0.
- **JSON:** includes `health`; `scan-security --json` reports advisory IDs, aliases, fixed version and CVSS score.
- Smaller: database drivers, hashers and cache clients picked by Django settings count as used; faster advisory fetching; `--no-cache` no longer wipes the cache; circular import fixed; `github <repo>` and `docker` describe what they actually do.

### v2.1.0
- **`pydoctor fix` reports what really happened:** upgrades are verified against the installed version. If a pin in `pyproject.toml` blocks an upgrade, pydoctor says so and shows the constraint instead of printing "fixed".
- **One prompt per package:** vulnerable packages are offered once, with the advisory count, and upgraded to the lowest version that fixes every advisory.
Expand Down
2 changes: 1 addition & 1 deletion pydoctor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
License: MIT
"""

__version__ = "2.1.0"
__version__ = "2.2.0"
__author__ = "Favour Bamgboye"
__description__ = "Python environment diagnostic CLI — your project's doctor."
53 changes: 48 additions & 5 deletions pydoctor/analysis/health_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import math
from dataclasses import dataclass

from pydoctor.core.report import DiagnosisReport, Issue
Expand All @@ -18,20 +19,62 @@ class HealthMetrics:
message: str


# Each category can cost at most this many points. Within a category every
# extra issue still lowers the score, but by less (diminishing returns), so
# fixing issues in a project with dozens of them visibly moves the score.
CATEGORY_CAPS: dict[str, float] = {
"security": 60,
"ci": 50,
"dependencies": 40,
"environment": 30,
"outdated": 15,
"unused": 10,
}

# A project with any critical issue can't be rated Good or Excellent.
CRITICAL_CEILING = 69

# Issues that mean part of the scan didn't run.
INCOMPLETE_CODES = {"ENV_PACKAGES_UNAVAILABLE", "SEC_SCAN_INCOMPLETE"}


def calculate_health(report: DiagnosisReport) -> HealthMetrics:
"""
Calculate project health score (0-100) based on identified issues.
"""
score = _compute_score(report)
categories = list({i.category for i in report.issues})
return _get_verdict(score, categories)
categories = list({i.category for i in report.issues if i.code not in INCOMPLETE_CODES})
metrics = _get_verdict(score, categories)
if any(i.code in INCOMPLETE_CODES for i in report.issues):
metrics.verdict = "Incomplete"
metrics.message = (
"Some checks couldn't run, so this score only covers what was checked. "
"See the errors above."
)
return metrics


def _compute_score(report: DiagnosisReport) -> int:
score = 100
raw: dict[str, float] = {}
# One upgrade usually fixes every advisory for a package, so a package
# costs the penalty of its worst advisory, not one penalty per advisory.
per_package: dict[str, int] = {}
for issue in report.issues:
score -= _calculate_issue_penalty(issue)
return max(0, score)
penalty = _calculate_issue_penalty(issue)
if issue.code == "SEC_VULNERABILITY" and issue.package:
per_package[issue.package] = max(per_package.get(issue.package, 0), penalty)
continue
raw[issue.category] = raw.get(issue.category, 0) + penalty
if per_package:
raw["security"] = raw.get("security", 0) + sum(per_package.values())
lost = 0.0
for category, points in raw.items():
cap = CATEGORY_CAPS.get(category)
lost += cap * (1 - math.exp(-points / cap)) if cap else points
score = max(0, round(100 - lost))
if any(i.severity.lower() == "critical" for i in report.issues):
score = min(score, CRITICAL_CEILING)
return score


def _calculate_issue_penalty(issue: Issue) -> int:
Expand Down
9 changes: 8 additions & 1 deletion pydoctor/cache/cache_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,23 @@ def get(self, key: str) -> Any | None:

return entry.get("value")

def set(self, key: str, value: Any) -> None:
def set(self, key: str, value: Any, save: bool = True) -> None:
"""
Store a value in the cache with the current timestamp.

Parameters
----------
key: Cache key.
value: Any JSON-serialisable value.
save: Write to disk now. Pass False when setting many keys, then call
``save()`` once (writing the whole file per key is quadratic).
"""
self._data[key] = {"_ts": time.time(), "value": value}
if save:
self._save()

def save(self) -> None:
"""Persist the cache to disk."""
self._save()

def invalidate(self, key: str) -> None:
Expand Down
Loading
Loading