mirror.Manyana/setUp.py

365 lines
15 KiB
Python
Raw Normal View History

2024-06-15 23:23:12 +08:00
# -*- coding:utf-8 -*-
2024-02-27 21:33:36 +08:00
import logging
2023-07-22 08:39:49 +08:00
import os
2024-02-27 21:33:36 +08:00
import subprocess
2024-07-15 17:36:22 +08:00
import sys
2023-08-02 20:31:56 +08:00
2024-07-22 21:27:42 +08:00
import colorlog
import shutil
2024-06-26 01:00:02 +08:00
current_dir = os.getcwd()
# 获取上一级目录
parent_dir = os.path.dirname(current_dir)
# 检测parent_dir目录下是否有NTQQ_Launcher文件夹
NTQQ_Launcher_path = os.path.join(parent_dir, "NTQQ_Launcher")
if os.path.exists(NTQQ_Launcher_path):
# 如果有NTQQ_Launcher文件夹则设置路径为parent_dir下的指定目录
git_path = os.path.join(parent_dir, "git","bin", "git.exe")
custom_python_path = os.path.join(parent_dir, "python", "python.exe")
python_path = custom_python_path
custom_pip_path = os.path.join(parent_dir, "python", "Scripts", "pip.exe")
pip_path = custom_pip_path
2024-07-24 14:38:00 +08:00
os.system(f"\"{python_path}\" -m pip install --upgrade pip")
#print("{pip_path} -m {custom_pip_path} install --upgrade pip")
# os.system(f"\"{python_path}\" -m pip install --upgrade pip")
print("检测到NTQQ_Launcher文件夹使用对应文件夹下的git、python、pip")
else:
2024-07-22 21:27:42 +08:00
# 检测上一级目录下的environments/MinGit/cmd/git.exe是否存在
custom_git_path = os.path.join(parent_dir, "environments", "MinGit", "cmd", "git.exe")
if os.path.exists(custom_git_path):
git_path = custom_git_path
else:
git_path = "git"
2024-07-03 21:37:49 +08:00
2024-07-22 21:27:42 +08:00
custom_python_path = os.path.join(parent_dir, "environments", "Python39", "python.exe")
if os.path.exists(custom_python_path):
python_path = custom_python_path
else:
python_path = "python"
custom_pip_path = os.path.join(parent_dir, "environments", "Python39", "Scripts", "pip.exe")
if os.path.exists(custom_pip_path):
pip_path = custom_pip_path
else:
pip_path = "pip"
#print("未检测到NTQQ_Launcher文件夹使用系统环境变量中的git、python、pip")
#print("git路径", git_path)
#print("python路径", python_path)
#print("pip路径", pip_path)
2024-07-24 14:38:00 +08:00
2024-02-27 21:33:36 +08:00
def newLogger():
# 创建一个logger对象
logger = logging.getLogger("villia")
# 设置日志级别为DEBUG这样可以输出所有级别的日志
logger.setLevel(logging.DEBUG)
# 创建一个StreamHandler对象用于输出日志到控制台
console_handler = logging.StreamHandler()
# 设置控制台输出的日志格式和颜色
logger.propagate = False
console_format = '%(log_color)s%(asctime)s - %(name)s - %(levelname)s - %(message)s'
console_colors = {
'DEBUG': 'white',
'INFO': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'bold_red',
}
console_formatter = colorlog.ColoredFormatter(console_format, log_colors=console_colors)
console_handler.setFormatter(console_formatter)
# 将控制台处理器添加到logger对象中
logger.addHandler(console_handler)
return logger
2023-07-22 08:39:49 +08:00
2024-02-27 21:33:36 +08:00
logger = newLogger()
2023-07-22 19:38:52 +08:00
def main():
2024-07-25 10:33:31 +08:00
print(
"""请输入要执行的指令:
1 绑定到远程仓库(如果通过源码包安装请执行)
2 更新bot代码
3 清理无用数据(如缓存图片)
4 导出群信息制作一个chatLearning可用的配置文件
5 其他素材下载(可选)
6 安装vits功能对应依赖""")
a = input("输入要执行的数字")
2024-07-22 21:27:42 +08:00
if a=="1":
2024-06-26 01:00:02 +08:00
os.system(f"{git_path} init")
os.system(f"{git_path} remote add origin https://github.com/avilliai/Manyana.git")
2023-07-24 11:46:20 +08:00
print("正在添加文件.....这可能需要较长时间")
2024-06-26 01:00:02 +08:00
os.system(f"{git_path} add .")
2023-07-22 19:38:52 +08:00
print("over")
2024-07-22 21:27:42 +08:00
elif a=="2":
2024-02-27 21:33:36 +08:00
updaat()
2024-07-22 21:27:42 +08:00
elif a=="3":
2023-07-23 13:19:52 +08:00
print("执行清理缓存操作")
shutil.rmtree("data/pictures/benzi")
if not os.path.exists("data/pictures/benzi"):
os.mkdir("data/pictures/benzi")
2024-07-25 10:33:31 +08:00
aimdir = ["data/pictures/avatars", "data/voices", "data/music/musicCache", "data/pictures/cache",
"data/pictures/wallpaper", "data/blueArchive/arona", "data/blueArchive/cache", "data/Elo",
"data/arknights", "data/backrooms"]
for ib in aimdir:
ls1 = os.listdir(ib)
for i in ls1:
try:
os.remove(f"{ib}/" + i)
except:
continue
2023-07-23 13:19:52 +08:00
print("清理缓存完成")
2024-07-22 21:27:42 +08:00
elif a=="4":
import json
2024-07-22 21:27:42 +08:00
print("chatLearning https://mirai.mamoe.net/topic/1018/chatlearning-%E8%AE%A9bot%E5%AD%A6%E4%BC%9A%E4%BD%A0%E7%9A%84%E7%BE%A4%E8%81%8A/8")
if os.path.exists("config.clc"):
2023-09-14 13:07:31 +08:00
with open("config.clc", "r", encoding="utf_8_sig") as ass:
p = json.loads(ass.read())
else:
p = {
"unmergegrouplist": [],
"learninggrouplist": [
628763673,
699455559
],
"replygrouplist": [
628763673,
699455559
],
"interval": 900,
"sendmode": 0,
"mergetime": 900,
"learning": 0,
"merge": 0,
"reply": 0,
"replychance": 50,
"admin": 0,
"Administrator": [
123456
],
"blackfreq": 5,
"voicereply": 0,
"voicereplychance": 20,
"synthesizer": "",
"version": "3.0.3",
"tag": {},
"singlereplychance": {},
"singlevoicereplychance": {},
"typefreq": {},
"tempmessagenum": 32,
"fastdelete": 0,
"replywait": [
0,
0
],
"voicecharerror": "存在违规字符,转换失败",
"voicecderror": "转换冷却中",
"voicelengtherror": "长度超过限制",
"deletesuccess": "已从词库内删除!",
"deletetemperror": "删除失败,该消息已不在缓存内",
"deletefinderror": "删除失败,词库中已无法找到该答案",
"replycd": 3,
"stopsign": 0,
"voicecommand": "快说 ",
"cosmatch": 0,
"cosmatching": 0.5,
"botname": [
""
],
"replylength": 100,
"atreply": 1
}
file1 = open('data/music/groups.txt', 'r')
js1 = file1.read()
js1 = json.loads(js1)
file1.close()
da = js1.keys()
print(list(da), type(list(da)))
p["replygrouplist"] = list(da)
p["learninggrouplist"] = list(da)
2024-07-22 21:27:42 +08:00
ja = json.dumps(p,ensure_ascii=False,indent=1)
with open("config.clc", "w", encoding="utf_8_sig",newline='\n') as fp:
fp.write(ja)
print("完毕请用config.clc覆盖chatLearning文件夹下同名文件")
2024-07-15 17:23:57 +08:00
elif a=="5":
2024-06-13 23:18:58 +08:00
logger.info("图片素材下载/更新\n输入要执行的指令:\n1 下载/更新星穹铁道资源")
2024-07-22 21:27:42 +08:00
orderP=input("在这里输入指令:")
if orderP=="1":
2024-06-13 23:18:58 +08:00
if os.path.exists("./data/star-rail-atlas"):
os.chdir("./data")
logger.info("文件夹已存在,进入更新模式")
2024-06-26 01:00:02 +08:00
os.system(f"{git_path} pull https://gitee.com/Nwflower/star-rail-atlas.git")
2024-06-13 23:18:58 +08:00
else:
os.chdir("./data")
logger.info("文件夹不存在,拉取素材")
2024-06-26 01:00:02 +08:00
os.system(f"{git_path} clone https://gitee.com/Nwflower/star-rail-atlas.git")
2024-07-22 21:27:42 +08:00
elif a=="6":
logger.info("开始安装vits语音模块相关依赖")
os.system(f"\"{python_path}\" -m pip install -r vits/vitsRequirements.txt")
2023-07-22 19:38:52 +08:00
else:
print("结束")
2024-07-22 21:27:42 +08:00
def updaat(f=False,jump=False,source=None):
if jump==False:
2024-07-27 19:12:57 +08:00
logger.warning("更新python库可以按1跳过如果更新后启动报错请回来执行这一步")
2024-02-29 22:25:26 +08:00
if input("在这里输入:") != "1":
2024-06-04 23:37:51 +08:00
logger.warning("即将开始更新依赖库,请确保已关闭代理,否则无法安装依赖库")
input("按任意键继续:")
#print(str(custom_pip_path)+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!5555555!!!!!!!!!!!!!!!")
2024-07-29 00:47:29 +08:00
os.system(f"\"{python_path}\" -m pip install jmcomic")
2024-07-29 23:38:21 +08:00
os.system(f"\"{python_path}\" -m pip install img2pdf")
2024-07-30 12:38:06 +08:00
os.system(f"\"{python_path}\" -m pip install lanzou-api")
2024-07-22 21:27:42 +08:00
if source==None:
2024-02-29 22:25:26 +08:00
logger.info("拉取bot代码\n--------------------")
2024-07-27 17:41:09 +08:00
logger.info("选择更新源(git源 镜像源相互兼容)\n1 git源\n2 git代理源1\n3 git代理源2 \n4 国内源(稳定,但一般有一天延迟)")
2024-02-29 22:26:24 +08:00
source = input("选择更新源(输入数字 )")
2024-02-29 22:25:26 +08:00
else:
2024-07-22 21:27:42 +08:00
source=str(source)
2024-02-29 22:25:26 +08:00
if source == "1":
2024-02-27 21:33:36 +08:00
# os.system("git pull https://github.com/avilliai/Manyana.git")
# 启动进程
2024-07-15 17:23:57 +08:00
p = subprocess.Popen([f'{git_path}', 'pull', 'https://github.com/avilliai/Manyana.git'], stdout=subprocess.PIPE,
2024-06-16 11:23:17 +08:00
stderr=subprocess.PIPE)
2024-07-22 21:27:42 +08:00
elif source=="2":
p = subprocess.Popen([f'{git_path}', 'pull', 'https://github.moeyy.xyz/https://github.com/avilliai/Manyana'], stdout=subprocess.PIPE,
2024-06-16 11:24:30 +08:00
stderr=subprocess.PIPE)
2024-07-22 21:27:42 +08:00
elif source=="3":
p=subprocess.Popen([f'{git_path}', 'pull', 'https://mirror.ghproxy.com/https://github.com/avilliai/Manyana'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
2024-07-25 22:02:06 +08:00
elif source=="4":
2024-07-25 21:59:33 +08:00
p=subprocess.Popen([f'{git_path}', 'pull', 'https://www.gitlink.org.cn/lux-QAQ/Manyana'], stdout=subprocess.PIPE,stderr=subprocess.PIPE)
2024-07-25 22:02:06 +08:00
else:
logger.error("无效输入,重新执行")
updaat()
2024-02-28 12:00:19 +08:00
# 获取进程的输出和错误信息
stdout, stderr = p.communicate()
2024-06-16 11:17:49 +08:00
stdout = stdout.decode('utf-8', errors='ignore')
stderr = stderr.decode('utf-8', errors='ignore')
2024-02-28 12:00:19 +08:00
logger.info(stdout)
2024-06-16 17:21:48 +08:00
logger.warning(stderr)
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 标记是否在错误信息中
in_error_info = False
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 存放冲突文件名的列表
conflict_files = []
2024-07-22 21:27:42 +08:00
if f==True:
2024-02-28 12:00:19 +08:00
if os.path.exists("./temp"):
if len(os.listdir("./config")) != 14:
2024-07-22 21:27:42 +08:00
logger.error("文件数目异常请参考远程仓库config文件夹https://github.com/avilliai/Manyana/tree/main/config补全缺失文件。")
input("如已补全请按任意键继续:")
2024-02-28 12:00:19 +08:00
for i in os.listdir("./temp"):
2024-07-22 21:27:42 +08:00
logger.info("开始处理"+i)
if os.path.exists("config/"+i):
conflict_file_dealter("./temp/"+i,"config/"+i)
2024-03-24 14:25:54 +08:00
if os.path.exists("./temp/conflict " + i):
os.remove("./temp/conflict " + i)
os.rename("./temp/" + i, "./temp/conflict " + i)
elif os.path.exists("data/"+i):
conflict_file_dealter("./temp/"+i,"data/"+i)
if os.path.exists("./temp/conflict " + i):
os.remove("./temp/conflict " + i)
os.rename("./temp/" + i, "./temp/conflict " + i)
2024-02-28 12:00:19 +08:00
else:
continue
2024-07-22 21:27:42 +08:00
#shutil.rmtree("./temp")
2024-02-28 12:00:19 +08:00
logger.info("处理冲突文件完成")
2024-03-17 20:01:24 +08:00
logger.info("旧的冲突文件被保存到了temp文件夹以防万一你需要它们。")
2024-02-28 12:00:19 +08:00
logger.info("你可以关闭此窗口了")
2024-06-04 23:37:51 +08:00
logger.warning("如果出现启动闪退,重新执行更新脚本,不要跳过 更新python库 环节,执行结束后即可正常启动")
2024-02-28 12:00:19 +08:00
input()
# 逐行检查错误信息
for line in stderr.split('\n'):
# 标记冲突文件名开始位置
if 'error: Your local changes to the following files would be overwritten by merge:' in line:
in_error_info = True
continue # 结束当前循环,进入下一个循环
2024-06-16 14:57:11 +08:00
elif 'error: 您对下列文件的本地修改将被合并操作覆盖:' in line:
2024-07-22 21:27:42 +08:00
in_error_info=True
2024-06-16 14:57:11 +08:00
continue
2024-02-28 12:00:19 +08:00
# 标记冲突文件名结束位置
if 'Please commit your changes or stash them before you merge.' in line:
in_error_info = False
2024-06-16 14:57:11 +08:00
elif '请在合并前提交或贮藏您的修改。' in line:
2024-07-22 21:27:42 +08:00
in_error_info=False
2024-02-28 12:00:19 +08:00
# 将冲突文件名添加到列表
if in_error_info:
conflict_files.append(line.strip())
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
# 显示冲突的文件列表
for file in conflict_files:
print('冲突文件:', file)
logger.warning("开始处理冲突文件")
if file.endswith(".py"):
os.remove(file)
logger.warning("移除了" + file)
elif file.endswith(".yaml"):
logger.info("冲突的配置文件" + file)
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
logger.warning("开始处理冲突文件.....读取中")
2024-02-27 21:33:36 +08:00
2024-02-28 12:00:19 +08:00
if os.path.exists("./temp"):
2024-03-30 19:25:56 +08:00
try:
2024-07-22 21:27:42 +08:00
shutil.copyfile(file, file.replace("config/", "temp/").replace("data/","temp/"))
2024-03-30 19:25:56 +08:00
os.remove(file)
except:
continue
2024-02-28 12:00:19 +08:00
else:
os.mkdir("./temp")
shutil.copyfile(file, file.replace("config", "temp").replace("data","temp"))
2024-02-28 12:00:19 +08:00
os.remove(file)
else:
2024-06-16 14:57:11 +08:00
os.remove(file)
logger.warning("移除了 " + file)
2024-07-22 21:27:42 +08:00
#logger.warning("请自行决定删除或修改文件名称,在重新拉取后根据旧文件重新填写新文件")
2024-04-14 13:13:47 +08:00
logger.warning("开始处理冲突文件")
logger.info("即将再次执行拉取操作")
2024-07-22 21:27:42 +08:00
updaat(True,True,str(source))
2024-02-28 12:00:19 +08:00
# 不要忘了等待进程结束
p.wait()
2024-02-27 21:33:36 +08:00
2024-07-22 21:27:42 +08:00
2024-02-27 21:33:36 +08:00
logger.info("结束")
logger.info("如更新成功请自行查看 更新日志.yaml")
# 创建一个YAML对象来加载和存储YAML数据
try:
from ruamel.yaml import YAML
except Exception as e:
logger.error("未安装ruamel.yaml库无法处理冲突文件开始安装缺少的依赖")
os.system(f"\"{python_path}\" -m pip install ruamel.yaml")
2024-02-27 21:33:36 +08:00
from ruamel.yaml import YAML
# 创建一个YAML对象来加载和存储YAML数据
yaml = YAML()
def merge_dicts(old, new):
for k, v in old.items():
# 如果值是一个字典并且键在新的yaml文件中那么我们就递归地更新键值对
if isinstance(v, dict) and k in new and isinstance(new[k], dict):
merge_dicts(v, new[k])
# 如果键在新的yaml文件中我们就更新它的值
elif k in new:
2024-07-22 21:27:42 +08:00
logger.info("更新key"+str(k)+" value"+str(v))
2024-02-27 21:33:36 +08:00
new[k] = v
def conflict_file_dealter(file_old='old_aiReply.yaml', file_new='new_aiReply.yaml'):
# 加载旧的YAML文件
2024-07-22 21:27:42 +08:00
with open(file_old, 'r',encoding="utf-8") as file:
2024-02-27 21:33:36 +08:00
old_data = yaml.load(file)
# 加载新的YAML文件
2024-07-22 21:27:42 +08:00
with open(file_new, 'r',encoding="utf-8") as file:
2024-02-27 21:33:36 +08:00
new_data = yaml.load(file)
# 遍历旧的YAML数据并更新新的YAML数据中的相应值
merge_dicts(old_data, new_data)
# 把新的YAML数据保存到新的文件中
2024-07-22 21:27:42 +08:00
with open(file_new, 'w',encoding="utf-8") as file:
2024-02-27 21:33:36 +08:00
yaml.dump(new_data, file)
2024-07-22 21:27:42 +08:00
2023-07-22 19:38:52 +08:00
# 添加远程仓库
# 获取远程更新并合并到本地
if __name__ == '__main__':
2023-07-26 09:15:36 +08:00
main()
2024-03-17 16:44:15 +08:00
input("按任意键退出.")