utils/dep_freshness/discover.py
Krzysztof kuhy Rudnicki a4ea305164 dep_freshness: Gradle ecosystem -- version catalogs, wrapper, Maven registries
The gate had no way to read a Gradle build, which left the two Kotlin
forks (tachiyomisy, syncyomi-src) outside the fleet-wide rule.

- parsers/gradle.py reads gradle/libs.versions.toml ([libraries] and
  [plugins] in every shape: inline string, module + version.ref,
  group/name/version, rich {strictly|require|prefer} tables) into
  `maven:<group>:<artifact>` deps, pointing the fix at the [versions]
  line when a ref is used; BOM-managed entries without a version are
  skipped. gradle-wrapper.properties becomes `toolchain:gradle`.
- registries/maven.py asks Google Maven, Central and the plugin portal
  in that order, reading the full <versions> list -- AndroidX writes
  alphas into <latest>. Plugins resolve through their marker artifact.
- registries/toolchain.gradle_latest reads services.gradle.org.
- http.get_text shares the transport with get_json (XML bodies).
- Templates: the pre-commit `files:` and the CI cache key know the
  catalog. 100% branch on every new file; fixtures only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Yc4TiPjhbQLfH35QBPfrbP
2026-09-12 19:10:42 +02:00

91 lines
2.7 KiB
Python

"""Find manifests, and route each one to the parser that understands it.
Discovery skips git-ignored files for the same reason the file-length gate
does: a violation in build output or a vendored tree is not something a commit
could ever fix, so reporting it only teaches the user to ignore the gate.
"""
from __future__ import annotations
import os
import re
import subprocess
from pathlib import Path
from dep_freshness._tables import EXCLUDED_DIRS, MANIFEST_GLOBS, REQUIREMENTS_PATTERN
from dep_freshness.models import Dep
from dep_freshness.parsers import (
fvm,
golang,
gradle,
javascript,
pubspec,
python,
rust,
workflow,
)
_REQUIREMENTS = re.compile(REQUIREMENTS_PATTERN)
_BY_NAME = {
"pubspec.yaml": pubspec.parse,
"pyproject.toml": python.parse_pyproject,
"package.json": javascript.parse_package_json,
"Cargo.toml": rust.parse,
"go.mod": golang.parse,
".fvmrc": fvm.parse,
".nvmrc": javascript.parse_nvmrc,
".python-version": python.parse_python_version,
gradle.CATALOG_NAME: gradle.parse_catalog,
gradle.WRAPPER_NAME: gradle.parse_wrapper,
}
def is_manifest(path: Path) -> bool:
return (
path.name in MANIFEST_GLOBS
or bool(_REQUIREMENTS.match(path.name))
or workflow.is_workflow(path)
)
def parse_manifest(path: Path) -> list[Dep]:
"""Every dependency declared in `path`, or [] if it is not a manifest."""
parser = _BY_NAME.get(path.name)
if parser is not None:
return parser(path)
if _REQUIREMENTS.match(path.name):
return python.parse_requirements(path)
if workflow.is_workflow(path):
return workflow.parse(path)
return []
def _git_ignored(paths: list[Path], root: Path) -> set[Path]:
if not paths:
return set()
try:
result = subprocess.run(
["git", "-C", str(root), "check-ignore", "--stdin"],
input="\n".join(str(p) for p in paths),
capture_output=True, text=True, check=False,
)
except OSError:
return set()
if result.returncode not in (0, 1): # 128 = not a repo
return set()
return {Path(line) for line in result.stdout.splitlines() if line}
def find_manifests(root: Path) -> list[Path]:
"""Every tracked manifest under `root`, deepest-first for stable output."""
found: list[Path] = []
for dirpath, dirnames, filenames in os.walk(root, followlinks=False):
dirnames[:] = [d for d in dirnames if d not in EXCLUDED_DIRS]
for name in filenames:
candidate = Path(dirpath) / name
if is_manifest(candidate) and not candidate.is_symlink():
found.append(candidate)
ignored = _git_ignored(found, root)
return sorted(p for p in found if p not in ignored)