Refactor environment and configuration management

- Updated the project structure to store configuration files (.env, models.yaml, extensions.yaml) in the project root instead of the user's home directory.
- Enhanced the setup process to automatically copy example files to the project root on first run.
- Implemented a migration function to transfer legacy configuration files from the user's home directory to the new project structure.
- Revised documentation to reflect changes in file locations and setup instructions.
- Improved code readability and maintainability by refactoring path management functions.
This commit is contained in:
Leonid Pershin
2026-08-21 03:20:18 +03:00
parent c2ca39a4a7
commit 343f741baa
22 changed files with 202 additions and 90 deletions
+79 -21
View File
@@ -1,33 +1,91 @@
"""Paths: ~/.gpu-rent, app tree, SSH key."""
"""Paths: project root, local .gpu-rent runtime, SSH key."""
from __future__ import annotations
import shutil
from pathlib import Path
def home_dir() -> Path:
return Path.home() / ".gpu-rent"
def env_path() -> Path:
return home_dir() / ".env"
def state_path() -> Path:
return home_dir() / "state.json"
def lock_path() -> Path:
return home_dir() / "gpu-rent.lock"
def default_ssh_key_path() -> Path:
return home_dir() / "id_ed25519"
def detect_app_root() -> Path:
cwd = Path.cwd().resolve()
for candidate in (cwd, *cwd.parents):
if (candidate / "Models").is_dir() and (candidate / "docs").is_dir():
return candidate
return cwd
def app_root() -> Path:
return detect_app_root()
def runtime_dir() -> Path:
"""State, lock, SSH key — always inside the project (gitignored)."""
return app_root() / ".gpu-rent"
# Back-compat alias used across the package.
def home_dir() -> Path:
return runtime_dir()
def env_path() -> Path:
return app_root() / ".env"
def models_manifest_path() -> Path:
return app_root() / "models.yaml"
def extensions_manifest_path() -> Path:
return app_root() / "extensions.yaml"
def state_path() -> Path:
return runtime_dir() / "state.json"
def lock_path() -> Path:
return runtime_dir() / "gpu-rent.lock"
def default_ssh_key_path() -> Path:
return runtime_dir() / "id_ed25519"
def legacy_user_dir() -> Path:
"""Old location (~/.gpu-rent) — only for one-shot migrate."""
return Path.home() / ".gpu-rent"
def migrate_legacy_if_needed() -> list[str]:
"""Copy ~/.gpu-rent leftovers into the project once. Returns log lines."""
legacy = legacy_user_dir()
if not legacy.is_dir():
return []
root = app_root()
runtime = runtime_dir()
notes: list[str] = []
pairs = (
(legacy / ".env", env_path()),
(legacy / "models.yaml", models_manifest_path()),
(legacy / "extensions.yaml", extensions_manifest_path()),
(legacy / "state.json", state_path()),
(legacy / "id_ed25519", default_ssh_key_path()),
(legacy / "id_ed25519.pub", default_ssh_key_path().with_suffix(".pub")),
)
for src, dst in pairs:
if not src.is_file() or dst.is_file():
continue
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
notes.append(f"перенёс {src}{dst}")
if notes:
runtime.mkdir(parents=True, exist_ok=True)
marker = runtime / ".migrated-from-user-home"
if not marker.is_file():
marker.write_text(
f"from {legacy}\n" + "\n".join(notes) + "\n",
encoding="utf-8",
)
return notes