~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/xunit/protocols/checkbox.py

  • Committer: Marc Tardif
  • Date: 2012-03-21 22:32:04 UTC
  • Revision ID: marc.tardif@canonical.com-20120321223204-8g7mvzzwmh8ifbrt
Added support for getting systems from a person (LP #899361)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
except ImportError:
13
13
    import cElementTree as etree
14
14
 
 
15
import re
 
16
 
15
17
from StringIO import StringIO
16
18
from logging import getLogger
17
19
from pkg_resources import resource_string
20
22
 
21
23
from lpresults import xunit
22
24
from lpresults.xunit.dispatcher import DispatcherQueue
 
25
from lpresults.xunit.parsers.ansi import AnsiParser
23
26
from lpresults.xunit.parsers.cpuinfo import CpuinfoParser
24
27
from lpresults.xunit.parsers.cputable import CputableParser
25
28
from lpresults.xunit.parsers.deferred import DeferredParser
29
32
from lpresults.xunit.parsers.udevadm import UdevadmParser
30
33
from lpresults.xunit.protocol import Protocol
31
34
from lpresults.xunit.status import Status
 
35
from lpresults.xunit.validators import INVALID_XML_RE
32
36
 
33
37
 
34
38
CHECKBOX_HEADER = """<?xml version="1.0" ?>"""
35
39
 
36
40
 
 
41
# Validator functions for common invalid patterns.
 
42
INVALID_TO_VALID = {
 
43
    ur"<comment>.*?</comment>":
 
44
        lambda text: str(AnsiParser(StringIO(text))),
 
45
    ur"<property .*?</property>":
 
46
        lambda text: INVALID_XML_RE.sub("", text),
 
47
    ur"\xFF{8,}":
 
48
        lambda text: "",
 
49
    }
 
50
 
 
51
INVALID_PATTERN = "|".join(["(%s)" % text for text in INVALID_TO_VALID.keys()])
 
52
INVALID_RE = re.compile(INVALID_PATTERN, re.M | re.S)
 
53
 
 
54
VALID_FUNCTIONS = INVALID_TO_VALID.values()
 
55
 
 
56
 
37
57
class CheckboxResult:
38
58
 
39
59
    def __init__(self, test_run_factory, **kwargs):
87
107
            }
88
108
        parser = parsers.get(command)
89
109
        if parser:
90
 
            if not isinstance(text, unicode):
91
 
                text = text.decode("utf-8")
92
 
            stream = StringIO(text)
 
110
            stream = StringIO(unicode(text))
93
111
            p = parser(stream)
94
112
            p.run(self)
95
113
 
147
165
            "yes": Status.PASS,
148
166
            }
149
167
 
 
168
        # Required arguments
150
169
        test_result = dict(
151
170
            name=question["name"],
152
 
            output=question["comment"],
153
171
            status=answer_to_status[question["answer"]["value"]],
154
172
            )
 
173
 
 
174
        # Optional arguments
 
175
        if "comment" in question:
 
176
            test_result["output"] = question["comment"]
 
177
 
155
178
        test_result.update(self.test_run_kwargs)
156
179
        self.dispatcher.publishEvent("test_result", test_result)
157
180
 
224
247
 
225
248
        self.logger = getLogger()
226
249
 
 
250
    def _getSubmission(self):
 
251
        def repl(match):
 
252
            for index, group in enumerate(match.groups()):
 
253
                if group is not None:
 
254
                    return VALID_FUNCTIONS[index](group)
 
255
 
 
256
        return INVALID_RE.sub(repl, self.file.read())
 
257
 
227
258
    def _getClient(self, node):
228
259
        """Return a dictionary with the name and version of the client."""
229
260
        return {
279
310
 
280
311
    def _getValueAsDatetime(self, node):
281
312
        """Return the value of the attribute "value" as a datetime."""
282
 
        string = node.attrib["value"]
283
 
        return string_to_datetime(string)
 
313
        return string_to_datetime(node.attrib["value"])
284
314
 
285
315
    def _getValueAsString(self, node):
286
316
        """Return the value of the attribute "value"."""
287
 
        return node.attrib["value"].decode("utf-8")
 
317
        return unicode(node.attrib["value"])
288
318
 
289
319
    def parseContext(self, result, node):
290
320
        """Parse the <context> part of a submission."""
318
348
                if child.getchildren():
319
349
                    parser(result, child)
320
350
                else:
321
 
                    text = child.text
322
 
                    if not isinstance(text, unicode):
323
 
                        text = text.decode("utf-8")
324
 
                    stream = StringIO(text)
 
351
                    stream = StringIO(unicode(child.text))
325
352
                    p = parser(stream)
326
353
                    p.run(result)
327
354
            else:
378
405
            assert child.tag == "question", \
379
406
                "Unexpected tag <%s>, expected <question>" % child.tag
380
407
            question = {
381
 
                "name": child.get("name").decode("utf-8"),
 
408
                "name": unicode(child.get("name")),
382
409
                "targets": [],
383
410
                }
384
411
            plugin = child.get("plugin", None)
416
443
                    text = sub_node.text
417
444
                    if text is None:
418
445
                        text = u""
419
 
                    elif not isinstance(text, unicode):
420
 
                        text = text.decode("utf-8")
421
 
                    question[sub_tag] = text.strip()
 
446
                    else:
 
447
                        text = unicode(text.strip())
 
448
                    question[sub_tag] = text
422
449
 
423
450
                else:
424
451
                    raise AssertionError(
485
512
                    "Unsupported tag <%s> in <system>" % child.tag)
486
513
 
487
514
    def run(self, test_run_factory, **kwargs):
 
515
        submission = self._getSubmission()
488
516
        parser = etree.XMLParser()
489
 
 
490
 
        tree = etree.parse(self.file, parser=parser)
 
517
        tree = etree.parse(StringIO(submission), parser=parser)
491
518
        root = tree.getroot()
492
519
        if root.tag != "system":
493
520
            raise AssertionError(