~oem-qa/checkbox/patch_deselect_ancestors_when_no_child_is_selected

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python

import re
import os
import sys
import posixpath

from subprocess import Popen, PIPE
from urlparse import urlparse

from optparse import OptionParser


DEFAULT_DIRECTORY = "/var/cache/checkbox/ltp"
DEFAULT_LOCATION = ":pserver:anonymous:@ltp.cvs.sourceforge.net:/cvsroot/ltp"
DEFAULT_TIMEOUT = 10800

COMMAND_TEMPLATE = "cd '%(directory)s' && ./runltp -f %(suite)s -r '%(directory)s' -p -q 2>/dev/null | ltp_filter --suite=%(suite)s"

def print_line(key, value):
    if type(value) is list:
        print "%s:" % key
        for v in value:
            print " %s" % v
    else:
        print "%s: %s" % (key, value)

def print_element(element):
    for key, value in element.iteritems():
        print_line(key, value)

    print

def parse_url(url):
    scheme, host, path, params, query, fragment = urlparse(url)

    if "@" in host:
        username, host = host.rsplit("@", 1)
        if ":" in username:
            username, password = username.split(":", 1)
        else:
            password = None
    else:
        username = password = None

    if ":" in host:
        host, port = host.split(":")
        assert port.isdigit()
        port = int(port)
    else:
        port = None

    return scheme, username, password, host, port, path, params, query, fragment

def checkout_ltp(location, directory):
    if posixpath.exists(directory):
        return

    target_directory = posixpath.basename(directory)
    next_directory = posixpath.dirname(directory)
    if not posixpath.exists(next_directory):
        os.makedirs(next_directory)

    previous_directory = posixpath.abspath(posixpath.curdir)
    os.chdir(next_directory)

    # Use this to prevent installing into /opt
    os.environ["DESTDIR"] = directory
    os.environ["SKIP_IDCHECK"] = "1"

    for command_template in [
            "cvs -d '%(location)s' login",
            "cvs -z3 -d '%(location)s' co -d %(target_directory)s ltp",
            "make -C '%(target_directory)s' autotools",
            "cd '%(target_directory)s' && ./configure",
            "make -C '%(target_directory)s'",
            "make -C '%(target_directory)s' install"]:
        command = command_template % {
            "location": location,
            "target_directory": target_directory}
        process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
        stdout, stderr = process.communicate()
        if process.returncode:
            raise Exception, "Failed to run command %s" % command

    os.chdir(previous_directory)

def run_ltp(location, directory, timeout=None):
    checkout_ltp(location, directory)

    description_pattern = re.compile(r"#DESCRIPTION:(?P<description>.*)")

    elements = []
    directory = posixpath.join(directory, "opt", "ltp")
    suites_directory = posixpath.join(directory, "runtest")
    for suite_name in os.listdir(suites_directory):
        suite_path = posixpath.join(suites_directory, suite_name)
        if posixpath.isfile(suite_path):
            first_line = open(suite_path, "r").readline()
            match = description_pattern.match(first_line)
            if match:
                description = match.group("description")
                element = {}
                element["plugin"] = "remote"
                element["depends"] = "ltp"
                element["timeout"] = timeout
                element["name"] = suite_name
                element["description"] = match.group("description")
                element["user"] = "root"
                element["command"] = COMMAND_TEMPLATE % {
                    "suite": suite_name,
                    "directory":directory}

                elements.append(element)

    return elements

def main(args):
    usage = "Usage: %prog [OPTIONS]"
    parser = OptionParser(usage=usage)
    parser.add_option("-d", "--directory",
        default=DEFAULT_DIRECTORY,
        help="Directory where to branch ltp")
    parser.add_option("-l", "--location",
        default=DEFAULT_LOCATION,
        help="Location from where to checkout ltp")
    parser.add_option("-t", "--timeout",
        default=DEFAULT_TIMEOUT,
        type="int",
        help="Timeout when running ltp")
    (options, args) = parser.parse_args(args)

    # Check for http_proxy environment variable
    location = options.location
    if "http_proxy" in os.environ:
        host, port = parse_url(os.environ["http_proxy"])[3:5]
        pserver_proxy = "pserver;proxy=%s;proxyport=%s" % (host, port)
        location = location.replace("pserver", pserver_proxy)

    suites = run_ltp(location, options.directory, options.timeout)
    if not suites:
        return 1

    for suite in suites:
        print_element(suite)

    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))