Musicreater/msctspt/bugReporter.py

137 lines
4.8 KiB
Python
Raw Normal View History

2021-11-21 00:59:15 +08:00
# -*- coding: UTF-8 -*-
2021-11-21 11:38:01 +08:00
'''提供错误报告的基本操作及方法 顺便提供版本更新、安装库等功能'''
2021-11-21 00:59:15 +08:00
def makeZip(sourceDir, outFilename,compression = 8,exceptFile = None):
'''使用compression指定的算法打包目录为zip文件\n
默认算法为DEFLATED(8),可用算法如下\n
STORED = 0\n
DEFLATED = 8\n
BZIP2 = 12\n
LZMA = 14\n
'''
import os, zipfile
zipf = zipfile.ZipFile(outFilename, 'w',compression)
pre_len = len(os.path.dirname(sourceDir))
for parent, dirnames, filenames in os.walk(sourceDir):
for filename in filenames:
if filename == exceptFile:
2022-01-15 00:40:17 +08:00
continue
2021-11-21 00:59:15 +08:00
print(filename)
pathfile = os.path.join(parent, filename)
arcname = pathfile[pre_len:].strip(os.path.sep) #相对路径
zipf.write(pathfile, arcname)
zipf.close()
del zipf,pre_len
#以上函数节选并修改自 正在攀登的小蜗牛 的博客https://blog.csdn.net/qq_21127151/article/details/107503942
2021-11-24 12:52:08 +08:00
2021-11-21 00:59:15 +08:00
class report():
2021-11-24 12:52:08 +08:00
'''发送报告以及相应的任务处理'''
2021-11-21 00:59:15 +08:00
def __init__(self,senderName:str = 'Unknown',senderContact:str = 'None',describetion:str = ''):
2021-11-24 12:52:08 +08:00
''':param senderName 发送者名称
:param senderContact 发送者联系方式
:param describetion 问题描述'''
2022-01-15 00:40:17 +08:00
self.senderName = senderName
self.senderContact = senderContact
self.describetion = describetion
2021-11-21 00:59:15 +08:00
if not self.senderName :
2022-01-15 00:40:17 +08:00
self.senderName = 'Unknown'
2021-11-21 00:59:15 +08:00
if not self.senderContact :
2022-01-15 00:40:17 +08:00
self.senderContact = 'None'
2021-11-21 00:59:15 +08:00
2021-11-24 12:52:08 +08:00
def emailReport(self):
'''使用E-mail方法发送当前的日志和临时文件等'''
import smtplib
2022-01-15 00:40:17 +08:00
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
2021-11-24 12:52:08 +08:00
from nmcsup.log import log
log("发送错误报告")
2022-01-15 00:40:17 +08:00
import os
2021-11-24 12:52:08 +08:00
log("添加标题与正文")
2022-01-15 00:40:17 +08:00
msg = MIMEMultipart()
2021-11-24 12:52:08 +08:00
#发送者与接收者显示名称
2022-01-15 00:40:17 +08:00
msg["From"] = Header(self.senderName,'utf-8')
msg["To"] = Header("W-YI (QQ2647547478)",'utf-8')
2021-11-24 12:52:08 +08:00
#标题
2022-01-15 00:40:17 +08:00
msg["Subject"] = '音·创 - 来自 '+self.senderName+' 的错误报告'
2021-11-24 12:52:08 +08:00
#正文
2022-01-15 00:40:17 +08:00
msg.attach(MIMEText("来自"+self.senderName+"( "+self.senderContact+" )的错误描述:\n"+self.describetion,'plain','utf-8'))
2021-11-24 12:52:08 +08:00
log("添加完毕,正在生成压缩包...")
2022-01-15 00:40:17 +08:00
makeZip("./","Temps&Logs.zip",exceptFile="Temps&Logs.zip")
attafile=MIMEText(open("Temps&Logs.zip",'rb').read(),"base64",'gb2312')
attafile["Content-Type"] = 'application/octet-stream'
attafile["Content-Disposition"] = 'attachmentfilename="BugReport_from_'+self.senderName+'.zip"'
msg.attach(attafile)
2021-11-24 12:52:08 +08:00
log("完毕,准备发送")
try:
smtp = smtplib.SMTP()
2022-01-15 00:40:17 +08:00
smtp.connect("smtp.163.com")
#smtp.login("RyounDevTeam@163.com","RyounDaiYi99")
2021-11-24 12:52:08 +08:00
#SIQQKQQYCZRVIDFJ是授权密码
2022-01-15 00:40:17 +08:00
smtp.login("RyounDevTeam@163.com","SIQQKQQYCZRVIDFJ")
2021-11-24 12:52:08 +08:00
smtp.sendmail("RyounDevTeam@163.com",["RyounDevTeam@163.com",],msg.as_string())
log("错误汇报邮件已发送")
except smtplib.SMTPException as e:
2022-01-15 00:40:17 +08:00
log("错误汇报邮件发送失败:\n"+str(e))
2021-11-24 12:52:08 +08:00
log("清空内存和临时文件")
del msg,attafile
os.remove("./Temps&Logs.zip")
2021-11-21 00:59:15 +08:00
2021-11-21 11:38:01 +08:00
class version:
libraries = ('mido','amulet','amulet-core','amulet-nbt','piano_transcription_inference','pypinyin','pyinstaller','py7zr','websockets','torch')
2021-11-21 11:38:01 +08:00
'''当前所需库,有一些是开发用的,用户不需要安装'''
2022-01-15 00:40:17 +08:00
version = ('0.0.1','Delta',)
2021-11-21 11:38:01 +08:00
'''当前版本'''
def __init__(self) -> None:
self.libraries = version.libraries
'''当前所需库,有一些是开发用的,用户不需要安装'''
self.version = version.version
'''当前版本'''
def installLibraries(self):
'''安装全部开发用库'''
from sys import platform
import os
if platform == 'win32':
import shutil
2021-11-21 12:45:20 +08:00
try:
shutil.rmtree(os.getenv('APPDATA')+'\\Musicreater\\')
except:
2022-01-15 00:40:17 +08:00
pass
2021-11-21 14:11:28 +08:00
for i in self.libraries:
print("安装库:"+i)
os.system("python -m pip install "+i+" -i https://pypi.tuna.tsinghua.edu.cn/simple")
2021-12-19 02:00:30 +08:00
elif platform == 'linux':
2021-11-21 14:11:28 +08:00
os.system("sudo apt-get install python3-pip")
2022-01-15 00:40:17 +08:00
os.system("sudo apt-get install python3-tk")
2021-12-19 02:00:30 +08:00
os.system("sudo apt-get install python3-tkinter")
2021-11-21 14:11:28 +08:00
for i in self.libraries:
print("安装库:"+i)
os.system("sudo python3 -m pip install "+i+" -i https://pypi.tuna.tsinghua.edu.cn/simple")