From 2929a9ed0c8b8d127a173f1cb355c6e97d12e556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B1=BC=E9=A6=99ROS?= <87068644+fishros@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:46:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=B7=9F=E8=B8=AA=E8=AE=B0=E5=BD=95=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install.py | 52 ++++++++++++++++++++++++++++++++++++++------- tools/base.py | 58 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/install.py b/install.py index 6bfd9e1..bf769f2 100644 --- a/install.py +++ b/install.py @@ -51,13 +51,13 @@ # 将工具信息添加到相应类型的列表中 tool_categories[tool_type][tool_id]=tool_info - +tracking = None def main(): # download base url_prefix = os.environ.get('FISHROS_URL','http://mirror.fishros.com/install') os.system("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(base_url,base_url.replace(url_prefix,''))) - from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask,ChooseWithCategoriesTask + from tools.base import CmdTask,FileUtils,PrintUtils,ChooseTask,ChooseWithCategoriesTask,Tracking from tools.base import encoding_utf8,osversion,osarch from tools.base import run_tool_file,download_tools from tools.base import config_helper,tr @@ -66,6 +66,10 @@ def main(): CmdTask("wget {} -O /tmp/fishinstall/{} --no-check-certificate".format(translator_url,translator_url.replace(url_prefix,''))).run() importlib.import_module("tools.translation.translator").Linguist() from tools.base import tr + import copy + + global tracing + tracing = copy.copy(Tracking) # 使用量统计 @@ -87,7 +91,7 @@ def main(): print('Solutions: https://fishros.org.cn/forum/topic/24 ') return False PrintUtils.print_success(tr.tr("基础检查通过...")) - + book = tr.tr(""" .-~~~~~~~~~-._ _.-~~~~~~~~~-. __.' ~. .~ `.__ @@ -110,8 +114,6 @@ def main(): """) PrintUtils.print_delay(tip,0.001) PrintUtils.print_delay(book,0.001) - - # download tools code,result = ChooseWithCategoriesTask(tool_categories, tips=tr.tr("---众多工具,等君来用---"),categories=tools_type_map).run() if code==0: PrintUtils().print_success(tr.tr("是觉得没有合胃口的菜吗?那快联系的小鱼增加菜单吧~")) @@ -121,8 +123,42 @@ def main(): config_helper.gen_config_file() PrintUtils.print_delay(tr.tr("欢迎加入机器人学习交流QQ群:438144612(入群口令:一键安装)"),0.1) - PrintUtils.print_success(tr.tr("鱼香小铺正式开业,最低499可入手一台能建图会导航的移动机器人,淘宝搜店:鱼香ROS 或打开链接查看:https://item.taobao.com/item.htm?id=696573635888"),0.001) - PrintUtils.print_success(tr.tr("如在使用过程中遇到问题,请打开:https://fishros.org.cn/forum 进行反馈"),0.001) + PrintUtils.print_delay(tr.tr("鱼香小铺正式开业,最低499可入手一台能建图会导航的移动机器人,淘宝搜店:鱼香ROS 或打开链接查看:https://item.taobao.com/item.htm?id=696573635888"),0.001) + PrintUtils.print_delay(tr.tr("如在使用过程中遇到问题,请打开:https://fishros.org.cn/forum 进行反馈"),0.001) if __name__=='__main__': - main() + run_exc = [] + + try: + main() + except Exception as e: + import traceback + print('\r\n检测到程序发生异常退出,请打开:https://fishros.org.cn/forum 携带如下内容进行反馈\n\n') + print("标题:使用一键安装过程中遇到程序崩溃") + print("```") + traceback.print_exc() + run_exc.append(traceback.format_exc()) + print("```") + print('本次运行详细日志文件已保存至 /tmp/fishros_install.log') + + try: + with open("/tmp/fishros_install.log", "w", encoding="utf-8") as f: + for exec in run_exc: + print(exec, file=f) # 打印异常输出到文件中 + for text,end in tracing.logs: + print(text, file=f,end=end) # 打印输出到文件中 + for text in tracing.err_logs: + print(text, file=f) # 打印输出到文件中 + if tracing.need_report: + print("") + input('检测到本次运行出现失败命令,直接退出按Ctrl+C,按任意键上传日志并退出\n') + ret = os.system("""curl -s -F "file=@/tmp/fishros_install.log" http://103.226.124.73:5000/upload > /tmp/fishros_upload 2>&1""") + if ret == 0: + with open("/tmp/fishros_upload","r") as f: + print("错误日志上传成功,反馈码:",f.read()) + else: + print("日志上传失败,若还需反馈请手动发帖!") + except: + pass + + \ No newline at end of file diff --git a/tools/base.py b/tools/base.py index 62bbe17..b5fd8c3 100644 --- a/tools/base.py +++ b/tools/base.py @@ -781,32 +781,60 @@ def get_codename(self): osversion = GetOsVersion() +class Tracking(): + """ + 日志跟踪模块 + """ + logs = [] + err_logs = [] + need_report = False + def put_log(values,end=""): + Tracking.logs.append((values,end)) + + def put_cmd_result(code,out,err,command): + if code!=0: + Tracking.need_report = True + Tracking.err_logs.append("Execute Command: {} Error Code{}".format(command,code)) + Tracking.err_logs.append('====================OUT====================') + for line in out: + Tracking.err_logs.append(line) + Tracking.err_logs.append('====================ERR====================') + for line in err: + Tracking.err_logs.append(line) + + class PrintUtils(): + @staticmethod def print_delay(data,delay=0.03,end="\n"): + PrintUtils.print_text("\033[37m",end="") for d in data: d = d.encode("utf-8").decode("utf-8") - print("\033[37m{}".format(d),end="",flush=True) + PrintUtils.print_text("{}".format(d),end="",flush=True) time.sleep(delay) - print(end=end) + PrintUtils.print_text(end=end) @staticmethod def print_error(data,end="\n"): - print("\033[31m{}\033[37m".format(data),end=end) + PrintUtils.print_text("\033[31m{}\033[37m".format(data),end=end) @staticmethod def print_info(data,end="\n"): - print("\033[37m{}".format(data)) + PrintUtils.print_text("\033[37m{}".format(data),end=end) @staticmethod def print_success(data,end="\n"): - print("\033[32m{}\033[37m".format(data)) + PrintUtils.print_text("\033[32m{}\033[37m".format(data),end=end) @staticmethod def print_warn(data,end="\n"): - print("\033[33m{}\033[37m".format(data)) - + PrintUtils.print_text("\033[33m{}\033[37m".format(data),end=end) + @staticmethod + def print_text(values: object="",end: str | None = "\n",flush= False): + print(values,end=end,flush=flush) + Tracking.put_log(values,end=end) + @staticmethod def print_fish(timeout=1,scale=30): return @@ -820,6 +848,9 @@ def print_fish(timeout=1,scale=30): time.sleep(timeout/scale) print("\n") + + + class Task(): """ - type: 任务类型 @@ -855,13 +886,13 @@ def __init__(self,timeout=10,scale=20) -> None: def update(self,log=""): if (self.i%4) == 0: - print('\r[/][{:.2f}s] {}'.format(self.dur,log),end="") + PrintUtils.print_text('\r[/][{:.2f}s] {}'.format(self.dur,log),end="") elif(self.i%4) == 1: - print('\r[\\][{:.2f}s] {}'.format(self.dur,log),end="") + PrintUtils.print_text('\r[\\][{:.2f}s] {}'.format(self.dur,log),end="") elif (self.i%4) == 2: - print('\r[|][{:.2f}s] {}'.format(self.dur,log),end="") + PrintUtils.print_text('\r[|][{:.2f}s] {}'.format(self.dur,log),end="") elif (self.i%4) == 3: - print('\r[-][{:.2f}s] {}'.format(self.dur,log),end="") + PrintUtils.print_text('\r[-][{:.2f}s] {}'.format(self.dur,log),end="") sys.stdout.flush() self.i += 1 # update time @@ -884,7 +915,7 @@ def update_time(self): def finsh(self,log="",color='\033[32m'): log = log+" "*(Progress.line_width-len(log)-15) - print('\r{}[-][{:.2f}s] {}'.format(color,self.dur, log), end="\r\n\r\n") + PrintUtils.print_text('\r{}[-][{:.2f}s] {}'.format(color,self.dur, log), end="\r\n\r\n") @@ -981,6 +1012,7 @@ def run_command(self,executable='/bin/sh'): self.bar.update_time() time.sleep(0.1) + Tracking.put_cmd_result(self.ret_code,self.ret_out,self.ret_err,self.command) return (self.ret_code,self.ret_out,self.ret_err) def is_command_finish(self): @@ -1034,7 +1066,7 @@ def __choose(data,tips,array): while True: if choose_item: choose = str(choose_item['choose']) - print(tr.tr("为您从配置文件找到默认选项:"),choose_item) + PrintUtils.print_text(tr.tr("为您从配置文件找到默认选项:"),choose_item) else: choose = input(tr.tr("请输入[]内的数字以选择:")) choose_item = None