From fa155766080f126f495644df6f179f7564a129d2 Mon Sep 17 00:00:00 2001 From: arabcoders Date: Tue, 2 Sep 2025 04:59:07 +0300 Subject: [PATCH] Refactor logging setup and enhance user site directory management in PackageInstaller and Upgrader --- app/library/PackageInstaller.py | 28 +++++++++++++++++++++++++--- app/library/config.py | 12 ++++++++---- app/upgrader.py | 14 ++++++++++++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/library/PackageInstaller.py b/app/library/PackageInstaller.py index b0ed0463..b736123c 100644 --- a/app/library/PackageInstaller.py +++ b/app/library/PackageInstaller.py @@ -8,7 +8,17 @@ from pathlib import Path import httpx -LOG = logging.getLogger("package_installer") +LOG: logging.Logger = logging.getLogger("package_installer") + +if base_dir := os.environ.get("YTP_CONFIG_PATH"): + base_dir = Path(base_dir) + user_site: Path = base_dir / f"python{sys.version_info.major}.{sys.version_info.minor}-packages" + + if not user_site.exists(): + user_site.mkdir(parents=True, exist_ok=True) + + if user_site.is_dir() and str(user_site) not in sys.path: + sys.path.insert(0, str(user_site)) def parse_version(v: str) -> tuple[int, ...]: @@ -113,13 +123,16 @@ class PackageInstaller: cmd.extend(["--disable-pip-version-check", pkg]) try: - subprocess.run( + proc: subprocess.CompletedProcess[bytes] = subprocess.run( cmd, check=True, + capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0, ) + self.out(out=proc.stdout, err=proc.stderr) except subprocess.CalledProcessError as e: - LOG.error(f"Failed to install package '{pkg}'. Error: {e}") + LOG.error(f"Failed to install '{pkg}' (exit {e.returncode}). {e!s}") + self.out(out=e.stdout, err=e.stderr) raise def check(self, pkgs: Packages): @@ -165,3 +178,12 @@ class PackageInstaller: return True return False + + def out(self, out: bytes | str | None = None, err=bytes | str | None) -> None: + if out: + for line in (line.strip() for line in out.strip().splitlines() if line.strip()): + LOG.info(line.decode() if isinstance(line, bytes) else line) + + if err: + for line in (line.strip() for line in err.strip().splitlines() if line.strip()): + LOG.warning(line.decode() if isinstance(line, bytes) else line) diff --git a/app/library/config.py b/app/library/config.py index dc3d68b0..d70ca70c 100644 --- a/app/library/config.py +++ b/app/library/config.py @@ -7,6 +7,7 @@ import time from logging.handlers import TimedRotatingFileHandler from multiprocessing.managers import SyncManager from pathlib import Path +from typing import TYPE_CHECKING import coloredlogs from dotenv import load_dotenv @@ -14,6 +15,9 @@ from dotenv import load_dotenv from .Utils import FileLogFormatter, arg_converter from .version import APP_BRANCH, APP_BUILD_DATE, APP_COMMIT_SHA, APP_VERSION +if TYPE_CHECKING: + from subprocess import CompletedProcess + class Config: app_env: str = "production" @@ -558,8 +562,8 @@ class Config: try: import subprocess - branch_result = subprocess.run( - ["git", "rev-parse", "--abbrev-ref", "HEAD"], # noqa: S607 + branch_result: CompletedProcess[str] = subprocess.run( + ["git", "-c", f"safe.directory={git_path.parent!s}", "rev-parse", "--abbrev-ref", "HEAD"], # noqa: S607 cwd=os.path.dirname(git_path), capture_output=True, text=True, @@ -576,8 +580,8 @@ class Config: logging.warning("Git branch name is empty.") return - commit_result = subprocess.run( - ["git", "log", "-1", "--format=%ct_%H"], # noqa: S607 + commit_result: CompletedProcess[str] = subprocess.run( + ["git", "-c", f"safe.directory={git_path.parent!s}", "log", "-1", "--format=%ct_%H"], # noqa: S607 cwd=os.path.dirname(git_path), capture_output=True, text=True, diff --git a/app/upgrader.py b/app/upgrader.py index ec86102f..abd98dbd 100644 --- a/app/upgrader.py +++ b/app/upgrader.py @@ -13,7 +13,17 @@ from pathlib import Path from dotenv import load_dotenv from library.PackageInstaller import PackageInstaller, Packages -LOG = logging.getLogger("upgrader") +LOG: logging.Logger = logging.getLogger("upgrader") + +if base_dir := os.environ.get("YTP_CONFIG_PATH"): + base_dir = Path(base_dir) + user_site: Path = base_dir / f"python{sys.version_info.major}.{sys.version_info.minor}-packages" + + if not user_site.exists(): + user_site.mkdir(parents=True, exist_ok=True) + + if user_site.is_dir() and str(user_site) not in sys.path: + sys.path.insert(0, str(user_site)) class Upgrader: @@ -59,7 +69,7 @@ class Upgrader: Packages( env=os.environ.get("YTP_PIP_PACKAGES", None), file=str(Path(config_path, "pip.txt")), - upgrade=not bool(os.environ.get("YTP_PIP_IGNORE_UPDATES", False)), + upgrade=not bool(os.environ.get("YTP_PIP_IGNORE_UPDATES", None)), ) ) except Exception as e: