Report lines of code and the main languages from cloc.txt

The project page takes the size from cloc's sum and names the three to
five largest languages, leaving out prose and data files such as Markdown,
text, CSV and JSON, and anything under 1% of the code beyond the third.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
alexanderandClaude Opus 5.5 committed 2026-09-24 21:14:14 +02:00
1 parent 9d78e9edf4
commit 2d88ffa953
4 files changed
+121 -15

No files matched your search

+24 -11
View File
@@ -25,6 +25,7 @@ from typing import TYPE_CHECKING, Any
from .vendor.cipherverifier.core.services.sarif import _build_finding, _build_report, _rules_by_id
from .vendor.cipherverifier.core.services.vdr import summarize_vdr
from .cloc import main_languages
from .layout import TITLE
from .severity import finding_severity
@@ -79,9 +80,9 @@ def build_context(
"sections": [_tool_section(name, report) for name, report in tools],
}
# Without the pipeline's metadata this page would be nothing but gaps.
if metadata:
context["project"] = _project(metadata, project)
# Without the pipeline's metadata or cloc's count this page would be nothing but gaps.
if metadata or documents.get("cloc"):
context["project"] = _project(metadata, project, documents.get("cloc"))
critical = _critical(tools, sca)
@@ -240,24 +241,36 @@ def _version(metadata: dict[str, Any]) -> str | None:
return commit if isinstance(commit, str) and _COMMIT.fullmatch(commit) else None
def _project(metadata: dict[str, Any], project: str | None) -> dict[str, Any]:
"""What was scanned."""
def _project(metadata: dict[str, Any], project: str | None, cloc: dict[str, Any] | None) -> dict[str, Any]:
"""What was scanned, and how much of it there is, as cloc counted it."""
return {
"id": "project",
"title": "Project Information",
"overview": [
_fact("Project", project),
_fact("Version", _version(metadata), kind="mono"),
# Nothing in the pipeline counts lines, so nothing here can report them.
_fact("Analysed lines of code", None),
_fact("Analysed lines of code", f"{cloc['code']:,} in {cloc['files']:,} files" if cloc else None),
],
# Left as one unstated row rather than a table of zeroes: no stage reports a language
# breakdown, and a share invented from the file extensions of the findings would be a
# measurement of where the bugs are, not of what the project is written in.
"languages": {"title": "Languages", "rows": [{"label": UNKNOWN, "share": UNKNOWN}]},
"languages": {"title": "Main languages", "rows": _languages(cloc)},
}
def _languages(cloc: dict[str, Any] | None) -> list[dict[str, Any]]:
"""The main languages with their lines of code and share of the whole project.
Left as one unstated row rather than a table of zeroes where cloc did not run: a share invented
from the file extensions of the findings would measure where the bugs are, not what the project
is written in.
"""
if not cloc:
return [{"label": UNKNOWN, "share": UNKNOWN}]
return [
{"label": one["name"], "share": f"{one['code']:,} lines ({one['code'] / cloc['code'] * 100:.1f} %)"}
for one in main_languages(cloc)
]
def _summary(
tools: list[tuple[str, SarifReport]], sca: VdrReport | None, metrics: dict[str, Any] | None
) -> dict[str, Any]: