From 4e86e1c93deadc7bb09c200f5eed65194225e7df Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 7 Nov 2025 14:04:47 +0000 Subject: [PATCH] Address code review feedback - Made package-data configuration more specific to langbot package only - Improved path detection with caching to avoid repeated file I/O - Removed sys.path searching which was incorrect for package data - Removed interactive input() call for non-interactive environment compatibility - Simplified error messages for version check Co-authored-by: RockChinQ <45992437+RockChinQ@users.noreply.github.com> --- langbot/__main__.py | 4 +-- pkg/utils/paths.py | 68 ++++++++++++++++++++++++++++----------------- pyproject.toml | 2 +- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/langbot/__main__.py b/langbot/__main__.py index 8c4b6eb5..69833d1a 100644 --- a/langbot/__main__.py +++ b/langbot/__main__.py @@ -79,8 +79,8 @@ def main(): # Check Python version if sys.version_info < (3, 10, 1): print('需要 Python 3.10.1 及以上版本,当前 Python 版本为:', sys.version) - input('按任意键退出...') - print('Your Python version is not supported. Please exit the program by pressing any key.') + print('Your Python version is not supported.') + print('Python 3.10.1 or higher is required. Current version:', sys.version) sys.exit(1) # Set up the working directory diff --git a/pkg/utils/paths.py b/pkg/utils/paths.py index d4f4a404..379ac63c 100644 --- a/pkg/utils/paths.py +++ b/pkg/utils/paths.py @@ -4,6 +4,35 @@ import sys from pathlib import Path +_is_source_install = None + + +def _check_if_source_install() -> bool: + """ + Check if we're running from source directory or an installed package. + Cached to avoid repeated file I/O. + """ + global _is_source_install + + if _is_source_install is not None: + return _is_source_install + + # Check if main.py exists in current directory with LangBot marker + if os.path.exists('main.py'): + try: + with open('main.py', 'r', encoding='utf-8') as f: + # Only read first 500 chars to check for marker + content = f.read(500) + if 'LangBot/main.py' in content: + _is_source_install = True + return True + except Exception: + pass + + _is_source_install = False + return False + + def get_frontend_path() -> str: """ Get the path to the frontend build files. @@ -13,32 +42,21 @@ def get_frontend_path() -> str: - Package mode: installed via pip/uvx """ # First, check if we're running from source directory - # (main.py exists in current directory) - if os.path.exists('main.py') and os.path.exists('web/out'): - with open('main.py', 'r', encoding='utf-8') as f: - content = f.read() - if 'LangBot/main.py' in content: - return 'web/out' + if _check_if_source_install() and os.path.exists('web/out'): + return 'web/out' - # Second, check if we're in a development/source directory - # (current directory has web/out) + # Second, check current directory for web/out (in case user is in source dir) if os.path.exists('web/out'): return 'web/out' - # Third, try to find it relative to this file's location - # This handles the case when installed as a package + # Third, find it relative to the package installation + # Get the directory where this file is located + # paths.py is in pkg/utils/, so go up 3 levels to get to the package root pkg_dir = Path(__file__).parent.parent.parent frontend_path = pkg_dir / 'web' / 'out' if frontend_path.exists(): return str(frontend_path) - # Fourth, check if it's in the package installation directory - # Look for the web/out directory in the sys.path - for path in sys.path: - candidate = Path(path) / 'web' / 'out' - if candidate.exists(): - return str(candidate) - # Return the default path (will be checked by caller) return 'web/out' @@ -53,21 +71,21 @@ def get_resource_path(resource: str) -> str: Returns: Absolute path to the resource """ - # First, check if resource exists in current directory + # First, check if resource exists in current directory (source install) + if _check_if_source_install() and os.path.exists(resource): + return resource + + # Second, check current directory anyway if os.path.exists(resource): return resource - # Second, try to find it relative to package directory + # Third, find it relative to package directory + # Get the directory where this file is located + # paths.py is in pkg/utils/, so go up 3 levels to get to the package root pkg_dir = Path(__file__).parent.parent.parent resource_path = pkg_dir / resource if resource_path.exists(): return str(resource_path) - # Third, check in sys.path - for path in sys.path: - candidate = Path(path) / resource - if candidate.exists(): - return str(candidate) - # Return the original path return resource diff --git a/pyproject.toml b/pyproject.toml index 9f6d425c..29ccd057 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -112,7 +112,7 @@ packages = { find = {} } include-package-data = true [tool.setuptools.package-data] -"*" = [ +langbot = [ "web/out/**/*", "templates/**/*", "res/**/*",