root/releases/pkgcore-checks/0.3.4/pkgcore_checks/report_stream.py @ ferringb%2540gmail.com-20070208002354-i5yevkkrkesm7u2a

Revision ferringb%2540gmail.com-20070208002354-i5yevkkrkesm7u2a, 4.3 KB (checked in by Brian Harring <ferringb@…>, 2 years ago)

remove a print that was screwing up the feed

Line 
1#!/usr/bin/python
2
3"""
4Replay a pickled results stream from pcheck, feeding the results into a
5reporter.
6
7Useful if you need to delay acting on results until it can be done in
8one minimal window (say updating a database), or want to generate
9several different reports without using a config defined multiplex reporter.
10"""
11
12from pkgcore_checks import base
13
14from pkgcore.util import commandline, demandload
15demandload.demandload(globals(),
16    "pkgcore.util:pickling,formatters "
17    "pkgcore.util.modules:load_attribute "
18    "pkgcore_checks:reporters "
19    "os "
20    )
21
22
23class StreamHeader(object):
24
25    def __init__(self, checks, criteria):
26        self.checks = sorted((x for x in checks if x.known_results),
27            key=lambda x:x.__name__)
28        self.known_results = set()
29        for x in checks:
30            self.known_results.update(x.known_results)
31       
32        self.known_results = tuple(sorted(self.known_results))
33        self.criteria = str(criteria)
34       
35
36class PickleStream(base.Reporter):
37    """
38    Generate a stream of pickled objects.
39    For each specific target for checks, a header is pickled
40    detailing the checks used, possible results, and search
41    criteria.
42   
43    """
44    priority = -1001
45    protocol = 0
46   
47    def __init__(self, out):
48        """Initialize.
49
50        @type out: L{pkgcore.util.formatters.Formatter}.
51        """
52        base.Reporter.__init__(self)
53        self.out = out
54        self.dump = pickling.dump
55
56    def start(self):
57        self.out.wrap = False
58        self.out.autoline = False
59
60    def start_check(self, checks, target):
61        self.dump(StreamHeader(checks, target), self.out)
62
63    def add_report(self, result):
64        try:
65            self.dump(result, self.out, self.protocol)
66        except TypeError, t:
67            raise TypeError(result, str(t))
68   
69
70class BinaryPickleStream(PickleStream):
71    """
72    Dump a binary pickle stream (highest protocol).
73    For details of the stream, see PickleStream
74    """
75    priority = -1002
76    protocol = -1
77
78
79class OptionParser(commandline.OptionParser):
80
81    def __init__(self, **kwargs):
82        commandline.OptionParser.__init__(self, description=__doc__, 
83            usage="replay_report_stream <pickle-file> <python namespace path"
84            "reporter to replay it into>",
85            **kwargs)
86        self.add_option("--quiet", default=False, action='store_true',
87            help="disable all status information written to stderr.")
88        self.add_option("--out", default=None,
89            help="redirect reporters output to a file")
90
91    def check_values(self, values, args):
92        vals, args = commandline.OptionParser.check_values(self, values, args)
93       
94        if len(args) < 2:
95            self.error("need at least two args, pickle file, and reporter")
96        elif len(args) > 2:
97            self.error("only two arguements are accepted")
98        args[0] = os.path.abspath(args[0])
99        if not os.path.isfile(args[0]):
100            self.error("pickle file %r doesn't exist" % args[0])
101        values.reporter = load_attribute(args[1])
102        values.stream_path = args[0]
103       
104        return values, []
105
106
107def replay_stream(stream_handle, reporter, debug=None):
108    headers = []
109    last_count = 0
110    for count, item in enumerate(pickling.iter_stream(stream_handle)):
111        if isinstance(item, StreamHeader):
112            if debug:
113                if headers:
114                    debug.write("finished processing %i results for %s" % 
115                        (count - last_count, headers[-1].criteria))
116                last_count = count
117                debug.write("encountered new stream header for %s" %
118                    item.criteria)
119            if headers:
120                reporter.end_check()
121            reporter.start_check(item.checks, item.criteria)
122            headers.append(item)
123            continue
124        reporter.add_report(item)
125    if headers:
126        reporter.end_check()
127        if debug:
128            debug.write("finished processing %i results for %s" % 
129                (count - last_count, headers[-1].criteria))
130
131
132def main(options, out, err):
133    if options.out:
134        out = formatters.get_formatter(open(options.out, 'w'))
135    debug = None
136    if options.debug:
137        debug = err
138    replay_stream(open(options.stream_path), options.reporter(out),
139        debug=debug)
140    return 0
Note: See TracBrowser for help on using the browser.