clean up duplicate code
This commit is contained in:
parent
3c55162a77
commit
b35ef74a1a
4 changed files with 64 additions and 79 deletions
57
bot.py
57
bot.py
|
@ -1,23 +1,48 @@
|
|||
from core.utils import init_bot
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from queue import Queue, Empty
|
||||
from threading import Thread
|
||||
|
||||
from core.bots import run
|
||||
|
||||
cache_path = os.path.abspath('./cache/')
|
||||
if os.path.exists(cache_path):
|
||||
shutil.rmtree(cache_path)
|
||||
os.mkdir(cache_path)
|
||||
else:
|
||||
os.mkdir(cache_path)
|
||||
def enqueue_output(out, queue):
|
||||
for line in iter(out.readline, b''):
|
||||
queue.put(line)
|
||||
out.close()
|
||||
|
||||
version = os.path.abspath('.version')
|
||||
write_version = open(version, 'w')
|
||||
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:7])
|
||||
write_version.close()
|
||||
|
||||
tag = os.path.abspath('.version_tag')
|
||||
write_tag = open(tag, 'w')
|
||||
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
|
||||
write_tag.close()
|
||||
def run():
|
||||
logging.basicConfig(format="%(msg)s", level=logging.INFO)
|
||||
botdir = './core/bots/'
|
||||
lst = os.listdir(botdir)
|
||||
runlst = []
|
||||
for x in lst:
|
||||
bot = f'{botdir}{x}/bot.py'
|
||||
if os.path.exists(bot):
|
||||
p = subprocess.Popen(f'python {bot}', shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
runlst.append(p)
|
||||
q = Queue()
|
||||
threads = []
|
||||
for p in runlst:
|
||||
threads.append(Thread(target=enqueue_output, args=(p.stdout, q)))
|
||||
|
||||
for t in threads:
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
while True:
|
||||
try:
|
||||
line = q.get_nowait()
|
||||
except Empty:
|
||||
pass
|
||||
else:
|
||||
logging.info(line.decode('utf8')[:-1])
|
||||
|
||||
# break when all processes are done.
|
||||
if all(p.poll() is not None for p in runlst):
|
||||
break
|
||||
|
||||
|
||||
init_bot()
|
||||
run()
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
from queue import Queue, Empty
|
||||
from threading import Thread
|
||||
|
||||
|
||||
def enqueue_output(out, queue):
|
||||
for line in iter(out.readline, b''):
|
||||
queue.put(line)
|
||||
out.close()
|
||||
|
||||
|
||||
def run():
|
||||
logging.basicConfig(format="%(msg)s", level=logging.INFO)
|
||||
botdir = './core/bots/'
|
||||
lst = os.listdir(botdir)
|
||||
runlst = []
|
||||
for x in lst:
|
||||
bot = f'{botdir}{x}/bot.py'
|
||||
if os.path.exists(bot):
|
||||
p = subprocess.Popen(f'python {bot}', shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
runlst.append(p)
|
||||
q = Queue()
|
||||
threads = []
|
||||
for p in runlst:
|
||||
threads.append(Thread(target=enqueue_output, args=(p.stdout, q)))
|
||||
|
||||
for t in threads:
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
while True:
|
||||
try:
|
||||
line = q.get_nowait()
|
||||
except Empty:
|
||||
pass
|
||||
else:
|
||||
logging.info(line.decode('utf8')[:-1])
|
||||
|
||||
# break when all processes are done.
|
||||
if all(p.poll() is not None for p in runlst):
|
||||
break
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
import re
|
||||
import shutil
|
||||
import traceback
|
||||
import uuid
|
||||
from os.path import abspath
|
||||
|
@ -9,6 +10,26 @@ import filetype as ft
|
|||
|
||||
from core.logger import Logger
|
||||
|
||||
|
||||
def init_bot():
|
||||
cache_path = os.path.abspath('./cache/')
|
||||
if os.path.exists(cache_path):
|
||||
shutil.rmtree(cache_path)
|
||||
os.mkdir(cache_path)
|
||||
else:
|
||||
os.mkdir(cache_path)
|
||||
|
||||
version = os.path.abspath('.version')
|
||||
write_version = open(version, 'w')
|
||||
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:7])
|
||||
write_version.close()
|
||||
|
||||
tag = os.path.abspath('.version_tag')
|
||||
write_tag = open(tag, 'w')
|
||||
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
|
||||
write_tag.close()
|
||||
|
||||
|
||||
"""
|
||||
async def load_prompt():
|
||||
author_cache = os.path.abspath('.cache_restart_author')
|
||||
|
|
22
unit_test.py
22
unit_test.py
|
@ -8,10 +8,9 @@ if not Config('db_path'):
|
|||
|
||||
import asyncio
|
||||
import traceback
|
||||
import os
|
||||
import shutil
|
||||
import aioconsole
|
||||
|
||||
from core.utils import init_bot
|
||||
from core.elements import Module
|
||||
from core.elements.message import MsgInfo, Session
|
||||
from core.unit_test.template import Template as MessageSession, FetchTarget
|
||||
|
@ -19,23 +18,6 @@ from core.parser.message import parser
|
|||
from core.scheduler import Scheduler
|
||||
from core.loader import Modules
|
||||
|
||||
cache_path = os.path.abspath('./cache/')
|
||||
if os.path.exists(cache_path):
|
||||
shutil.rmtree(cache_path)
|
||||
os.mkdir(cache_path)
|
||||
else:
|
||||
os.mkdir(cache_path)
|
||||
|
||||
version = os.path.abspath('.version')
|
||||
write_version = open(version, 'w')
|
||||
write_version.write(os.popen('git rev-parse HEAD', 'r').read()[0:7])
|
||||
write_version.close()
|
||||
|
||||
tag = os.path.abspath('.version_tag')
|
||||
write_tag = open(tag, 'w')
|
||||
write_tag.write(os.popen('git tag -l', 'r').read().split('\n')[-2])
|
||||
write_tag.close()
|
||||
|
||||
|
||||
async def unit_test_scheduler():
|
||||
gather_list = []
|
||||
|
@ -62,7 +44,7 @@ async def unit_test_command():
|
|||
except Exception:
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
init_bot()
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(unit_test_scheduler())
|
||||
loop.create_task(unit_test_command())
|
||||
|
|
Reference in a new issue