fix: 脚本路径
This commit is contained in:
parent
4e54528b3c
commit
39156a6e3a
@ -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
|
||||
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Loading…
Reference in New Issue
Block a user