From e8144e9591639c9638ee00413a44d74c6a97b1e8 Mon Sep 17 00:00:00 2001 From: Fengqing Liu Date: Sun, 19 Oct 2025 16:27:12 +1100 Subject: [PATCH] chore: Refactor environment detection and paths; remove unused variables and improve resource path handling --- CLAUDE.md | 5 +++-- Dockerfile | 4 ++-- src/__main__.py | 8 -------- src/config/__init__.py | 2 -- src/config/paths.py | 30 +++--------------------------- 5 files changed, 8 insertions(+), 41 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 88367ec..2fe7a2e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -271,9 +271,10 @@ The application uses a web-based interface accessible via browser: **src/config/paths.py:** - Detects Docker environment via `DOCKER_ENV` env var or `/.dockerenv` file -- Uses `/app` for code, `/app/data` for persistent storage +- Docker: Uses `/app` for code, `/app/data` for persistent storage +- Development: Uses `/data` for persistent storage - All user data (cookies, settings, cache, logs) stored in DATA_DIR -- Provides `_resource_path()` helper for locating resources +- Provides `_resource_path()` helper for locating bundled resources **Dockerfile:** - Based on `python:3.11-slim` diff --git a/Dockerfile b/Dockerfile index 6ea35fc..d154b5d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.11-slim +FROM python:3 # Build arguments for metadata ARG BUILD_DATE @@ -55,4 +55,4 @@ HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/api/status')" || exit 1 # Run the application (web GUI is now default) -CMD ["python", "main.py", "-v"] +CMD ["python", "main.py"] diff --git a/src/__main__.py b/src/__main__.py index d6425ff..7d977e5 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,11 +1,6 @@ from __future__ import annotations -# import an additional thing for proper PyInstaller freeze support -from multiprocessing import freeze_support - - if __name__ == "__main__": - freeze_support() import argparse import asyncio import logging @@ -39,9 +34,6 @@ if __name__ == "__main__": warnings.simplefilter("default", ResourceWarning) - # import tracemalloc - # tracemalloc.start(3) - if sys.version_info < (3, 10): raise RuntimeError("Python 3.10 or higher is required") diff --git a/src/config/__init__.py b/src/config/__init__.py index f8c5158..9bb8a3d 100644 --- a/src/config/__init__.py +++ b/src/config/__init__.py @@ -39,7 +39,6 @@ from .paths import ( COOKIES_PATH, DATA_DIR, DUMP_PATH, - IS_APPIMAGE, IS_DOCKER, IS_PACKAGED, LANG_PATH, @@ -86,7 +85,6 @@ __all__ = [ "WATCH_INTERVAL", "WINDOW_TITLE", # paths.py - "IS_APPIMAGE", "IS_PACKAGED", "IS_DOCKER", "SYS_SITE_PACKAGES", diff --git a/src/config/paths.py b/src/config/paths.py index 6d2362c..99d65af 100644 --- a/src/config/paths.py +++ b/src/config/paths.py @@ -13,8 +13,6 @@ JsonType = dict[str, Any] # Environment detection -IS_APPIMAGE = "APPIMAGE" in os.environ and os.path.exists(os.environ["APPIMAGE"]) -IS_PACKAGED = hasattr(sys, "_MEIPASS") or IS_APPIMAGE IS_DOCKER = os.getenv("DOCKER_ENV") == "1" or os.path.exists("/.dockerenv") # Site-packages venv path changes depending on the system platform @@ -36,17 +34,8 @@ else: def _resource_path(relative_path: Path | str) -> Path: """ Get an absolute path to a bundled resource. - - Works for dev and for PyInstaller. """ - if IS_APPIMAGE: - base_path = Path(sys.argv[0]).resolve().parent - elif IS_PACKAGED: - # PyInstaller's folder where the one-file app is unpacked - meipass: str = sys._MEIPASS # type: ignore[attr-defined] - base_path = Path(meipass) - else: - base_path = WORKING_DIR + base_path = WORKING_DIR return base_path.joinpath(relative_path) @@ -84,22 +73,9 @@ if IS_DOCKER: SELF_PATH = Path("/app/main.py") WORKING_DIR = Path("/app") DATA_DIR = Path("/app/data") -elif IS_APPIMAGE: - SELF_PATH = Path(os.environ["APPIMAGE"]).resolve() - WORKING_DIR = SELF_PATH.parent - DATA_DIR = WORKING_DIR -else: - # NOTE: pyinstaller will set sys.argv[0] to its own executable when building - # NOTE: sys.argv[0] will point to gui.py when running the gui.py directly for GUI debug - # detect these and use __file__ and main.py redirection instead - SELF_PATH = Path(sys.argv[0]).resolve() - if SELF_PATH.stem == "pyinstaller" or SELF_PATH.name == "gui.py": - SELF_PATH = Path(__file__).with_name("main.py").resolve() - WORKING_DIR = SELF_PATH.parent - DATA_DIR = WORKING_DIR -# Ensure data directory exists in Docker -if IS_DOCKER and not DATA_DIR.exists(): +# Ensure data directory exists +if not DATA_DIR.exists(): DATA_DIR.mkdir(parents=True, exist_ok=True) # Development paths