summaryrefslogtreecommitdiffstats
path: root/git_taskrepo/taskrepo.py
blob: 9028101e6d50e0917e5bc30e618443e579ce317d (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
from git_taskrepo.testinfo import SemiStrictParser, ParserError
import string
import os
import commands

# Keys from testinfo to populate with
keys = ("test_archs",
        "releases",
        "runfor",
        "types",
        "bugs",
        )

singles = ("test_name",
           "test_description",
           "owner",
           )

class TaskRepoException(Exception):
    pass

class TRX(TaskRepoException):
    pass

class TRX_Parse(TRX):
    pass

class TRX_TestInfo(TRX):
    pass

def only_ascii(s):
    return filter(lambda x: x in string.printable, s)

def _update_taskrepo(taskrepo, taskname, testinfo):
    with taskrepo:
        cur = taskrepo.cursor()
        # Do we already have an entry for this task?
        cur.execute("SELECT id FROM tasks WHERE name=?", (taskname,))
        result = cur.fetchone()
        if result:
            taskid = result[0]
        else:
            cur.execute("INSERT INTO tasks(name) VALUES (?)", (taskname,))
            taskid = cur.lastrowid;

        # Clear old values
        cur.execute("DELETE FROM key_value_inc WHERE task_id=?", (taskid,))
        cur.execute("DELETE FROM key_value_exc WHERE task_id=?", (taskid,))

        # Populate with new values
        for key in keys:
            for value in getattr(testinfo, key):
                if value and type(value) == type(str()) and value[0] == '-':
                    table = "key_value_exc"
                    value = value[1:]
                else:
                    table = "key_value_inc"
                cur.execute("INSERT INTO %s VALUES (?,?,?)" % table, (taskid, key, value))
        for key in singles:
            value = only_ascii(getattr(testinfo, key))
            cur.execute("INSERT INTO key_value_inc VALUES (?,?,?)", (taskid, key, value))

def update_taskrepo(repo, taskrepo, taskpath):
    if os.path.exists(os.path.join(taskpath, "Makefile")):
        (status, output) = commands.getstatusoutput("make -C %s -q testinfo.desc" % taskpath)
        if status == 1:
            os.system("make -C %s testinfo.desc" % taskpath)
    if os.path.exists(os.path.join(taskpath, "testinfo.desc")):
        taskname = taskpath.split(repo.working_tree_dir)[1]
        try:
            testinfo = parse_testinfo(os.path.join(taskpath, "testinfo.desc"))
        except ParserError:
            raise
        _update_taskrepo(taskrepo, taskname, testinfo)
    else:
        raise TRX_TestInfo('No testinfo.desc')

def parse_testinfo(filename):
    p = SemiStrictParser(True)
    p.parse(open(filename).readlines())
    return p.info