From 63103ea3f6ed5afc8aece4c314feece7bb5852b5 Mon Sep 17 00:00:00 2001 From: ngfchl Date: Tue, 6 Dec 2022 06:11:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=A8=8B=E5=BA=8F=EF=BC=8C=E6=B5=8B=E8=AF=95=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_pt/views.py | 7 +++++ pt_site/views.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/auto_pt/views.py b/auto_pt/views.py index 6b10700..f363ddf 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -396,6 +396,7 @@ def update_page(request): }) +""" def exec_command(commands): result = [] for key, command in commands.items(): @@ -406,9 +407,14 @@ def exec_command(commands): 'res': p.returncode }) return result +""" def do_update(request): + return JsonResponse(data=pt_site.auto_upgrade().to_dict(), safe=False) + + +""" try: logger.info('开始更新') pt_site_site_mtime = os.stat('pt_site_site.json').st_mtime @@ -467,6 +473,7 @@ def do_update(request): return JsonResponse(data=CommonResponse.error( msg=msg ).to_dict(), safe=False) +""" def do_xpath(request): diff --git a/pt_site/views.py b/pt_site/views.py index 63ff6d7..5dab973 100644 --- a/pt_site/views.py +++ b/pt_site/views.py @@ -1,7 +1,10 @@ # Create your views here. import datetime +import json import logging +import os import socket +import subprocess import time from concurrent.futures.thread import ThreadPoolExecutor @@ -10,7 +13,7 @@ from django_apscheduler.jobstores import DjangoJobStore from pt_site.UtilityTool import PtSpider, MessageTemplate, FileSizeConvert from pt_site.models import MySite, TorrentInfo -from ptools.base import StatusCodeEnum +from ptools.base import StatusCodeEnum, CommonResponse job_defaults = { 'coalesce': True, @@ -216,6 +219,83 @@ def auto_get_torrent_hash(): '> {} 任务运行成功!耗时:{}{} \n'.format('获取种子HASH', end - start, time.strftime("%Y-%m-%d %H:%M:%S"))) +def exec_command(commands): + """执行命令行命令""" + result = [] + for key, command in commands.items(): + p = subprocess.run(command, shell=True) + logger.info('{} 命令执行结果:\n{}'.format(key, p)) + result.append({ + 'command': key, + 'res': p.returncode + }) + return result + + +def auto_upgrade(): + """程序更新""" + try: + logger.info('开始自动更新') + pt_site_site_mtime = os.stat('pt_site_site.json').st_mtime + requirements_mtime = os.stat('requirements.txt').st_mtime + update_commands = { + # 'cp db/db.sqlite3 db/db.sqlite3-$(date "+%Y%m%d%H%M%S")', + '强制覆盖本地': 'git reset --hard', + '获取更新信息': 'git fetch --all', + '拉取代码更新': 'git pull origin {}'.format(os.getenv('DEV')), + } + requirements_commands = { + '安装依赖': 'pip install -r requirements.txt', + } + migrate_commands = { + '同步数据库': 'python manage.py migrate', + } + logger.info('拉取最新代码') + result = exec_command(update_commands) + new_requirements_mtime = os.stat('requirements.txt').st_mtime + if new_requirements_mtime > requirements_mtime: + logger.info('更新环境依赖') + result.extend(exec_command(requirements_commands)) + new_pt_site_site = os.stat('pt_site_site.json').st_mtime + logger.info('更新前文件最后修改时间') + logger.info(pt_site_site_mtime) + logger.info('更新后文件最后修改时间') + logger.info(new_pt_site_site) + if new_pt_site_site == pt_site_site_mtime: + logger.info('本次无规则更新,跳过!') + result.append({ + 'command': '本次无更新规则', + 'res': 0 + }) + pass + else: + logger.info('拉取更新完毕,开始更新Xpath规则') + p = subprocess.run('cp db/db.sqlite3 db/db.sqlite3-$(date "+%Y%m%d%H%M%S")', shell=True) + logger.info('备份数据库 命令执行结果:\n{}'.format(p)) + result.append({ + 'command': '备份数据库', + 'res': p.returncode + }) + result.extend(exec_command(migrate_commands)) + logger.info('同步数据库 命令执行结果:\n{}'.format(p)) + logger.info('更新完毕') + pt_spider.send_text(json.dumps(result)) + return CommonResponse.success( + msg='更新成功,15S后自动刷新页面!', + data={ + 'result': result + } + ) + except Exception as e: + # raise + msg = '更新失败!{},请初始化Xpath!'.format(str(e)) + logger.error(msg) + pt_spider.send_text(msg) + return CommonResponse.error( + msg=msg + ) + + try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(("127.0.0.1", 44444))