Files
gitlab-instance-0a899031_do…/scripts/env_check.py
2026-03-28 18:10:43 -05:00

92 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""启动前环境分析Python 依赖、ffmpeg、Node/npm、端口提示Linux"""
from __future__ import annotations
import importlib.util
import shutil
import socket
import sys
def _has_module(name: str) -> bool:
return importlib.util.find_spec(name) is not None
def _local_ips() -> list[str]:
out: list[str] = []
try:
hostname = socket.gethostname()
for info in socket.getaddrinfo(hostname, None, family=socket.AF_INET):
ip = info[4][0]
if ip and ip != "127.0.0.1" and ip not in out:
out.append(ip)
except OSError:
pass
return out[:4]
def print_env_report(
*,
dev: bool,
need_npm: bool,
host: str,
port: str,
next_port: str = "3000",
) -> None:
print()
print("┌── 环境检测 ─────────────────────────────────────────")
print(f"│ 系统: {sys.platform} Python: {sys.version.split()[0]}")
ok = True
for mod, label in (
("fastapi", "FastAPI"),
("uvicorn", "Uvicorn"),
):
if _has_module(mod):
print(f"│ ✓ {label} 已安装")
else:
print(f"│ ✕ 缺少 {label},请执行: pip install -r requirements.txt")
ok = False
ff = shutil.which("ffmpeg")
fp = shutil.which("ffplay")
if ff:
print(f"│ ✓ ffmpeg: {ff}")
else:
print("│ ⚠ 未找到 ffmpeg推流/录制/OBS 拉流会失败Docker 镜像已内置)")
ok = False
if fp:
print(f"│ ✓ ffplay: {fp}")
else:
print("│ ⚠ 未找到 ffplayHDMI 全屏播放需要完整 ffmpeg 套件)")
if need_npm:
npm = shutil.which("npm")
if npm:
print(f"│ ✓ npm: {npm}")
else:
print("│ ✕ 未找到 npm构建/开发前端需要安装 Node.js")
ok = False
print("")
print("│ 同机端口提示(若冲突请在 config/runtime.env 修改 PORT")
print("│ 本控制台 API/Web 使用环境变量 PORT默认见 launch.py")
print("│ SRS 常见: 1935/1985/8080/10080 等;代理常见: 7890/9090Chrome 远程: 9222")
print("")
print(f"│ 监听: {host}:{port}")
if dev:
ips = _local_ips()
print(f"│ 开发 UI: 0.0.0.0:{next_port}(局域网可用本机 IP 访问)")
if ips:
print(f"│ 示例: http://{ips[0]}:{next_port}/")
else:
if host == "0.0.0.0":
ips = _local_ips()
if ips:
print(f"│ 局域网访问示例: http://{ips[0]}:{port}/")
else:
print("│ 局域网访问: http://<本机IP>:%s/" % port)
print("└────────────────────────────────────────────────────")
print()
if not ok and not dev:
print("[env] 存在警告,仍可尝试启动;若运行失败请按上述提示补全依赖。\n")