--- a/candolint/models.py Mon Jun 13 11:10:56 2016 +0800
+++ b/candolint/models.py Mon Jun 13 11:14:12 2016 +0800
+ def create_or_get(cls, **kwargs):
+ with cls._meta.database.atomic():
+ return cls.create(**kwargs), True
+ except pw.IntegrityError:
+ # a variation on https://github.com/coleifer/peewee/pull/681
+ compound_unique = set()
+ for field_names, unique in cls._meta.indexes:
+ compound_unique.update(field_names)
+ for field_name, value in kwargs.items():
+ field = getattr(cls, field_name)
+ if field.unique or field.primary_key or field_name in compound_unique:
+ query.append(field == value)
+ return cls.get(*query), False
class Project(BaseModel):
url = pw.CharField(unique=True)
--- a/incoming.py Mon Jun 13 11:10:56 2016 +0800
+++ b/incoming.py Mon Jun 13 11:14:12 2016 +0800
+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):
+ if line.startswith(meta_prefix):
+ rest = line[len(meta_prefix):]
+ m = re.match('^task: (\w+)$', rest)
+ m = re.match('^job failed$', rest)
+ m = re.match('^job (started|finished): ([\dT:+-]+)$', rest)
+ 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)
+ m = re.match('^commit: (\d+):(\w+) (\w+)$', rest)
+ m = re.match('^commit date: (.+)$', rest)
+ m = re.match('^commit author: (.+)$', rest)
+ m = re.match('^commit message: (.+)$', rest)
+ result.append((cls, line))
+ domain = parsed.hostname
+ parts = [part for part in parsed.path.split('/') if part]
+ user, name = parts[-2:]
+ with database.transaction():
+ project, created = Project.create_or_get(
+ change, created = Change.create_or_get(
+ ordinal=Check.select().where(Check.project == project).count() + 1,
+ lines=json_encode(result),
database.init(rel('database.sqlite'))
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()))
--- a/requirements.txt Mon Jun 13 11:10:56 2016 +0800
+++ b/requirements.txt Mon Jun 13 11:14:12 2016 +0800