26:8c1ce7d12350
Anton Shestakov <av6@dwimlabs.net>, Wed, 15 Jun 2016 23:49:22 +0800
viewer: use new get_url() method for project and check links

next change 42:06c224e8b052
previous change 20:5d830ac627fc

incoming.py

Permissions: -rwxr-xr-x

Other formats: Feeds:
#!/usr/bin/env python
from __future__ import absolute_import
import fileinput
import logging
import os
import re
from datetime import datetime
from urlparse import urlparse
from tornado.escape import json_encode
from candolint.models import database, Project, Change, Check
rel = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x))
def parse_timestamp(value):
return datetime.strptime(value, '%Y-%m-%dT%H:%M:%S+00:00')
def insert_check(lines):
text = ''.join(lines)
meta_prefix = '# C&O '
state = 'not even started'
result = []
errors = 0
warnings = 0
url = None
rev = None
node = None
branch = None
date = None
author = None
message = None
success = True
started = None
finished = None
for line in lines:
cls = None
if state == 'checks':
"""
m = re.match(r'^(.+):\d+:\d+: ')
if m is not None:
filename = m.group(1)
"""
# pep8-specific E9xx
# https://pep8.readthedocs.io/en/latest/intro.html#error-codes
if re.match(r'^.+:\d+:\d+: E9\d{2}', line) is not None:
errors += 1
cls = 'error'
elif re.match(r'^.+:\d+:\d+: [EWFCN]\d{3}', line) is not None:
warnings += 1
cls = 'warning'
elif re.match(r'^.+:\d+:\d+: \[error\]', line) is not None:
errors += 1
cls = 'error'
elif re.match(r'^.+:\d+:\d+: \[warning\]', line) is not None:
warnings += 1
cls = 'warning'
elif re.match(r'^.+:\d+:\d+: \(E\d{3}\)', line) is not None:
errors += 1
cls = 'error'
elif re.match(r'^.+:\d+:\d+: \(W\d{3}\)', line) is not None:
warnings += 1
cls = 'warning'
elif re.match(r'^.+:\d+:\d+: ', line) is not None:
warnings += 1
cls = 'warning'
if line.startswith(meta_prefix):
cls = 'meta'
rest = line[len(meta_prefix):]
m = re.match('^task: (\w+)$', rest)
if m is not None:
state = m.group(1)
cls = 'task'
m = re.match('^job failed$', rest)
if m is not None:
success = False
m = re.match('^job (started|finished): ([\dT:+-]+)$', rest)
if m is not None:
if m.group(1) == 'started':
started = parse_timestamp(m.group(2))
elif m.group(1) == 'finished':
finished = parse_timestamp(m.group(2))
m = re.match('^project URL: (\S+)$', rest)
if m is not None:
url = m.group(1)
m = re.match('^commit: (\d+):(\w+) (\w+)$', rest)
if m is not None:
rev = m.group(1)
node = m.group(2)
branch = m.group(3)
m = re.match('^commit date: (.+)$', rest)
if m is not None:
date = m.group(1)
m = re.match('^commit author: (.+)$', rest)
if m is not None:
author = m.group(1)
m = re.match('^commit message: (.+)$', rest)
if m is not None:
message = m.group(1)
result.append((cls, line))
parsed = urlparse(url)
domain = parsed.hostname
if domain is None:
domain = 'self'
parts = [part for part in parsed.path.split('/') if part]
if len(parts) > 1:
user, name = parts[-2:]
else:
user = None
name = parts[0]
with database.transaction():
project, created = Project.create_or_get(
url=url,
domain=domain,
user=user,
name=name)
change, created = Change.create_or_get(
rev=rev,
node=node,
branch=branch,
date=date,
author=author,
message=message,
project=project)
Check.create(
ordinal=Check.select().where(Check.project == project).count() + 1,
errors=errors,
warnings=warnings,
raw=text,
lines=json_encode(result),
success=success,
started=started,
finished=finished,
project=project,
change=change)
def main():
database.init(rel('database.sqlite'))
database.connect()
database.create_tables([Project, Change, Check], safe=True)
logger = logging.getLogger('peewee')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
insert_check(list(fileinput.input()))
database.close()
if __name__ == '__main__':
main()