初始化和更新代码时的命令添加参数提升速度、回显更多输出,拆分stdout和stderr分别输出

This commit is contained in:
li-xiaochen 2024-12-08 10:04:49 +08:00
parent 23f551fe18
commit 383bed2a9d

View file

@ -1,16 +1,19 @@
import io
import json import json
import mimetypes import mimetypes
import os
import shutil
import subprocess
import threading
from pathlib import Path from pathlib import Path
from shutil import rmtree from shutil import rmtree
from subprocess import CREATE_NO_WINDOW, PIPE, STDOUT, Popen from subprocess import CREATE_NO_WINDOW, Popen
import requests import requests
import webview import webview
from launcher import config from launcher import config
from log import logger from log import logger
import shutil
import os
mimetypes.add_type("text/html", ".html") mimetypes.add_type("text/html", ".html")
mimetypes.add_type("text/css", ".css") mimetypes.add_type("text/css", ".css")
@ -33,6 +36,7 @@ def custom_event(data):
mirror_list = { mirror_list = {
"pypi": "https://pypi.org/simple", "pypi": "https://pypi.org/simple",
"aliyun": "https://mirrors.aliyun.com/pypi/simple/", "aliyun": "https://mirrors.aliyun.com/pypi/simple/",
"tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple", "tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple",
"sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple", "sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple",
@ -41,16 +45,27 @@ mirror_list = {
command_list = { command_list = {
"lfs": "git\\bin\\git lfs install", "lfs": "git\\bin\\git lfs install",
"ensurepip": "python\\python -m ensurepip --default-pip", "ensurepip": "python\\python -m ensurepip --default-pip",
"clone": "git\\bin\\git clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow", "clone": "git\\bin\\git -c lfs.concurrenttransfers=100 clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow",
"fetch": lambda: f"..\\git\\bin\\git fetch origin {config.conf.branch}", "fetch": lambda: f"..\\git\\bin\\git fetch origin {config.conf.branch} --progress",
"switch": lambda: f"..\\git\\bin\\git switch -f {config.conf.branch}", "switch": lambda: f"..\\git\\bin\\git -c lfs.concurrenttransfers=100 switch -f {config.conf.branch} --progress",
"reset": lambda: f"..\\git\\bin\\git reset --hard origin/{config.conf.branch}", "reset": lambda: f"..\\git\\bin\\git -c lfs.concurrenttransfers=200 reset --hard origin/{config.conf.branch}",
"pip_install": lambda: f"..\\python\\Scripts\\pip install --no-cache-dir -i {mirror_list[config.conf.mirror]} -r requirements.txt --no-warn-script-location", "pip_install": lambda: f"..\\python\\Scripts\\pip install --no-cache-dir -i {mirror_list[config.conf.mirror]} -r requirements.txt --no-warn-script-location",
"webview": "start ..\\python\\pythonw webview_ui.py", "webview": "start ..\\python\\pythonw webview_ui.py",
"manager": "start ..\\python\\pythonw manager.py", "manager": "start ..\\python\\pythonw manager.py",
} }
def read_stream(stream, log_func):
text_io = io.TextIOWrapper(stream, encoding='utf-8', errors='replace')
try:
for line in iter(text_io.readline, ''):
text = line.rstrip('\n')
log_func(f"log: {text}")
custom_event(text.strip() + "\n")
finally:
text_io.close()
class Api: class Api:
def load_config(self): def load_config(self):
@ -121,19 +136,26 @@ class Api:
if callable(command): if callable(command):
command = command() command = command()
custom_event(command + "\n") custom_event(command + "\n")
logger.info(f"cwd {cwd}")
logger.info(f"command {command}")
try: try:
with Popen( with subprocess.Popen(
command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd, bufsize=0,
universal_newlines=False
) as p: ) as p:
for data in p.stdout: stdout_thread = threading.Thread(target=read_stream, args=(p.stdout, logger.info))
try: stderr_thread = threading.Thread(target=read_stream, args=(p.stderr, logger.error))
text = data.decode("utf-8")
except Exception: stdout_thread.start()
text = data.decode("gbk") stderr_thread.start()
custom_event(text)
stdout_thread.join()
stderr_thread.join()
if p.returncode == 0: if p.returncode == 0:
return "success" return "success"
except Exception as e: except Exception as e:
logger.error(f"command {command} 执行异常{e}")
logger.exception(e)
custom_event(str(e)) custom_event(str(e))
return "failed" return "failed"