launcher/main.py

92 lines
2.4 KiB
Python
Raw Normal View History

2024-09-23 16:12:34 +08:00
import json
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen
import webview
version = "2024-10-10"
2024-10-10 09:48:57 +08:00
config = {
"page": "init",
"branch": "slow",
"mirror": "tuna",
}
2024-09-23 16:12:34 +08:00
config_path = Path("launcher.json")
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-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-10-10 09:48:57 +08:00
mirror_list = {
"pypi": "https://pypi.org/simple",
"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-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-09-23 16:12:34 +08:00
with Popen(
2024-09-23 19:42:56 +08:00
command,
2024-09-23 16:12:34 +08:00
stdout=PIPE,
stderr=STDOUT,
shell=True,
bufsize=1,
text=True,
cwd=cwd,
2024-09-23 20:30:32 +08:00
encoding="utf-8",
2024-09-23 16:12:34 +08:00
) as p:
for line in p.stdout:
2024-09-23 19:42:56 +08:00
custom_event(line)
2024-09-23 16:12:34 +08:00
window = webview.create_window(f"mower-ng launcher {version}", "ui/dist/index.html", 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)