~checkbox-dev/checkbox-legacy/trunk

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
#
# This file is part of Checkbox.
#
# Copyright 2008 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.

#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
#
import os
import re
import logging
import posixpath
import signal

from io import StringIO

from checkbox.lib.path import path_expand_recursive
from checkbox.lib.template_i18n import TemplateI18n

from checkbox.parsers.description import DescriptionParser

from checkbox.job import Job, PASS, FAIL
from checkbox.plugin import Plugin

from gettext import gettext as _


class DescriptionResult:

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

    def setDescription(self, purpose, steps, verification, info):
        self.message["purpose"] = purpose
        self.message["steps"] = steps
        self.message["verification"] = verification
        self.message["info"] = info


class MessageInfo(Plugin):

    def register(self, manager):
        super(MessageInfo, self).register(manager)

        for (rt, rh) in [
             ("report-messages", self.report_messages),
             ("message-directory", self.message_directory),
             ("message-exec", self.message_exec),
             ("message-file", self.message_file),
             ("message-filename", self.message_filename),
             ("message-result", self.message_result)]:
            self._manager.reactor.call_on(rt, rh)

    def report_messages(self, messages):
        for message in messages:
            self._manager.reactor.fire("report-message", message)

    def message_directory(self, directory, blacklist=[], whitelist=[]):
        whitelist_patterns = [re.compile(r"^%s$" % r) for r in whitelist if r]
        blacklist_patterns = [re.compile(r"^%s$" % r) for r in blacklist if r]

        for filename in path_expand_recursive(directory):
            name = posixpath.basename(filename)
            if name.startswith(".") or name.endswith("~"):
                continue

            if whitelist_patterns:
                if not [name for p in whitelist_patterns if p.match(name)]:
                    continue
            elif blacklist_patterns:
                if [name for p in blacklist_patterns if p.match(name)]:
                    continue

            self._manager.reactor.fire("message-filename", filename)

    def message_exec(self, message):
        if "name" in message:
            logging.info(_("Executing %(test_name)s" % {
                "test_name": message["name"]}))

        if "user" not in message:

            def stop():
                os.kill(0, signal.SIGTERM)

            job = Job(message["command"], message.get("environ"),
                message.get("timeout"))

            # Kill the job if the stop event is fired during execution
            event_id = self._manager.reactor.call_on("stop", stop)
            status, data, duration = job.execute()
            self._manager.reactor.cancel_call(event_id)

            try:
                data = data.decode("utf-8")
            except UnicodeDecodeError:
                status, data, duration = (FAIL, "Decode error", 0,)

            self._manager.reactor.fire(
                "message-result", status, data, duration)

    def message_file(self, file, filename="<stream>"):
        template = TemplateI18n()
        messages = template.load_file(file, filename)
        for message in messages:
            long_ext = "_extended"
            for long_key in list(message.keys()):
                if long_key.endswith(long_ext):
                    short_key = long_key.replace(long_ext, "")
                    message[short_key] = message.pop(long_key)
            if "description" in message:
                parser = DescriptionParser(StringIO(message["description"]))
                result = DescriptionResult(message)
                parser.run(result)

        if messages:
            self._manager.reactor.fire("report-messages", messages)

    def message_filename(self, filename):
        file = open(filename, "r", encoding="utf-8")
        try:
            self._manager.reactor.fire("message-file", file, filename)
        finally:
            file.close()

    def message_result(self, status, data, duration):
        if status == PASS:
            file = StringIO(data)
            self._manager.reactor.fire("message-file", file)


factory = MessageInfo