Files
mxpic_EDA/backend/storage_paths.py
T
2026-06-08 14:48:14 +08:00

50 lines
2.0 KiB
Python

# -----------------------------------------------------------------------------
# Description: Runtime storage path resolution for SQLite data, saved layouts, and temporary exports.
# Inside functions: resolve_database_root
# Developer : Qin Yue @ 2026
# Organization : OptiHK Limited
# -----------------------------------------------------------------------------
import os
BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
REPO_ROOT = os.path.abspath(os.path.join(BACKEND_DIR, ".."))
DEFAULT_DATABASE_ROOT = os.path.abspath(os.path.join(REPO_ROOT, "..", "mxpic_EDA_database"))
def _is_inside_repo(path: str) -> bool:
"""Return True when a candidate runtime data folder lives inside this repo."""
repo_root = os.path.normcase(REPO_ROOT)
candidate = os.path.normcase(os.path.abspath(path))
try:
return os.path.commonpath([repo_root, candidate]) == repo_root
except ValueError:
return False
def resolve_database_root() -> str:
"""Resolve and validate the external runtime database root."""
configured_root = os.environ.get("MXPIC_DATABASE_ROOT", "").strip()
database_root = os.path.abspath(configured_root or DEFAULT_DATABASE_ROOT)
if _is_inside_repo(database_root):
raise RuntimeError(
"Runtime database root must live outside the mxpic_EDA repository. "
f"Resolved path: {database_root}. Move the database beside the repo as "
"mxpic_EDA_database or set MXPIC_DATABASE_ROOT to an external path."
)
if not os.path.isdir(database_root):
raise RuntimeError(
"Runtime database root is missing. Move the existing database folder "
f"beside mxpic_EDA and rename it to mxpic_EDA_database, or set "
f"MXPIC_DATABASE_ROOT to the moved folder. Expected path: {database_root}"
)
return database_root
DATABASE_ROOT = resolve_database_root()
DB_FILE = os.path.join(DATABASE_ROOT, "mxpic_data.db")
EXPORT_ROOT = os.path.join(DATABASE_ROOT, "_exports")