- 新增 GitHub Issue 模板(Bug 报告、功能请求)和 Pull Request 模板 - 新增 Code of Conduct(贡献者行为准则)和 Security Policy(安全政策) - 新增 CI 工作流(GitHub Actions),包含 ruff 代码检查和导入验证 - 新增开发依赖文件 requirements-dev.txt 📦 build(ci): 配置 GitHub Actions 持续集成 - 在 push 到 main 分支和 pull request 时自动触发 CI - 添加 lint 任务执行 ruff 代码风格检查 - 添加 import-check 任务验证核心服务模块导入 ♻️ refactor(structure): 重构项目目录结构 - 将根目录的 6 个服务模块迁移至 services/ 包 - 更新所有相关文件的导入语句(main.py、ui/、services/) - 根目录仅保留 main.py 作为唯一 Python 入口文件 🔧 chore(config): 调整配置和资源文件路径 - 将 config.json 移至 config/ 目录,更新相关引用 - 将个人头像图片移至 assets/faces/ 目录,更新 .gitignore - 更新 Dockerfile 和 docker-compose.yml 中的配置路径 📝 docs(readme): 完善 README 文档 - 添加项目状态徽章(Python 版本、License、CI) - 更新项目结构图反映实际目录布局 - 修正使用指南中的 Tab 名称和操作路径 - 替换 your-username 占位符为格式提示 🗑️ chore(cleanup): 清理冗余文件 - 删除旧版备份文件、测试脚本、临时记录和运行日志 - 删除散落的个人图片文件(已归档至 assets/faces/)
123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
"""
|
||
services/autostart.py
|
||
Windows 开机自启管理
|
||
"""
|
||
import os
|
||
import platform
|
||
import logging
|
||
|
||
logger = logging.getLogger("autobot")
|
||
|
||
_APP_NAME = "XHS_AI_AutoBot"
|
||
_STARTUP_REG_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
|
||
|
||
|
||
def _get_startup_script_path() -> str:
|
||
"""获取启动脚本路径(.vbs 静默启动,不弹黑窗)"""
|
||
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "scripts", "_autostart.vbs")
|
||
|
||
|
||
def _get_startup_bat_path() -> str:
|
||
"""获取启动 bat 路径"""
|
||
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "scripts", "_autostart.bat")
|
||
|
||
|
||
def _create_startup_scripts():
|
||
"""创建静默启动脚本(bat + vbs)"""
|
||
app_dir = os.path.dirname(os.path.abspath(os.path.join(__file__, "..")))
|
||
# __file__ is services/autostart.py, so app_dir should be parent
|
||
app_dir = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
|
||
venv_python = os.path.join(app_dir, ".venv", "Scripts", "pythonw.exe")
|
||
# 如果没有 pythonw,退回 python.exe
|
||
if not os.path.exists(venv_python):
|
||
venv_python = os.path.join(app_dir, ".venv", "Scripts", "python.exe")
|
||
main_script = os.path.join(app_dir, "main.py")
|
||
|
||
# 创建 bat
|
||
bat_path = _get_startup_bat_path()
|
||
os.makedirs(os.path.dirname(bat_path), exist_ok=True)
|
||
bat_content = f"""@echo off
|
||
cd /d "{app_dir}"
|
||
"{venv_python}" "{main_script}"
|
||
"""
|
||
with open(bat_path, "w", encoding="utf-8") as f:
|
||
f.write(bat_content)
|
||
|
||
# 创建 vbs(静默运行 bat,不弹出命令行窗口)
|
||
vbs_path = _get_startup_script_path()
|
||
vbs_content = f"""Set WshShell = CreateObject("WScript.Shell")
|
||
WshShell.Run chr(34) & "{bat_path}" & chr(34), 0
|
||
Set WshShell = Nothing
|
||
"""
|
||
with open(vbs_path, "w", encoding="utf-8") as f:
|
||
f.write(vbs_content)
|
||
|
||
return vbs_path
|
||
|
||
|
||
def is_autostart_enabled() -> bool:
|
||
"""检查是否已设置开机自启"""
|
||
if platform.system() != "Windows":
|
||
return False
|
||
try:
|
||
import winreg
|
||
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, _STARTUP_REG_KEY, 0, winreg.KEY_READ)
|
||
try:
|
||
val, _ = winreg.QueryValueEx(key, _APP_NAME)
|
||
winreg.CloseKey(key)
|
||
return bool(val)
|
||
except FileNotFoundError:
|
||
winreg.CloseKey(key)
|
||
return False
|
||
except Exception:
|
||
return False
|
||
|
||
|
||
def enable_autostart() -> str:
|
||
"""启用 Windows 开机自启"""
|
||
if platform.system() != "Windows":
|
||
return "❌ 此功能仅支持 Windows 系统"
|
||
try:
|
||
import winreg
|
||
vbs_path = _create_startup_scripts()
|
||
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, _STARTUP_REG_KEY, 0, winreg.KEY_SET_VALUE)
|
||
# 用 wscript 运行 vbs 以实现静默启动
|
||
winreg.SetValueEx(key, _APP_NAME, 0, winreg.REG_SZ, f'wscript.exe "{vbs_path}"')
|
||
winreg.CloseKey(key)
|
||
logger.info(f"开机自启已启用: {vbs_path}")
|
||
return "✅ 开机自启已启用\n下次开机时将自动后台运行本程序"
|
||
except Exception as e:
|
||
logger.error(f"设置开机自启失败: {e}")
|
||
return f"❌ 设置失败: {e}"
|
||
|
||
|
||
def disable_autostart() -> str:
|
||
"""禁用 Windows 开机自启"""
|
||
if platform.system() != "Windows":
|
||
return "❌ 此功能仅支持 Windows 系统"
|
||
try:
|
||
import winreg
|
||
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, _STARTUP_REG_KEY, 0, winreg.KEY_SET_VALUE)
|
||
try:
|
||
winreg.DeleteValue(key, _APP_NAME)
|
||
except FileNotFoundError:
|
||
pass
|
||
winreg.CloseKey(key)
|
||
# 清理启动脚本
|
||
for f in [_get_startup_script_path(), _get_startup_bat_path()]:
|
||
if os.path.exists(f):
|
||
os.remove(f)
|
||
logger.info("开机自启已禁用")
|
||
return "✅ 开机自启已禁用"
|
||
except Exception as e:
|
||
logger.error(f"禁用开机自启失败: {e}")
|
||
return f"❌ 禁用失败: {e}"
|
||
|
||
|
||
def toggle_autostart(enabled: bool) -> str:
|
||
"""切换开机自启状态(供 UI 调用)"""
|
||
if enabled:
|
||
return enable_autostart()
|
||
else:
|
||
return disable_autostart()
|