2
# This file is part of Checkbox.
4
# Copyright 2008 Canonical Ltd.
6
# Checkbox is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# Checkbox is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
21
from StringIO import StringIO
23
from checkbox.lib.path import path_expand_recursive
24
from checkbox.lib.template_i18n import TemplateI18n
26
from checkbox.job import Job, PASS
27
from checkbox.plugin import Plugin
28
from checkbox.arguments import coerce_arguments
29
from checkbox.properties import Float, Int, List, Map, String, Unicode
32
message_schema = Map({
35
"status": String(required=False),
36
"suite": String(required=False),
37
"description": Unicode(required=False),
38
"command": String(required=False),
39
"depends": List(String(), required=False),
40
"duration": Float(required=False),
41
"environ": List(String(), required=False),
42
"requires": List(String(), separator=r"\n", required=False),
43
"timeout": Int(required=False),
44
"user": String(required=False)})
47
class MessageInfo(Plugin):
49
def register(self, manager):
50
super(MessageInfo, self).register(manager)
53
("message", self.message),
54
("messages", self.messages),
55
("message-command", self.message_command),
56
("message-directory", self.message_directory),
57
("message-exec", self.message_exec),
58
("message-file", self.message_file),
59
("message-filename", self.message_filename),
60
("message-job", self.message_job),
61
("message-string", self.message_string)]:
62
self._manager.reactor.call_on(rt, rh)
64
@coerce_arguments(message=message_schema)
65
def message(self, message):
66
self._manager.reactor.fire("report-%s" % message["plugin"], message)
68
def messages(self, messages):
69
for message in messages:
70
self._manager.reactor.fire("message", message)
72
def message_command(self, command, environ=[], timeout=None):
73
job = Job(command, environ, timeout)
74
self._manager.reactor.fire("message-job", job)
76
def message_directory(self, directory, blacklist=[], whitelist=[]):
77
whitelist_patterns = [re.compile(r"^%s$" % r) for r in whitelist if r]
78
blacklist_patterns = [re.compile(r"^%s$" % r) for r in blacklist if r]
81
for filename in path_expand_recursive(directory):
82
name = posixpath.basename(filename)
83
if name.startswith(".") or name.endswith("~"):
86
if whitelist_patterns:
87
if not [name for p in whitelist_patterns if p.match(name)]:
89
elif blacklist_patterns:
90
if [name for p in blacklist_patterns if p.match(name)]:
93
self._manager.reactor.fire("message-filename", filename)
95
def message_exec(self, message):
96
self._manager.reactor.fire("message-command", message["command"],
97
message.get("environ"), message.get("timeout"))
99
def message_file(self, file, filename="<stream>"):
100
template = TemplateI18n()
101
messages = template.load_file(file, filename)
102
for message in messages:
103
long_ext = "_extended"
104
for long_key in message.keys():
105
if long_key.endswith(long_ext):
106
short_key = long_key.replace(long_ext, "")
107
message[short_key] = message.pop(long_key)
109
self._manager.reactor.fire("messages", messages)
111
def message_filename(self, filename):
112
file = open(filename, "r")
113
self._manager.reactor.fire("message-file", file, filename)
115
def message_job(self, job):
117
if job.status == PASS:
118
self._manager.reactor.fire("message-string", job.data)
120
def message_string(self, string):
121
file = StringIO(string)
122
self._manager.reactor.fire("message-file", file)
125
factory = MessageInfo