summaryrefslogtreecommitdiffstats
path: root/retrace/interface/retrace.py
blob: 87f48c53c76c8d55c8d349d659932e8f31194b2f (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
89
#!/usr/bin/python
import os
import re
import ConfigParser
from webob import Request
from subprocess import *

REQUIRED_FILES = ["architecture", "coredump", "package"] #, "packages"]

DF_OUTPUT_PARSER = re.compile("^([^ ^\t]*)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+%)[ \t]+(.*)$")
XZ_OUTPUT_PARSER = re.compile("^totals[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+\.[0-9]+)[ \t]+([^ ^\t]+)[ \t]+([0-9]+)[ \t]+([0-9]+)$")

CONFIG_FILE = "/etc/abrt/retrace.conf"
CONFIG = {
    "MaxParallelTasks": 2,
    "MaxPackedSize": 30,
    "MaxUnpackedSize": 600,
    "MinStorageLeft": 10240,
    "WorkDir": "/var/spool/abrt-retrace",
}

def read_config():
    parser = ConfigParser.ConfigParser()
    parser.read(CONFIG_FILE)
    for key in CONFIG.keys():
        vartype = type(CONFIG[key])
        if vartype is int:
            get = parser.getint
        elif vartype is bool:
            get = parser.getbool
        elif vartype is float:
            get = parser.getfloat
        else:
            get = parser.get

        try:
            CONFIG[key] = get("retrace", key)
        except:
            pass

def free_space(path):
    pipe = Popen(["df", path], stdout=PIPE).stdout
    for line in pipe.readlines():
        match = DF_OUTPUT_PARSER.match(line)
        if match:
            pipe.close()
            return 1024 * int(match.group(4))

    pipe.close()
    return None

def unpacked_size(archive):
    pipe = Popen(["/usr/local/bin/xz", "--list", "--robot", archive], stdout=PIPE).stdout
    for line in pipe.readlines():
        match = XZ_OUTPUT_PARSER.match(line)
        if match:
            pipe.close()
            return int(match.group(4))

    pipe.close()
    return None

def new_crash():
    i = 1
    newdir = CONFIG["WorkDir"] + "/0"
    while os.path.exists(newdir):
        i += 1
        newdir = CONFIG["WorkDir"] + "/" + str(i)

    try:
        os.mkdir(newdir)
        return i, newdir
    except:
        return None, None

def unpack(archive):
    pipe = Popen(["tar", "xJf", archive])
    pipe.wait()
    return pipe.returncode

def get_task_password(crashdir):
    return "Password for " + crashdir

def get_task_est_time(crashdir):
    return 10

def response(start_response, status, body="", extra_headers=[]):
    start_response(status, [("Content-Type", "text/plain"), ("Content-Length", str(len(body)))] + extra_headers)
    return [body]