refactor: count files read with one live counter

The check of files on disk and the read of a card's games each drew
their own "read N/M" line and progress events. Both use one counter
now, which only differs by its event kind and its noun.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:04:39 +01:00
1 parent bf34e71804
commit 2502d96734
7 files changed
+97 -109

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@
# and Invoke-WebRequest throws on a cut transfer before the hash is read:
# the site's build rewrites this line there (scripts/sitebootstrap.py).
$mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
$defaultInstallSha256 = "5921275690dded6744c4cc13a2c471cb47e2fe1bac7e8121b285aa0d3973a36b"
$defaultInstallSha256 = "6b715a59ffa1b796490f810f2f5ae71ee26430e6049e08d27c08fd656c1083c7"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
$maximumInstallerBytes = 8MB
+42 -47
View File
@@ -2336,6 +2336,38 @@ def _answer_yes(prompt: str) -> bool:
return _ask(prompt).strip().lower() in ("y", "yes")
class ReadCounter:
"""Files read so far, as event_kind events and on the live line: the
program driving the installer gets the count the terminal gets."""
def __init__(self, total: int, term: Terminal | None, event_kind: str,
noun: str = "") -> None:
self.total = total
self.event_kind = event_kind
self.noun = noun
self.done = 0
self.evented = 0.0
self.drawn = 0.0
live = term is not None and term.live and not term.quiet
self.stream = term.stream if live and term is not None else None
def step(self, final: bool = False) -> None:
now = time.monotonic()
if EVENTS.on and (final or now - self.evented >= EVENT_PROGRESS_INTERVAL):
self.evented = now
EVENTS.emit(self.event_kind, files_done=self.done, files_total=self.total)
if self.stream is not None and (final or now - self.drawn >= 0.1):
self.drawn = now
print(f"\r read {self.done}/{self.total}{self.noun}", end="", file=self.stream,
flush=True)
def end(self) -> None:
if self.total:
self.step(final=True)
if self.stream is not None:
print("\r\033[K", end="", file=self.stream, flush=True)
# --- installer/paths.py ------------------------------------------------------
def _path_characters(value: object, field: str, colons: bool = True) -> str:
@@ -2951,27 +2983,17 @@ def check_local(
verdicts = pool.map(
lambda item: (item[0], _classify_local(item[0], item[1])), present
)
shown = term if term is not None and term.live and not term.quiet else None
last = evented = 0.0
for done, (entry, verdict) in enumerate(verdicts, 1):
# A screen with a bare timer over 6 GB on an SD card read as hung.
counter = ReadCounter(len(present), term, "checking")
for entry, verdict in verdicts:
if verdict == "up_to_date":
up_to_date.append(entry)
else:
mismatched.append(entry)
now = time.monotonic()
# The program driving the installer gets the count the
# terminal gets: a screen with a bare timer over 6 GB on an
# SD card read as hung.
if EVENTS.on and (now - evented >= EVENT_PROGRESS_INTERVAL
or done == len(present)):
evented = now
EVENTS.emit("checking", files_done=done, files_total=len(present))
if shown is not None and (now - last >= 0.1 or done == len(present)):
last = now
print(f"\r read {done}/{len(present)}", end="", file=shown.stream,
flush=True)
if shown is not None:
print("\r\033[K", end="", file=shown.stream, flush=True)
counter.done += 1
if counter.done < counter.total:
counter.step()
counter.end()
return to_download, up_to_date, mismatched
@@ -18024,7 +18046,8 @@ def plan_games(plat: str, bios_path: Path, manifest: dict, folder: Path | None =
docs = _GamesDocs(data, network)
cache_root = cache_folder()
cache = _ScanCache(cache_root / GAMES_SCAN_FILE if cache_root is not None else None)
progress = _GamesProgress(sum(len(games) for _folder, games in found), term)
progress = ReadCounter(sum(len(games) for _folder, games in found), term, "games_reading",
" games")
read = [(game_folder, _read_folder_games(plan, game_folder, games, docs, cache, progress))
for game_folder, games in found]
progress.end()
@@ -18119,34 +18142,6 @@ class _GamesDocs:
return self.arcades[name] or None
class _GamesProgress:
"""The games read so far, as events and on the live line."""
def __init__(self, total: int, term: Terminal | None) -> None:
self.total = total
self.done = 0
self.evented = 0.0
self.drawn = 0.0
live = term is not None and term.live and not term.quiet
self.stream = term.stream if live and term is not None else None
def step(self, final: bool = False) -> None:
now = time.monotonic()
if EVENTS.on and (final or now - self.evented >= EVENT_PROGRESS_INTERVAL):
self.evented = now
EVENTS.emit("games_reading", files_done=self.done, files_total=self.total)
if self.stream is not None and (final or now - self.drawn >= 0.1):
self.drawn = now
print(f"\r read {self.done}/{self.total} games", end="", file=self.stream,
flush=True)
def end(self) -> None:
if self.total:
self.step(final=True)
if self.stream is not None:
print("\r\033[K", end="", file=self.stream, flush=True)
def _folder_readers(row_systems: list[str], profiles: list[str | None],
docs: _GamesDocs) -> set[str]:
"""The cores of a folder's profiles that read its first system."""
@@ -18173,7 +18168,7 @@ def _count_game(plan: GamesPlan, game_folder: GameFolder, row_systems: list[str]
def _read_folder_games(plan: GamesPlan, game_folder: GameFolder, games: list[Path],
docs: _GamesDocs, cache: _ScanCache,
progress: _GamesProgress) -> list[GameFacts]:
progress: ReadCounter) -> list[GameFacts]:
"""Read the games of one folder: the ones that are not support sets."""
row = game_folder.row
row_systems = [str(s) for s in row.get("systems") or []]
+1 -1
View File
@@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins
# curl or wget stop on a cut transfer before the hash is read: the site's
# build rewrites this line there (scripts/sitebootstrap.py).
MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
DEFAULT_INSTALL_SHA256="5921275690dded6744c4cc13a2c471cb47e2fe1bac7e8121b285aa0d3973a36b"
DEFAULT_INSTALL_SHA256="6b715a59ffa1b796490f810f2f5ae71ee26430e6049e08d27c08fd656c1083c7"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
MAX_INSTALLER_BYTES=8388608
+8 -8
View File
@@ -27,8 +27,8 @@
{
"dest": "roms/ports/retrobios/install.py",
"repo_path": "install.py",
"size": 1450902,
"sha256": "5921275690dded6744c4cc13a2c471cb47e2fe1bac7e8121b285aa0d3973a36b",
"size": 1450457,
"sha256": "6b715a59ffa1b796490f810f2f5ae71ee26430e6049e08d27c08fd656c1083c7",
"executable": false
},
{
@@ -71,8 +71,8 @@
"muos": {
"archive": {
"dest": "ARCHIVE/RetroBIOS.muxapp",
"size": 1504527,
"sha256": "657b9c3a63251ef77487ca4e819a66b48219163c36e46d4e53f503f16d9d7ade"
"size": 1504082,
"sha256": "cd9bb5cabc3ea5f8e3d7f0eaf74d03dc04f51e2a89442da09963cfb4b9362fdf"
},
"members": [
{
@@ -92,8 +92,8 @@
{
"name": "RetroBIOS/install.py",
"repo_path": "install.py",
"size": 1450902,
"sha256": "5921275690dded6744c4cc13a2c471cb47e2fe1bac7e8121b285aa0d3973a36b",
"size": 1450457,
"sha256": "6b715a59ffa1b796490f810f2f5ae71ee26430e6049e08d27c08fd656c1083c7",
"executable": false
},
{
@@ -166,8 +166,8 @@
{
"dest": ".config/modules/retrobios/install.py",
"repo_path": "install.py",
"size": 1450902,
"sha256": "5921275690dded6744c4cc13a2c471cb47e2fe1bac7e8121b285aa0d3973a36b",
"size": 1450457,
"sha256": "6b715a59ffa1b796490f810f2f5ae71ee26430e6049e08d27c08fd656c1083c7",
"executable": false
},
{
+32
View File
@@ -273,3 +273,35 @@ def _confirm(prompt: str) -> bool:
def _answer_yes(prompt: str) -> bool:
"""Yes only to y or yes: a removal is never the default answer."""
return _ask(prompt).strip().lower() in ("y", "yes")
class ReadCounter:
"""Files read so far, as event_kind events and on the live line: the
program driving the installer gets the count the terminal gets."""
def __init__(self, total: int, term: Terminal | None, event_kind: str,
noun: str = "") -> None:
self.total = total
self.event_kind = event_kind
self.noun = noun
self.done = 0
self.evented = 0.0
self.drawn = 0.0
live = term is not None and term.live and not term.quiet
self.stream = term.stream if live and term is not None else None
def step(self, final: bool = False) -> None:
now = time.monotonic()
if EVENTS.on and (final or now - self.evented >= EVENT_PROGRESS_INTERVAL):
self.evented = now
EVENTS.emit(self.event_kind, files_done=self.done, files_total=self.total)
if self.stream is not None and (final or now - self.drawn >= 0.1):
self.drawn = now
print(f"\r read {self.done}/{self.total}{self.noun}", end="", file=self.stream,
flush=True)
def end(self) -> None:
if self.total:
self.step(final=True)
if self.stream is not None:
print("\r\033[K", end="", file=self.stream, flush=True)
+5 -33
View File
@@ -8,17 +8,16 @@ import json
import os
import re
import sys
import time
import urllib.error
from collections.abc import Callable, Collection, Iterable
from pathlib import Path
from typing import TYPE_CHECKING
from installer.cards import games_part
from installer.console import detail, fold_line
from installer.console import ReadCounter, detail, fold_line
from installer.destination import card_root_above
from installer.detect_os import detect_os, detected_os
from installer.events import EVENTS, EVENT_PROGRESS_INTERVAL
from installer.events import EVENTS
from installer.game_files import (
ARCADE_SUFFIXES,
GAMES_SCAN_FILE,
@@ -478,7 +477,8 @@ def plan_games(plat: str, bios_path: Path, manifest: dict, folder: Path | None =
docs = _GamesDocs(data, network)
cache_root = cache_folder()
cache = _ScanCache(cache_root / GAMES_SCAN_FILE if cache_root is not None else None)
progress = _GamesProgress(sum(len(games) for _folder, games in found), term)
progress = ReadCounter(sum(len(games) for _folder, games in found), term, "games_reading",
" games")
read = [(game_folder, _read_folder_games(plan, game_folder, games, docs, cache, progress))
for game_folder, games in found]
progress.end()
@@ -573,34 +573,6 @@ class _GamesDocs:
return self.arcades[name] or None
class _GamesProgress:
"""The games read so far, as events and on the live line."""
def __init__(self, total: int, term: Terminal | None) -> None:
self.total = total
self.done = 0
self.evented = 0.0
self.drawn = 0.0
live = term is not None and term.live and not term.quiet
self.stream = term.stream if live and term is not None else None
def step(self, final: bool = False) -> None:
now = time.monotonic()
if EVENTS.on and (final or now - self.evented >= EVENT_PROGRESS_INTERVAL):
self.evented = now
EVENTS.emit("games_reading", files_done=self.done, files_total=self.total)
if self.stream is not None and (final or now - self.drawn >= 0.1):
self.drawn = now
print(f"\r read {self.done}/{self.total} games", end="", file=self.stream,
flush=True)
def end(self) -> None:
if self.total:
self.step(final=True)
if self.stream is not None:
print("\r\033[K", end="", file=self.stream, flush=True)
def _folder_readers(row_systems: list[str], profiles: list[str | None],
docs: _GamesDocs) -> set[str]:
"""The cores of a folder's profiles that read its first system."""
@@ -627,7 +599,7 @@ def _count_game(plan: GamesPlan, game_folder: GameFolder, row_systems: list[str]
def _read_folder_games(plan: GamesPlan, game_folder: GameFolder, games: list[Path],
docs: _GamesDocs, cache: _ScanCache,
progress: _GamesProgress) -> list[GameFacts]:
progress: ReadCounter) -> list[GameFacts]:
"""Read the games of one folder: the ones that are not support sets."""
row = game_folder.row
row_systems = [str(s) for s in row.get("systems") or []]
+8 -19
View File
@@ -3,11 +3,10 @@ from __future__ import annotations
import concurrent.futures
import hashlib
import time
from pathlib import Path
from typing import IO, TYPE_CHECKING
from installer.events import EVENTS, EVENT_PROGRESS_INTERVAL
from installer.console import ReadCounter
from installer.host_fs import _held
from installer.paths import _entry_path
@@ -152,26 +151,16 @@ def check_local(
verdicts = pool.map(
lambda item: (item[0], _classify_local(item[0], item[1])), present
)
shown = term if term is not None and term.live and not term.quiet else None
last = evented = 0.0
for done, (entry, verdict) in enumerate(verdicts, 1):
# A screen with a bare timer over 6 GB on an SD card read as hung.
counter = ReadCounter(len(present), term, "checking")
for entry, verdict in verdicts:
if verdict == "up_to_date":
up_to_date.append(entry)
else:
mismatched.append(entry)
now = time.monotonic()
# The program driving the installer gets the count the
# terminal gets: a screen with a bare timer over 6 GB on an
# SD card read as hung.
if EVENTS.on and (now - evented >= EVENT_PROGRESS_INTERVAL
or done == len(present)):
evented = now
EVENTS.emit("checking", files_done=done, files_total=len(present))
if shown is not None and (now - last >= 0.1 or done == len(present)):
last = now
print(f"\r read {done}/{len(present)}", end="", file=shown.stream,
flush=True)
if shown is not None:
print("\r\033[K", end="", file=shown.stream, flush=True)
counter.done += 1
if counter.done < counter.total:
counter.step()
counter.end()
return to_download, up_to_date, mismatched