添加阿里镜像,修复依赖增加删除python/Scripts,fast/slow->测试版/稳定版
This commit is contained in:
parent
9e6001d0e0
commit
003769b8e4
4 changed files with 72 additions and 12 deletions
119
launcher.py
Normal file
119
launcher.py
Normal file
|
@ -0,0 +1,119 @@
|
|||
import json
|
||||
import mimetypes
|
||||
from pathlib import Path
|
||||
from shutil import rmtree
|
||||
from subprocess import PIPE, STDOUT, Popen
|
||||
import webview
|
||||
|
||||
mimetypes.add_type("text/html", ".html")
|
||||
mimetypes.add_type("text/css", ".css")
|
||||
mimetypes.add_type("application/javascript", ".js")
|
||||
|
||||
version = "2024-11-28"
|
||||
|
||||
config = {
|
||||
"page": "init",
|
||||
"branch": "slow",
|
||||
"mirror": "tuna",
|
||||
}
|
||||
config_path = Path("launcher.json")
|
||||
|
||||
try:
|
||||
with config_path.open("r") as f:
|
||||
user_config = json.load(f)
|
||||
config.update(user_config)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def custom_event(data):
|
||||
data = json.dumps({"log": data})
|
||||
js = f"var event = new CustomEvent('log', {{detail: {data}}}); window.dispatchEvent(event);"
|
||||
window.evaluate_js(js)
|
||||
|
||||
|
||||
mirror_list = {
|
||||
"aliyun": "https://mirrors.aliyun.com/pypi/simple/",
|
||||
"pypi": "https://pypi.org/simple",
|
||||
"tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple",
|
||||
"sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple",
|
||||
}
|
||||
|
||||
|
||||
command_list = {
|
||||
"lfs": "git\\bin\\git lfs install",
|
||||
"ensurepip": "python\\python -m ensurepip --default-pip",
|
||||
"clone": "git\\bin\\git clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow",
|
||||
"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",
|
||||
"webview": "start ..\\python\\pythonw webview_ui.py",
|
||||
"manager": "start ..\\python\\pythonw manager.py",
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
|
||||
def get_mirror(self):
|
||||
return config["mirror"]
|
||||
|
||||
def set_mirror(self, mirror):
|
||||
config["mirror"] = mirror
|
||||
|
||||
def update_self(self):
|
||||
# 更新启动器本身
|
||||
pass
|
||||
|
||||
def rm_site_packages(self):
|
||||
site_packages_path = Path("./python/Lib/site-packages")
|
||||
if site_packages_path.exists():
|
||||
rmtree(site_packages_path)
|
||||
return "移除成功"
|
||||
return "python\\Lib\\site-packages目录不存在"
|
||||
|
||||
def rm_python_script(self):
|
||||
python_script_path = Path("./python/Scripts")
|
||||
if python_script_path.exists():
|
||||
rmtree(python_script_path)
|
||||
return "移除成功"
|
||||
return "python\\Scripts目录不存在"
|
||||
|
||||
def run(self, command, cwd=None):
|
||||
command = command_list[command]
|
||||
if callable(command):
|
||||
command = command()
|
||||
custom_event(command + "\n")
|
||||
try:
|
||||
with Popen(
|
||||
command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0
|
||||
) 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"
|
||||
|
||||
|
||||
window = webview.create_window("mower-ng launcher", "ui/dist/index.html", js_api=Api())
|
||||
webview.start()
|
||||
|
||||
with config_path.open("w") as f:
|
||||
json.dump(config, f)
|
Loading…
Add table
Add a link
Reference in a new issue