2024-09-23 16:12:34 +08:00
|
|
|
import json
|
2024-11-03 19:03:22 +08:00
|
|
|
import mimetypes
|
2024-11-30 20:54:48 +08:00
|
|
|
import sys
|
2024-09-23 16:12:34 +08:00
|
|
|
from pathlib import Path
|
2024-10-10 10:15:20 +08:00
|
|
|
from shutil import rmtree
|
2024-09-23 16:12:34 +08:00
|
|
|
from subprocess import PIPE, STDOUT, Popen
|
2024-11-30 20:54:48 +08:00
|
|
|
|
|
|
|
import requests
|
2024-09-23 16:12:34 +08:00
|
|
|
import webview
|
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
from log import logger
|
|
|
|
from python.Lib import shutil, os, subprocess
|
|
|
|
|
2024-11-03 19:03:22 +08:00
|
|
|
mimetypes.add_type("text/html", ".html")
|
|
|
|
mimetypes.add_type("text/css", ".css")
|
|
|
|
mimetypes.add_type("application/javascript", ".js")
|
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
version = "V0.2"
|
2024-10-10 09:46:24 +08:00
|
|
|
|
2024-10-10 09:48:57 +08:00
|
|
|
config = {
|
|
|
|
"page": "init",
|
|
|
|
"branch": "slow",
|
2024-11-28 15:42:28 +08:00
|
|
|
"mirror": "aliyun",
|
2024-10-10 09:48:57 +08:00
|
|
|
}
|
2024-09-23 16:12:34 +08:00
|
|
|
config_path = Path("launcher.json")
|
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
get_new_version_url = "https://git.zhaozuohong.vip/api/v1/repos/mower-ng/launcher/releases/latest"
|
|
|
|
|
2024-12-01 17:54:09 +08:00
|
|
|
upgrade_script_name = "upgrade.bat"
|
|
|
|
|
2024-09-23 16:12:34 +08:00
|
|
|
try:
|
|
|
|
with config_path.open("r") as f:
|
2024-10-10 09:48:57 +08:00
|
|
|
user_config = json.load(f)
|
|
|
|
config.update(user_config)
|
2024-09-23 16:12:34 +08:00
|
|
|
except Exception:
|
2024-10-10 09:48:57 +08:00
|
|
|
pass
|
2024-09-23 16:12:34 +08:00
|
|
|
|
2024-10-10 10:15:20 +08:00
|
|
|
|
2024-09-23 19:42:56 +08:00
|
|
|
def custom_event(data):
|
|
|
|
data = json.dumps({"log": data})
|
2024-09-23 16:12:34 +08:00
|
|
|
js = f"var event = new CustomEvent('log', {{detail: {data}}}); window.dispatchEvent(event);"
|
|
|
|
window.evaluate_js(js)
|
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
|
2024-12-01 17:54:09 +08:00
|
|
|
def replace_restart(exename, new_exename):
|
|
|
|
script_path = os.path.join(os.getcwd(), upgrade_script_name)
|
2024-11-30 20:54:48 +08:00
|
|
|
with open(script_path, 'w') as b:
|
2024-12-01 17:54:09 +08:00
|
|
|
TempList = f"@echo off\n"
|
|
|
|
TempList += f"timeout /t 3 /nobreak\n" # 等待进程退出
|
|
|
|
TempList += f"del {exename}\n" # 删除旧程序
|
|
|
|
TempList += f"move {new_exename} {exename}\n" # 复制新版本程序
|
|
|
|
TempList += f"timeout /t 1 /nobreak\n" # 等待复制完成
|
|
|
|
TempList += f"start {exename}\n" # 启动新程序
|
|
|
|
TempList += f"exit"
|
2024-11-30 20:54:48 +08:00
|
|
|
b.write(TempList)
|
2024-12-01 17:54:09 +08:00
|
|
|
# 不显示cmd窗口
|
|
|
|
subprocess.Popen([script_path], creationflags=subprocess.CREATE_NO_WINDOW)
|
2024-11-30 20:54:48 +08:00
|
|
|
os._exit(0)
|
2024-09-23 16:12:34 +08:00
|
|
|
|
2024-12-01 17:54:09 +08:00
|
|
|
|
2024-10-10 09:48:57 +08:00
|
|
|
mirror_list = {
|
|
|
|
"pypi": "https://pypi.org/simple",
|
2024-11-28 15:42:28 +08:00
|
|
|
"aliyun": "https://mirrors.aliyun.com/pypi/simple/",
|
2024-10-10 09:48:57 +08:00
|
|
|
"tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple",
|
|
|
|
"sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple",
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:14:57 +08:00
|
|
|
command_list = {
|
2024-09-23 19:42:56 +08:00
|
|
|
"lfs": "git\\bin\\git lfs install",
|
2024-09-23 20:03:52 +08:00
|
|
|
"ensurepip": "python\\python -m ensurepip --default-pip",
|
2024-09-23 19:14:57 +08:00
|
|
|
"clone": "git\\bin\\git clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow",
|
2024-10-10 09:48:57 +08:00
|
|
|
"fetch": lambda: f"..\\git\\bin\\git fetch origin {config['branch']}",
|
|
|
|
"switch": lambda: f"..\\git\\bin\\git switch -f {config['branch']}",
|
|
|
|
"reset": lambda: f"..\\git\\bin\\git reset --hard origin/{config['branch']}",
|
|
|
|
"pip_install": lambda: f"..\\python\\Scripts\\pip install --no-cache-dir -i {mirror_list[config['mirror']]} -r requirements.txt --no-warn-script-location",
|
2024-09-23 19:14:57 +08:00
|
|
|
"webview": "start ..\\python\\pythonw webview_ui.py",
|
|
|
|
"manager": "start ..\\python\\pythonw manager.py",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 16:12:34 +08:00
|
|
|
class Api:
|
|
|
|
def get_branch(self):
|
|
|
|
return config["branch"]
|
|
|
|
|
|
|
|
def set_branch(self, branch):
|
|
|
|
config["branch"] = branch
|
|
|
|
|
|
|
|
def get_page(self):
|
|
|
|
return config["page"]
|
|
|
|
|
|
|
|
def set_page(self, page):
|
|
|
|
config["page"] = page
|
|
|
|
|
2024-10-10 09:48:57 +08:00
|
|
|
def get_mirror(self):
|
|
|
|
return config["mirror"]
|
|
|
|
|
|
|
|
def set_mirror(self, mirror):
|
|
|
|
config["mirror"] = mirror
|
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
def get_version(self):
|
|
|
|
return version
|
|
|
|
|
|
|
|
def get_new_version(self):
|
|
|
|
logger.info("获取最新版本号")
|
|
|
|
response = requests.get(get_new_version_url)
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
# 更新启动器本身
|
2024-12-01 17:54:09 +08:00
|
|
|
def update_self(self, download_url):
|
2024-11-30 20:54:48 +08:00
|
|
|
# 获取当前启动器的路径
|
|
|
|
current_path = sys.argv[0]
|
|
|
|
# 如果current_path是以py结尾,则将其转换为.exe
|
|
|
|
if current_path.endswith(".py"):
|
|
|
|
current_path = current_path[:-3] + ".exe"
|
|
|
|
temp_path = current_path + '.tmp'
|
|
|
|
|
|
|
|
logger.info(f"下载新版本: {download_url}")
|
|
|
|
response = requests.get(download_url, stream=True)
|
|
|
|
if response.status_code == 200:
|
|
|
|
with open(temp_path, 'wb') as file:
|
|
|
|
shutil.copyfileobj(response.raw, file)
|
|
|
|
else:
|
|
|
|
logger.error(f"下载新版本失败: {response.status_code}")
|
|
|
|
return f"下载新版本: {response.status_code}"
|
|
|
|
|
|
|
|
logger.info(f"替换旧版本,{temp_path} -> {current_path}")
|
2024-12-01 17:54:09 +08:00
|
|
|
replace_restart(current_path, temp_path)
|
2024-11-28 15:34:49 +08:00
|
|
|
|
2024-10-10 10:15:20 +08:00
|
|
|
def rm_site_packages(self):
|
|
|
|
site_packages_path = Path("./python/Lib/site-packages")
|
|
|
|
if site_packages_path.exists():
|
|
|
|
rmtree(site_packages_path)
|
2024-11-28 20:46:18 +08:00
|
|
|
return "site-packages目录移除成功"
|
2024-10-10 10:15:20 +08:00
|
|
|
return "python\\Lib\\site-packages目录不存在"
|
|
|
|
|
2024-11-28 20:46:18 +08:00
|
|
|
def rm_python_scripts(self):
|
|
|
|
python_scripts_path = Path("./python/Scripts")
|
|
|
|
if python_scripts_path.exists():
|
|
|
|
rmtree(python_scripts_path)
|
|
|
|
return "Scripts目录移除成功"
|
2024-11-28 15:34:49 +08:00
|
|
|
return "python\\Scripts目录不存在"
|
|
|
|
|
2024-09-23 16:12:34 +08:00
|
|
|
def run(self, command, cwd=None):
|
2024-09-23 19:42:56 +08:00
|
|
|
command = command_list[command]
|
2024-10-10 09:48:57 +08:00
|
|
|
if callable(command):
|
|
|
|
command = command()
|
2024-09-23 19:42:56 +08:00
|
|
|
custom_event(command + "\n")
|
2024-10-10 10:48:01 +08:00
|
|
|
try:
|
|
|
|
with Popen(
|
2024-12-01 17:54:09 +08:00
|
|
|
command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0
|
2024-10-10 10:48:01 +08:00
|
|
|
) as p:
|
|
|
|
for data in p.stdout:
|
|
|
|
try:
|
|
|
|
text = data.decode("utf-8")
|
|
|
|
except Exception:
|
|
|
|
text = data.decode("gbk")
|
|
|
|
custom_event(text)
|
|
|
|
if p.returncode == 0:
|
|
|
|
return "success"
|
|
|
|
except Exception as e:
|
|
|
|
custom_event(str(e))
|
|
|
|
return "failed"
|
2024-09-23 16:12:34 +08:00
|
|
|
|
2024-12-01 17:54:09 +08:00
|
|
|
|
2024-11-30 20:54:48 +08:00
|
|
|
# 如果当前路径存在更新脚本,则删除
|
2024-12-01 17:54:09 +08:00
|
|
|
if Path(upgrade_script_name).exists():
|
|
|
|
os.remove(upgrade_script_name)
|
2024-09-23 16:12:34 +08:00
|
|
|
|
2024-11-28 22:00:49 +08:00
|
|
|
window = webview.create_window(f"mower-ng launcher {version}", "ui/dist/index.html", js_api=Api())
|
2024-12-01 17:54:09 +08:00
|
|
|
# window = webview.create_window(f"mower-ng launcher {version}", "http://localhost:5173/", js_api=Api())
|
2024-09-23 20:35:05 +08:00
|
|
|
webview.start()
|
2024-09-23 16:12:34 +08:00
|
|
|
|
|
|
|
with config_path.open("w") as f:
|
|
|
|
json.dump(config, f)
|