From 39156a6e3a6dd4e49b431b564a561b64a9fd8e42 Mon Sep 17 00:00:00 2001 From: hp0912 <809211365@qq.com> Date: Sun, 5 Apr 2026 00:23:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=84=9A=E6=9C=AC=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/text-to-image/scripts/bootstrap.py | 60 ++++++++++++++++++- skills/text-to-image/scripts/text_to_image.py | 36 ++++++++++- 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/skills/text-to-image/scripts/bootstrap.py b/skills/text-to-image/scripts/bootstrap.py index 5a1a738..357c37d 100644 --- a/skills/text-to-image/scripts/bootstrap.py +++ b/skills/text-to-image/scripts/bootstrap.py @@ -9,16 +9,72 @@ from pathlib import Path sys.stderr = sys.stdout + +def _skill_root_from(script_dir: Path) -> Path: + return script_dir.parent + + +def _venv_dir(script_dir: Path) -> Path: + return _skill_root_from(script_dir) / ".venv" + + +def _venv_python(venv_dir: Path) -> Path: + if sys.platform == "win32": + return venv_dir / "Scripts" / "python.exe" + return venv_dir / "bin" / "python" + + +def _ensure_venv(venv_dir: Path, venv_python: Path) -> int: + if venv_python.is_file(): + return 0 + + sys.stdout.write(f"未检测到技能虚拟环境,正在创建: {venv_dir}\n") + command = [ + sys.executable, + "-m", + "venv", + str(venv_dir), + ] + + try: + subprocess.run(command, check=True, stdout=sys.stdout, stderr=sys.stdout) + except subprocess.CalledProcessError as exc: + sys.stdout.write(f"创建虚拟环境失败,退出码: {exc.returncode}\n") + return exc.returncode or 1 + + return 0 + def main() -> int: script_dir = Path(__file__).resolve().parent requirements_file = script_dir / "requirements.txt" + venv_dir = _venv_dir(script_dir) + venv_python = _venv_python(venv_dir) if not requirements_file.is_file(): sys.stdout.write(f"未找到依赖文件: {requirements_file}\n") return 1 + ensure_result = _ensure_venv(venv_dir, venv_python) + if ensure_result != 0: + return ensure_result + command = [ - sys.executable, + str(venv_python), + "-m", + "pip", + "install", + "--upgrade", + "pip", + ] + + try: + subprocess.run(command, check=True, stdout=sys.stdout, stderr=sys.stdout) + except subprocess.CalledProcessError as exc: + sys.stdout.write(f"升级 pip 失败,退出码: {exc.returncode}\n") + return exc.returncode or 1 + + command = [ + str(venv_python), "-m", "pip", "install", @@ -32,7 +88,7 @@ def main() -> int: sys.stdout.write(f"安装依赖失败,退出码: {exc.returncode}\n") return exc.returncode or 1 - sys.stdout.write("依赖安装完成\n") + sys.stdout.write(f"依赖安装完成,当前技能虚拟环境: {venv_dir}\n") return 0 diff --git a/skills/text-to-image/scripts/text_to_image.py b/skills/text-to-image/scripts/text_to_image.py index d12dbc4..c9d1b08 100644 --- a/skills/text-to-image/scripts/text_to_image.py +++ b/skills/text-to-image/scripts/text_to_image.py @@ -9,11 +9,45 @@ import sys import time import traceback import urllib.request +from pathlib import Path # The skill runner consumes stdout, so route Python error output there as well. sys.stderr = sys.stdout -import pymysql # type: ignore # noqa: E402 + +def _skill_root() -> Path: + script_dir = Path(__file__).resolve().parent + return script_dir.parent + + +def _skill_venv_python() -> Path: + venv_dir = _skill_root() / ".venv" + if sys.platform == "win32": + return venv_dir / "Scripts" / "python.exe" + return venv_dir / "bin" / "python" + + +def _ensure_skill_venv_python() -> None: + venv_python = _skill_venv_python() + if not venv_python.is_file(): + return + + current_python = Path(sys.executable).resolve() + if current_python == venv_python.resolve(): + return + + os.execv(str(venv_python), [str(venv_python), str(Path(__file__).resolve()), *sys.argv[1:]]) + + +_ensure_skill_venv_python() + +try: + import pymysql # type: ignore # noqa: E402 +except ModuleNotFoundError: + sys.stdout.write( + "缺少依赖 pymysql,请先执行 python3 text-to-image/scripts/bootstrap.py 安装当前 skill 的依赖\n" + ) + raise SystemExit(1) # ---------------------------------------------------------------------------