From 383bed2a9d92a919560d3163b6806c0fa6bb390d Mon Sep 17 00:00:00 2001 From: li-xiaochen <397721316@qq.com> Date: Sun, 8 Dec 2024 10:04:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BB=A3=E7=A0=81=E6=97=B6=E7=9A=84=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=82=E6=95=B0=E6=8F=90=E5=8D=87=E9=80=9F?= =?UTF-8?q?=E5=BA=A6=E3=80=81=E5=9B=9E=E6=98=BE=E6=9B=B4=E5=A4=9A=E8=BE=93?= =?UTF-8?q?=E5=87=BA=EF=BC=8C=E6=8B=86=E5=88=86stdout=E5=92=8Cstderr?= =?UTF-8?q?=E5=88=86=E5=88=AB=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- launcher.py | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/launcher.py b/launcher.py index 8ee5188..ed173e0 100644 --- a/launcher.py +++ b/launcher.py @@ -1,16 +1,19 @@ +import io import json import mimetypes +import os +import shutil +import subprocess +import threading from pathlib import Path from shutil import rmtree -from subprocess import CREATE_NO_WINDOW, PIPE, STDOUT, Popen +from subprocess import CREATE_NO_WINDOW, Popen import requests import webview from launcher import config from log import logger -import shutil -import os mimetypes.add_type("text/html", ".html") mimetypes.add_type("text/css", ".css") @@ -33,6 +36,7 @@ def custom_event(data): mirror_list = { "pypi": "https://pypi.org/simple", + "aliyun": "https://mirrors.aliyun.com/pypi/simple/", "tuna": "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple", "sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple", @@ -41,16 +45,27 @@ mirror_list = { 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.conf.branch}", - "switch": lambda: f"..\\git\\bin\\git switch -f {config.conf.branch}", - "reset": lambda: f"..\\git\\bin\\git reset --hard origin/{config.conf.branch}", + "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} --progress", + "switch": lambda: f"..\\git\\bin\\git -c lfs.concurrenttransfers=100 switch -f {config.conf.branch} --progress", + "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", "webview": "start ..\\python\\pythonw webview_ui.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: def load_config(self): @@ -121,19 +136,26 @@ class Api: if callable(command): command = command() custom_event(command + "\n") + logger.info(f"cwd {cwd}") + logger.info(f"command {command}") try: - with Popen( - command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0 + with subprocess.Popen( + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd, bufsize=0, + universal_newlines=False ) as p: - for data in p.stdout: - try: - text = data.decode("utf-8") - except Exception: - text = data.decode("gbk") - custom_event(text) + stdout_thread = threading.Thread(target=read_stream, args=(p.stdout, logger.info)) + stderr_thread = threading.Thread(target=read_stream, args=(p.stderr, logger.error)) + + stdout_thread.start() + stderr_thread.start() + + stdout_thread.join() + stderr_thread.join() if p.returncode == 0: return "success" except Exception as e: + logger.error(f"command {command} 执行异常{e}") + logger.exception(e) custom_event(str(e)) return "failed"