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, file=sys.stderr,
) )
continue continue
repo_bytes: dict[str, int] = {}
for lang, bytes_ in langs.items(): for lang, bytes_ in langs.items():
# Handle bytes as either int or string # Handle bytes as either int or string
if isinstance(bytes_, int): if isinstance(bytes_, int):
totals[lang] += bytes_ repo_bytes[lang] = bytes_
elif isinstance(bytes_, str) and bytes_.isdigit(): elif isinstance(bytes_, str) and bytes_.isdigit():
totals[lang] += int(bytes_) repo_bytes[lang] = int(bytes_)
# Skip non-numeric values # 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) return dict(totals)
def subtract_excluded_files( def subtract_excluded_files(
session: requests.Session, totals: dict[str, int] session: requests.Session, repo_name: str, langs: dict[str, int]
) -> dict[str, int]: ) -> dict[str, int]:
"""Subtract byte counts for explicitly excluded files.""" """Subtract byte counts for one repository's explicitly excluded files."""
for repo_name, file_path, language in EXCLUDE_FILES: for excluded_repo, file_path, language in EXCLUDE_FILES:
if excluded_repo != repo_name:
continue
try: try:
meta = session.get( meta = session.get(
f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo_name}/contents/{file_path}", f"{BASE_URL}/api/v1/repos/{USERNAME}/{repo_name}/contents/{file_path}",
@@ -187,11 +195,11 @@ def subtract_excluded_files(
) )
if meta.ok: if meta.ok:
size = meta.json().get("size", 0) 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: except requests.RequestException:
# Ignore failures – continue with best-effort stats # Ignore failures – continue with best-effort stats
pass pass
return totals return langs
def generate_markdown(totals: dict[str, int]) -> str: def generate_markdown(totals: dict[str, int]) -> str:
@@ -298,7 +306,6 @@ def main() -> int:
# Generate the stats # Generate the stats
totals = aggregate_language_bytes(session, repos) totals = aggregate_language_bytes(session, repos)
totals = subtract_excluded_files(session, totals)
if sum(totals.values()) == 0: if sum(totals.values()) == 0:
sys.exit("No language data returned from API.") sys.exit("No language data returned from API.")
+21
View File
@@ -93,6 +93,27 @@ def test_first_run_writes_the_stats_into_the_profile_readme(gitea, monkeypatch,
assert "README.md updated successfully." in capsys.readouterr().err assert "README.md updated successfully." in capsys.readouterr().err
def test_excluded_file_in_a_skipped_repo_does_not_lower_other_repos(gitea, monkeypatch):
# website (which holds the excluded aframe.js) can't be read, so none of
# its bytes were counted and none should be subtracted.
serve_account(gitea, monkeypatch)
gitea.add("GET", WEBSITE_LANGS, {"message": "token scope is limited"}, status=403)
gitea.add("GET", SCRIPTS_LANGS, {"Python": 3000, "JavaScript": 1000})
run_main(monkeypatch)
# Total 4000 -> Python 75.0% (37 blocks), JavaScript 25.0% (12 blocks)
assert pushed_readme(gitea) == (
"# Hi there\n\n"
"<!--LANG_STATS_START-->\n"
"## 📊 Languages\n"
+ "█" * 37
+ " Python 75.0%\\\n"
+ "█" * 12
+ " JavaScript 25.0%\n"
"<!--LANG_STATS_END-->\n\n"
"Thanks for visiting\n"
)
def test_first_run_saves_repo_timestamps_without_the_profile_repo( def test_first_run_saves_repo_timestamps_without_the_profile_repo(
gitea, monkeypatch, state_file gitea, monkeypatch, state_file
): ):
+13 -13
View File
@@ -118,7 +118,9 @@ def test_response_that_is_not_a_dict_is_skipped(gitea):
# --- subtract_excluded_files ------------------------------------------------ # --- subtract_excluded_files ------------------------------------------------
def test_every_excluded_file_size_is_subtracted_from_its_language(gitea, monkeypatch): def test_only_this_repos_excluded_files_are_subtracted(gitea, monkeypatch):
# The game repo's aframe.js isn't registered with the fake server, so
# fetching it while handling the guide repo would fail this test.
monkeypatch.setattr( monkeypatch.setattr(
language_stats, language_stats,
"EXCLUDE_FILES", "EXCLUDE_FILES",
@@ -128,14 +130,12 @@ def test_every_excluded_file_size_is_subtracted_from_its_language(gitea, monkeyp
("guide", "home/Vue3-v.5.html", "HTML"), ("guide", "home/Vue3-v.5.html", "HTML"),
], ],
) )
gitea.add("GET", AFRAME_FILE, {"size": 300})
gitea.add("GET", VUE4_FILE, {"size": 100}) gitea.add("GET", VUE4_FILE, {"size": 100})
gitea.add("GET", VUE5_FILE, {"size": 150}) gitea.add("GET", VUE5_FILE, {"size": 150})
totals = {"JavaScript": 1000, "HTML": 500, "Python": 400} langs = {"HTML": 500, "CSS": 400}
assert subtract_excluded_files(get_session(), totals) == { assert subtract_excluded_files(get_session(), "guide", langs) == {
"JavaScript": 700,
"HTML": 250, "HTML": 250,
"Python": 400, "CSS": 400,
} }
@@ -144,10 +144,10 @@ def test_subtracting_an_excluded_file_never_goes_below_zero(gitea, monkeypatch):
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")] language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
) )
gitea.add("GET", AFRAME_FILE, {"size": 5000}) gitea.add("GET", AFRAME_FILE, {"size": 5000})
totals = {"JavaScript": 1000, "Python": 400} langs = {"JavaScript": 1000, "CSS": 400}
assert subtract_excluded_files(get_session(), totals) == { assert subtract_excluded_files(get_session(), "game", langs) == {
"JavaScript": 0, "JavaScript": 0,
"Python": 400, "CSS": 400,
} }
@@ -156,8 +156,8 @@ def test_excluded_file_that_no_longer_exists_is_ignored(gitea, monkeypatch):
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")] language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
) )
gitea.add("GET", AFRAME_FILE, {"message": "not found"}, status=404) gitea.add("GET", AFRAME_FILE, {"message": "not found"}, status=404)
totals = {"JavaScript": 1000} langs = {"JavaScript": 1000}
assert subtract_excluded_files(get_session(), totals) == {"JavaScript": 1000} assert subtract_excluded_files(get_session(), "game", langs) == {"JavaScript": 1000}
def test_network_error_on_an_excluded_file_is_ignored(gitea, monkeypatch): def test_network_error_on_an_excluded_file_is_ignored(gitea, monkeypatch):
@@ -165,8 +165,8 @@ def test_network_error_on_an_excluded_file_is_ignored(gitea, monkeypatch):
language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")] language_stats, "EXCLUDE_FILES", [("game", "static/aframe.js", "JavaScript")]
) )
gitea.add_error("GET", AFRAME_FILE, requests.ConnectionError("connection reset")) gitea.add_error("GET", AFRAME_FILE, requests.ConnectionError("connection reset"))
totals = {"JavaScript": 1000} langs = {"JavaScript": 1000}
assert subtract_excluded_files(get_session(), totals) == {"JavaScript": 1000} assert subtract_excluded_files(get_session(), "game", langs) == {"JavaScript": 1000}
# --- generate_markdown -------------------------------------------------------- # --- generate_markdown --------------------------------------------------------