utils/dep_freshness/parsers/golang.py
Krzysztof kuhy Rudnicki 8f5cd3c5cd
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
ts_config tests / test (push) Waiting to run
ts_core tests / test (push) Waiting to run
web_ui tests / test (push) Waiting to run
dep_freshness: ruff import order + drop unused test imports
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 22:27:56 +02:00

54 lines
1.7 KiB
Python

"""`go.mod` -> module requirements.
Go pins exactly by construction, so every `require` line is a pin; the only
work is stripping the `v` prefix and ignoring `// indirect` entries, which the
module graph owns rather than this repo.
"""
from __future__ import annotations
import re
from pathlib import Path
from dep_freshness._tables import GOMOD, TOOLCHAIN
from dep_freshness.models import Dep
_REQUIRE = re.compile(r"^\s*(?P<module>[^\s()]+)\s+(?P<version>v\S+)\s*(?P<rest>.*)$")
def parse(path: Path) -> list[Dep]:
try:
lines = path.read_text(encoding="utf-8").splitlines()
except OSError:
return []
deps: list[Dep] = []
in_block = False
for number, raw in enumerate(lines, start=1):
line = raw.split("//")[0].strip()
comment = raw.partition("//")[2]
if line.startswith("go "):
deps.append(Dep(
ecosystem=TOOLCHAIN, name="go", constraint=line[3:].strip(),
path=path, line=number, caret_ok=True,
))
continue
if line.startswith("require ("):
in_block = True
continue
if in_block and line == ")":
in_block = False
continue
if line.startswith("require "):
line = line[len("require "):].strip()
elif not in_block:
continue
match = _REQUIRE.match(line)
if not match or "indirect" in comment:
continue
deps.append(Dep(
ecosystem=GOMOD, name=match.group("module"),
constraint=match.group("version"), path=path, line=number,
pinned=match.group("version").lstrip("v"),
))
return deps