Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/devbot/run.py
blob: 08ebe94787cde6985ad487cdd72ac594a3693705 (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
import os
import string
import random
import subprocess
import sys
import time
import tempfile

from devbot import config


def run(command):
    args = [command, "--home-dir", config.home_dir]

    resolution = config.get_pref("RESOLUTION")
    if resolution:
        args.extend(["--resolution", resolution])

    output = config.get_pref("OUTPUT")
    if output:
        args.extend(["--output", output])

    stdout = open(config.log_path, 'a')
    stderr = stdout

    os.dup2(stdout.fileno(), sys.stdout.fileno())
    os.dup2(stderr.fileno(), sys.stderr.fileno())

    os.execlp(args[0], *args)


def run_test(command, test_path):
    temp_dir = tempfile.mkdtemp("sugar-build-test")
    display_path = os.path.join(temp_dir, "display")

    args = [command,
            "--display-path", display_path,
            "--virtual"]

    command_process = subprocess.Popen(args, stdout=subprocess.PIPE)

    while True:
        if not os.path.exists(display_path):
            time.sleep(1)
        else:
            break

    with open(display_path) as f:
        os.environ["DISPLAY"] = f.read()

    os.unlink(display_path)
    os.rmdir(temp_dir)

    try:
        subprocess.check_call(["python", "-u", test_path])
        result = True
    except subprocess.CalledProcessError:
        result = False

    command_process.terminate()

    return result


def collect_logs(source_path):
    logs = {}
    for filename in os.listdir(source_path):
        if filename.endswith(".log"):
            path = os.path.join(source_path, filename)
            with open(path) as f:
                logs[filename] = f.read()

    with open(config.log_path, "w") as f:
        for filename, log in logs.items():
            f.write("\n===== %s =====\n\n%s" % (filename, log))


def _get_random_id():
    return ''.join(random.choice(string.letters) for i in xrange(8))