From 949fd1a07ff52b772a99b623450181b740a6323d Mon Sep 17 00:00:00 2001 From: gfdgd_xi <3025613752@qq.com> Date: Fri, 22 Dec 2023 21:55:38 +0800 Subject: [PATCH] Develop log report saver preliminary --- DefaultSetting.py | 191 +++++++++++++++++++++++++++- Resources/LogTemplate/icon.svg | 169 ++++++++++++++++++++++++ Resources/LogTemplate/template.html | 146 +++++++++++++++++++++ 3 files changed, 502 insertions(+), 4 deletions(-) create mode 100644 Resources/LogTemplate/icon.svg create mode 100644 Resources/LogTemplate/template.html diff --git a/DefaultSetting.py b/DefaultSetting.py index 93331b6..4ee759d 100644 --- a/DefaultSetting.py +++ b/DefaultSetting.py @@ -2,6 +2,12 @@ # 读取设置单独用一个 py 文件 import os import json +import base64 +import shutil +import getpass +import datetime +import traceback +import subprocess import PyQt5.QtGui as QtGui # 获取用户主目录 @@ -36,6 +42,13 @@ defultProgramList = { "FontSize": 1 } +programPath = os.path.split(os.path.realpath(__file__))[0] # 返回 string +try: + setting = json.loads(readtxt(get_home() + "/.config/deepin-wine-runner/WineSetting.json")) + information = json.loads(readtxt(f"{programPath}/information.json")) +except: + setting = defultProgramList + def SetFont(app): defaultFont = app.font() size = setting["FontSize"] @@ -47,7 +60,177 @@ def SetFont(app): font.setPointSize(int(defaultFont.pointSize() / size)) app.setFont(font) -try: - setting = json.loads(readtxt(get_home() + "/.config/deepin-wine-runner/WineSetting.json")) -except: - setting = defultProgramList \ No newline at end of file +def getFileFolderSize(fileOrFolderPath): + """get size for file or folder""" + totalSize = 0 + try: + if not os.path.exists(fileOrFolderPath): + return totalSize + if os.path.isfile(fileOrFolderPath): + totalSize = os.path.getsize(fileOrFolderPath) # 5041481 + return totalSize + if os.path.islink(fileOrFolderPath): + return 0 + if os.path.isdir(fileOrFolderPath): + with os.scandir(fileOrFolderPath) as dirEntryList: + for curSubEntry in dirEntryList: + curSubEntryFullPath = os.path.join(fileOrFolderPath, curSubEntry.name) + if curSubEntry.is_dir(): + curSubFolderSize = getFileFolderSize(curSubEntryFullPath) # 5800007 + totalSize += curSubFolderSize + elif curSubEntry.is_file(): + curSubFileSize = os.path.getsize(curSubEntryFullPath) # 1891 + totalSize += curSubFileSize + return totalSize + except: + return totalSize + +def FileToBase64(filePath): + src = "" + with open(filePath, "rb") as f: + base64Byte = base64.b64encode(f.read()) + src += base64Byte.decode("utf-8") + return src + +class SaveLog(): + userName = getpass.getuser() + time = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S") + kernelVersion = subprocess.getoutput("uname -a") + wineRunnerVersion = information["Version"] + architecture = subprocess.getoutput("arch") + cpuInfo = subprocess.getoutput("lscpu") + lsmod = subprocess.getoutput("lsmod") + lshw = subprocess.getoutput("lshw") + cpu = subprocess.getoutput("cat /proc/cpuinfo | grep 'model name' | head -n 1 | awk -F: '{print $2}'") + gpu = subprocess.getoutput("lspci | grep -i 'VGA\|3D\|2D'") + + + def __init__(self, chooseWineName, chooseWineCommand, runCommand, binPath, logOut, description, imgPath=[]) -> None: + self.chooseWineName = chooseWineName + self.chooseWineCommand = chooseWineCommand + self.runCommand = runCommand + self.binPath = binPath + self.logOut = logOut + self.description = description + self.imgPath = imgPath + # 读取可执行文件信息 + if os.path.exists(binPath): + try: + self.binSize = f"{str(int(getFileFolderSize(binPath) / 1024 / 1024))}MB" + except: + self.binSize = "Error" + traceback.print_exc() + os.system(f"'{programPath}/wrestool' '{binPath}' -x -t 14 > '/tmp/wine-runner-log-icon.png'") + # 如果提取成功 + if os.path.exists("/tmp/wine-runner-log-icon.png"): + # 转换成 base64 编码 + self.binIcon = "data:image/jpg;base64," + FileToBase64("/tmp/wine-runner-log-icon.png") + self.binIconPath = "/tmp/wine-runner-log-icon.png" + else: + self.binIcon = "Not Found" + self.binIconPath = "Not Found" + else: + self.binSize = "Not Found" + self.binIcon = "Not Found" + self.binIconPath = "Not Found" + try: + self.memoryInfo = readtxt("/proc/meminfo") + except: + traceback.print_exc() + self.memoryInfo = traceback.format_exc() + + def To7z(self, savePath): + os.system("rm -rfv /tmp/wine-runner-log") + os.system("mkdir -v /tmp/wine-runner-log") + self.ToHtml("/tmp/wine-runner-log/index.html", toZip=True) + shutil.copy(self.binIconPath, f"/tmp/wine-runner-log/{os.path.basename(self.binIconPath)}") + lists = ["wine-runner-log-icon.png", "index.html"] + for i in self.imgPath: + name = os.path.basename(i) + if os.path.basename(i) in lists: + while name in lists: + name = os.path.splitext(name)[0] + "-copy" + os.path.splitext(name)[1] + lists.append(name) + else: + lists.append(name) + shutil.copy(i, f"/tmp/wine-runner-log/{name}") + os.system(f"7z a '{savePath}' /tmp/wine-runner-log") + + + def ToHtml(self, savePath, toZip=False): + print(self.userName, self.time) + # 对文本进行处理 + description = "" + logOut = "" + cpuInfo = "" + memoryInfo = "" + imgPath = "" + lsmod = "" + lshw = "" + for i in self.description.splitlines(): + description += f'{i}\n' + for i in self.logOut.splitlines(): + logOut += f'{i}\n' + for i in self.cpuInfo.splitlines(): + cpuInfo += f'{i}\n' + for i in self.memoryInfo.splitlines(): + memoryInfo += f'{i}\n' + for i in self.lsmod.splitlines(): + lsmod += f'{i}\n' + for i in self.lshw.splitlines(): + lshw += f'{i}\n' + text = readtxt(f"{programPath}/Resources/LogTemplate/template.html") + if toZip: + binIcon = os.path.basename(self.binIconPath) + # 重名排除 + lists = ["wine-runner-log-icon.png", "index.html"] + for i in self.imgPath: + name = os.path.basename(i) + if os.path.basename(i) in lists: + while name in lists: + name = os.path.splitext(name)[0] + "-copy" + os.path.splitext(name)[1] + lists.append(name) + else: + lists.append(name) + imgPath += f'

