Only subtract excluded files from repos that were counted

This commit is contained in:
Mark Eaton committed 2026-11-11 19:53:17 -05:00
1 parent 9f76bf6c41
commit 26a9207daa
3 files changed
+49 -21

No files matched your search

+15 -8
View File
@@ -164,22 +164,30 @@ def aggregate_language_bytes(
file=sys.stderr,
)
continue
repo_bytes: dict[str, int] = {}
for lang, bytes_ in langs.items():
# Handle bytes as either int or string
if isinstance(bytes_, int):
totals[lang] += bytes_
repo_bytes[lang] = bytes_
elif isinstance(bytes_, str) and bytes_.isdigit():
totals[lang] += int(bytes_)
repo_bytes[lang] = int(bytes_)
# Skip non-numeric values
# Excluded files come off this repo's own bytes, so a repo that was
# skipped above can't lower the totals of other repos.
repo_bytes = subtract_excluded_files(session, repo["name"], repo_bytes)
for lang, nbytes in repo_bytes.items():
totals[lang] += nbytes
return dict(totals)
def subtract_excluded_files(
session: requests.Session, totals: dict[str, int]
session: requests.Session, repo_name: str, langs: dict[str, int]
) -> dict[str, int]:
"""Subtract byte counts for explicitly excluded files."""
for repo_name, file_path, language in EXCLUDE_FILES:
"""Subtract byte counts for one repository's explicitly excluded files."""
for excluded_repo, file_path, language in EXCLUDE_FILES:
if excluded_repo != repo_name:
continue
try:
meta = session.get(
f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo_name}/contents/{file_path}",
@@ -187,11 +195,11 @@ def subtract_excluded_files(
)
if meta.ok:
size = meta.json().get("size", 0)
totals[language] = max(0, totals.get(language, 0) - size)
langs[language] = max(0, langs.get(language, 0) - size)
except requests.RequestException:
# Ignore failures – continue with best-effort stats
pass
return totals
return langs
def generate_markdown(totals: dict[str, int]) -> str:
@@ -298,7 +306,6 @@ def main() -> int:
# Generate the stats
totals = aggregate_language_bytes(session, repos)
totals = subtract_excluded_files(session, totals)
if sum(totals.values()) == 0:
sys.exit("No language data returned from API.")