From e3b13edc1c74bb01dfc118fe7f2444341d0ccda8 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Sun, 21 Jan 2018 01:26:46 +0100 Subject: [PATCH] draft of history --- diffido.py | 51 +++++++++++++++++++++++++++++++++++++- dist/history.html | 63 +++++++++++++++++++++++++++++++++++++++++++++++ dist/index.html | 13 +++------- 3 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 dist/history.html diff --git a/diffido.py b/diffido.py index beeaace..7cee627 100755 --- a/diffido.py +++ b/diffido.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- import os +import re +import io import json import shutil import urllib @@ -28,6 +30,9 @@ API_VERSION = '1.0' SCHEDULES_FILE = 'conf/schedules.json' GIT_CMD = 'git' +re_insertion = re.compile(r'(\d+) insertion') +re_deletion = re.compile(r'(\d+) deletion') + logger = logging.getLogger() logger.setLevel(logging.INFO) @@ -68,7 +73,7 @@ def run_job(id_=None, *args, **kwargs): url = schedule.get('url') if not url: return - print('Running job id:%s title:%s url: %s' % (id_, schedule.get('title', ''), url)) + logger.debug('Running job id:%s title:%s url: %s' % (id_, schedule.get('title', ''), url)) req = requests.get(url, allow_redirects=True, timeout=(30.10, 240)) content = req.text req_path = urllib.parse.urlparse(req.url).path @@ -86,6 +91,41 @@ def run_job(id_=None, *args, **kwargs): p.join() +def get_history(id_): + def _history(id_, queue): + os.chdir('storage/%s' % id_) + p = subprocess.Popen([GIT_CMD, 'log', '--pretty=oneline', '--shortstat'], stdout=subprocess.PIPE) + stdout, _ = p.communicate() + queue.put(stdout) + queue = multiprocessing.Queue() + p = multiprocessing.Process(target=_history, args=(id_, queue)) + p.start() + res = queue.get().decode('utf-8') + p.join() + history = [] + res_io = io.StringIO(res) + while True: + commit_line = res_io.readline().strip() + if not commit_line: + break + commit_id, message = commit_line.split(' ', 1) + if len(commit_id) != 40: + continue + changes_line = res_io.readline().strip() + insert = re_insertion.findall(changes_line) + if insert: + insert = int(insert[0]) + else: + insert = 0 + delete = re_deletion.findall(changes_line) + if delete: + delete = int(delete[0]) + else: + delete = 0 + history.append({'id': commit_id, 'message': message, 'insertions': insert, 'deletions': delete, + 'changes': max(insert, delete)}) + return history + def scheduler_update(scheduler, id_): schedule = get_schedule(id_, addID=False) if not schedule: @@ -259,6 +299,12 @@ class ResetSchedulesHandler(BaseHandler): reset_from_schedules(self.scheduler) +class HistoryHandler(BaseHandler): + @gen.coroutine + def get(self, id_, *args, **kwargs): + self.write({'history': get_history(id_)}) + + class TemplateHandler(BaseHandler): """Handler for the / path.""" app_path = os.path.join(os.path.dirname(__file__), "dist") @@ -300,11 +346,14 @@ def serve(): _reset_schedules_path = r'schedules/reset' _schedules_path = r'schedules/?(?P\d+)?' + _history_path = r'history/?(?P\d+)' application = tornado.web.Application([ ('/api/%s' % _reset_schedules_path, ResetSchedulesHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _reset_schedules_path), ResetSchedulesHandler, init_params), ('/api/%s' % _schedules_path, SchedulesHandler, init_params), (r'/api/v%s/%s' % (API_VERSION, _schedules_path), SchedulesHandler, init_params), + ('/api/%s' % _history_path, HistoryHandler, init_params), + (r'/api/v%s/%s' % (API_VERSION, _history_path), HistoryHandler, init_params), (r'/?(.*)', TemplateHandler, init_params), ], static_path=os.path.join(os.path.dirname(__file__), 'dist/static'), diff --git a/dist/history.html b/dist/history.html new file mode 100644 index 0000000..86f2070 --- /dev/null +++ b/dist/history.html @@ -0,0 +1,63 @@ +{% extends "base.html" %} + +{% block body %} +
+
+
+ + +

History

+
+ + ${ item.id } + ${ item.message } + +
+
+
+
+ + + +{% end %} diff --git a/dist/index.html b/dist/index.html index 0d6d83a..1fc5297 100644 --- a/dist/index.html +++ b/dist/index.html @@ -9,9 +9,10 @@

Schedules add new

- ${ item.id } - ${ item.title } - ${ item.url } + ${ item.id } + ${ item.title } + ${ item.url } + history @@ -34,12 +35,6 @@ var app = new Vue({ computed: { }, methods: { - settingsUrl: function(item) { - if (!(item && item.id !== undefined)) { - return ''; - } - return '/schedule.html?id=' + item.id; - }, getSchedules: function() { self = this; var data = axios.get('/api/schedules').then(function(response) {