\n' + else: + binIcon = self.binIcon + for i in self.imgPath: + try: + path = "data:image/jpg;base64," + FileToBase64(i) + except: + traceback.print_exc() + path = "Error" + imgPath += f'

\n' + + replaceMap = { + "%UserName%": self.userName, + "%Time%": self.time, + "%KernelVersion": self.kernelVersion, + "%ChooseWineName%": self.chooseWineName, + "%ChooseWineCommand%": self.chooseWineCommand, + "%RunCommand%": self.runCommand, + "%BinPath%": self.binPath, + "%WineRunnerVersion%": self.wineRunnerVersion, + "%BinSize%": self.binSize, + "%BinIcon%": binIcon, + "%CPUInfo%": cpuInfo, + "%Architecture%": self.architecture, + "%MemoryInfo%": memoryInfo, + "%LogOut%": logOut, + "%Description%": description, + "%ImgPath%": imgPath, + "%Lsmod%": lsmod, + "%Lshw%": lshw, + "%CPU%": self.cpu, + "%GPU%": self.gpu + } + for i in replaceMap.keys(): + text = text.replace(i, replaceMap[i]) + with open(savePath, "w") as file: + file.write(text) + + + + +SaveLog("a", "b", "c", "/opt/apps/deepin-wine-runner/geek.exe", "e", "f", ["/tmp/wine-runner-log/wine-runner-log-icon.png"]).To7z("/tmp/a.7z") \ No newline at end of file diff --git a/Resources/LogTemplate/icon.svg b/Resources/LogTemplate/icon.svg new file mode 100644 index 0000000..773c7c0 --- /dev/null +++ b/Resources/LogTemplate/icon.svg @@ -0,0 +1,169 @@ + + + +Wine 运行器 diff --git a/Resources/LogTemplate/template.html b/Resources/LogTemplate/template.html new file mode 100644 index 0000000..ff7a0d9 --- /dev/null +++ b/Resources/LogTemplate/template.html @@ -0,0 +1,146 @@ + + %UserName%于%Time%的日志 + + + + + + + +

+ + +

+
+

%UserName%于%Time%的日志

+

信息

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
栏目
系统版本%SystemVersion%
CPU%CPU%
GPU%GPU%
内核版本%KernelVersion%
系统架构%Architecture%
Wine 运行器版本%WineRunnerVersion%
Wine 名称%ChooseWineName%
Wine 路径%ChooseWineCommand%
运行命令%RunCommand%
可执行文件路径%BinPath%
可执行文件大小%BinSize%
可执行文件图标
+

描述

+%ImgPath% +
+%Description%
+
+

Wine 输出

+
+%LogOut%
+
+

详细信息

+

CPU

+
+%CPUInfo%
+
+

内存

+
+%MemoryInfo%
+
+

内核模块

+
+%Lsmod%
+
+

PCI

+
+%Lshw%
+
+
+

©2020~Now gfdgd xi

+ + + \ No newline at end of file