aboutsummaryrefslogtreecommitdiffstats
path: root/Post/parser.py
blob: 900148a1d2fa36bd672d7c37b2483dd694773e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python

# Add errors to database from client
#
# Copyright (C) 2013 Intel Corporation
# Author: Andreea Brandusa Proca <andreea.b.proca@intel.com>
#
# Licensed under the MIT license, see COPYING.MIT for details

import json, re
from Post.models import Build, BuildFailure
from django.conf import settings
from django.utils import timezone
from django.core.urlresolvers import reverse

class Parser:

    def __init__(self, data):
        self.data = data

    # returns true if the values contain '<' char
    # Ignore the failures field (which is an array anyway)
    def contains_tags (self, data):
        for key,val in data.items():
            if key == 'failures':
                continue

            if '<' in val:
                return True
        return False

    def parse(self, host):
        build_fails_logged = []

        try:
            jsondata = json.loads(self.data)
        except:
             return  { 'error' : 'Invalid json' }

        if self.contains_tags(jsondata) == True:
            return  { 'error' : 'Invalid characters in json' }

        b = Build.objects.create()
        try:
            b.MACHINE_NAME = str(jsondata['machine'])
            b.NATIVELSBSTRING = str(jsondata['nativelsb'])
            b.TARGET_SYS = str(jsondata['target_sys'])
            b.BRANCH_COMMIT = str(jsondata['branch_commit'])
            b.COMPONENT = str(jsondata['component'])
            b.BUILD_SYS = str(jsondata['build_sys'])
            b.DISTRO = str(jsondata['distro'])
            b.NAME = str(jsondata['username'])
            b.EMAIL = str(jsondata['email'])
            b.LINK_BACK = jsondata.get("link_back", None)

            g = re.match(r'(.*): (.*)', jsondata['branch_commit'])
            b.BRANCH = str(g.group(1))
            b.COMMIT = str(g.group(2))
            b.DATE = timezone.now()

            b.save()
            failures = jsondata['failures']
        except:
            return { 'error' : "Problem reading json payload" }

        for fail in failures:
            if len(fail) > int(settings.MAX_UPLOAD_SIZE):
                build_fails_logged.append({ 'id': -1, 'error' : "The size of the upload is too large" })
                continue
            package = str(fail['package'])
            g = re.match(r'(.*)\-(\d.*)', package)
            f = BuildFailure(TASK = str(fail['task']), RECIPE = g.group(1), RECIPE_VERSION = g.group(2), ERROR_DETAILS = str(fail['log']), BUILD = b)
            f.save()

            url = 'http://' + host + reverse('details', args=[f.id])

            build_fails_logged.append({ 'id' : f.id,
                                        'url' : url,
                                      })

        build_url = 'http://' + host + reverse('build_errors', args=[b.id])

        result = { 'build_id' : b.id,
                   'build_url' : build_url,
                   'failures' : build_fails_logged,
                 }

        return result