fix: name a missing database in one error line

This commit is contained in:
Abdessamad Derraz committed 2026-10-18 08:34:09 +02:00
1 parent 9579f6c449
commit 4240c98359
7 files changed
+78 -16

No files matched your search

+26 -3
View File
@@ -31,10 +31,30 @@ except ImportError:
class DatabaseUnreadable(FileNotFoundError):
"""The database a command was pointed at does not exist or is not JSON."""
def load_database(db_path: str) -> dict:
"""Load database.json and return parsed dict."""
with open(db_path) as f:
return json.load(f)
"""Load database.json and return parsed dict.
A missing or damaged file raises DatabaseUnreadable, which run_cli turns
into one line saying how to build it: `--db nosuch.json` ended at the
bottom of a stack trace.
"""
try:
with open(db_path) as f:
return json.load(f)
except FileNotFoundError:
raise DatabaseUnreadable(
f"{db_path} not found. Build it with: python scripts/generate_db.py "
"--force --bios-dir bios --output database.json"
) from None
except json.JSONDecodeError as exc:
raise DatabaseUnreadable(
f"{db_path} is not valid JSON ({exc}). Rebuild it with: "
"python scripts/generate_db.py --force --bios-dir bios --output database.json"
) from None
@@ -2501,6 +2521,9 @@ def run_cli(main: Callable[[], int | None]) -> None:
try:
code = main()
sys.stdout.flush()
except DatabaseUnreadable as exc:
print(f"ERROR: {exc}", file=sys.stderr)
sys.exit(1)
except BrokenPipeError:
os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())
sys.exit(141)
+1 -2
View File
@@ -2426,8 +2426,7 @@ def _run_verify_packs(args: argparse.Namespace) -> None:
second implementation here answered a narrower one and reported OK while
the core extras went unchecked.
"""
with open(args.db) as f:
verify_db = json.load(f)
verify_db = load_database(args.db)
if args.platform:
platforms = [args.platform]
+2 -1
View File
@@ -78,8 +78,9 @@ def main(argv: list[str] | None = None) -> None:
profiles = load_emulator_profiles(args.emulators_dir)
# Load database (optional)
# The default may be absent; a --db that was named must exist.
db: dict | None = None
if os.path.exists(args.db):
if args.db != DEFAULT_DB_FILE or os.path.exists(args.db):
db = load_database(args.db)
# Determine platforms
+2 -2
View File
@@ -807,6 +807,7 @@ def main() -> int:
from common import (
list_registered_platforms,
load_database,
load_emulator_profiles,
load_platform_config,
require_platform,
@@ -830,8 +831,7 @@ def main() -> int:
)
args = parser.parse_args()
with open(args.db, encoding="utf-8") as handle:
db = json.load(handle)
db = load_database(args.db)
profiles = load_emulator_profiles(args.emulators_dir)
if args.platform:
require_platform(args.platform, args.platforms_dir)
+8 -6
View File
@@ -326,13 +326,15 @@ def _files_to_validate(
def _load_database_if_any(path: str) -> dict | None:
try:
return load_database(path)
except FileNotFoundError:
return None
except json.JSONDecodeError as e:
print(f"WARNING: corrupt database.json: {e}", file=sys.stderr)
"""The database, optional only where the default is meant.
A mistyped --db read as "no database" and every file passed its
duplicate and placement checks unchecked; a damaged one did the same
after a warning.
"""
if path == DEFAULT_DB and not os.path.exists(path):
return None
return load_database(path)
def main() -> None:
+2 -2
View File
@@ -56,6 +56,7 @@ from common import (
list_emulator_profiles,
list_system_ids,
load_data_dir_registry,
load_database,
load_emulator_profiles,
load_platform_config,
md5sum,
@@ -2178,8 +2179,7 @@ def _main(held: contextlib.ExitStack) -> None:
hold_data_dirs(held, load_data_dir_registry(args.platforms_dir))
with open(args.db) as f:
db = json.load(f)
db = load_database(args.db)
# Emulator mode
if args.emulator: