aboutsummaryrefslogtreecommitdiff
blob: 95d9e939370f8f7bd0ac9d0e46f011284c2acb7c (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
"""Basic reporters and reporter factories."""

from pkgcore.config import configurable
from snakeoil import formatters
from snakeoil.demandload import demandload

from pkgcheck import base

demandload(
    'collections:defaultdict',
    'json',
    'xml.sax.saxutils:escape@xml_escape',
    'snakeoil:currying,pickling',
    'pkgcheck:errors',
)


class StrReporter(base.Reporter):
    """Simple string reporter, pkgcheck-0.1 behaviour.

    Example::

        sys-apps/portage-2.1-r2: sys-apps/portage-2.1-r2.ebuild has whitespace in indentation on line 169
        sys-apps/portage-2.1-r2: rdepends  ppc-macos: unsolvable default-darwin/macos/10.4, solutions: [ >=app-misc/pax-utils-0.1.13 ]
        sys-apps/portage-2.1-r2: no change in 75 days, keywords [ ~x86-fbsd ]
    """

    # simple reporter; fallback default
    priority = 0

    def __init__(self, *args, **kwargs):
        super(StrReporter, self).__init__(*args, **kwargs)
        self.first_report = True

    def process_report(self, result):
        if self.first_report:
            self.out.write()
            self.first_report = False
        if result.threshold == base.versioned_feed:
            self.out.write(
                "%s/%s-%s: %s" % (result.category, result.package, result.version, result.desc))
        elif result.threshold == base.package_feed:
            self.out.write(
                "%s/%s: %s" % (result.category, result.package, result.desc))
        elif result.threshold == base.category_feed:
            self.out.write("%s: %s" % (result.category, result.desc))
        else:
            self.out.write(result.desc)

    def finish(self):
        if not self.first_report:
            self.out.write()


class FancyReporter(base.Reporter):
    """grouped colored output

    Example::

        sys-apps/portage
          WrongIndentFound: sys-apps/portage-2.1-r2.ebuild has whitespace in indentation on line 169
          NonsolvableDeps: sys-apps/portage-2.1-r2: rdepends  ppc-macos: unsolvable default-darwin/macos/10.4, solutions: [ >=app-misc/pax-utils-0.1.13 ]
          StaleUnstableKeyword: sys-apps/portage-2.1-r2: no change in 75 days, keywords [ ~x86-fbsd ]
    """

    # default report, akin to repoman
    priority = 1

    def __init__(self, *args, **kwargs):
        super(FancyReporter, self).__init__(*args, **kwargs)
        self.key = None

    def process_report(self, result):
        if result.threshold in (base.versioned_feed, base.package_feed):
            key = '%s/%s' % (result.category, result.package)
        elif result.threshold == base.category_feed:
            key = result.category
        else:
            key = 'unknown'

        if key != self.key:
            self.out.write()
            self.out.write(self.out.bold, key)
            self.key = key
        self.out.first_prefix.append('  ')
        self.out.later_prefix.append('    ')
        s = ''
        if result.threshold == base.versioned_feed:
            s = "version %s: " % result.version
        self.out.write(
            self.out.fg(result.color),
            result.__class__.__name__, self.out.reset,
            ': ', s, result.desc)
        self.out.first_prefix.pop()
        self.out.later_prefix.pop()


class NullReporter(base.Reporter):
    """reporter used for timing tests; no output"""

    priority = -10000000

    def __init__(self, *args, **kwargs):
        pass

    def process_report(self, result):
        pass


class JsonReporter(base.Reporter):
    """Dump a json feed of reports.

    Note that the format is newline-delimited JSON with each line being related
    to a separate report. To merge the objects together something like jq can
    be leveraged similar to the following:

        jq -c -s 'reduce.[]as$x({};.*$x)' orig.json > new.json
    """

    # json report should only be used if requested
    priority = -1000

    def __init__(self, *args, **kwargs):
        super(JsonReporter, self).__init__(*args, **kwargs)
        self._json_dict = lambda: defaultdict(self._json_dict)

    def process_report(self, result):
        data = self._json_dict()

        if result.threshold == base.repository_feed:
            d = data
        elif result.threshold == base.category_feed:
            d = data[result.category]
        elif result.threshold == base.package_feed:
            d = data[result.category][result.package]
        else:
            # versioned or ebuild feed
            d = data[result.category][result.package][result.version]

        name = result.__class__.__name__
        d['_' + result.level][name] = [result.desc]

        self.out.write(json.dumps(data))
        # flush output so partial objects aren't written
        self.out.stream.flush()


class XmlReporter(base.Reporter):
    """dump an xml feed of reports"""

    # xml report, shouldn't be used but in worst case.
    priority = -1000

    repo_template = (
        "<result><class>%(class)s</class>"
        "<msg>%(msg)s</msg></result>")
    cat_template = (
        "<result><category>%(category)s</category>"
        "<class>%(class)s</class><msg>%(msg)s</msg></result>")
    pkg_template = (
        "<result><category>%(category)s</category>"
        "<package>%(package)s</package><class>%(class)s</class>"
        "<msg>%(msg)s</msg></result>")
    ver_template = (
        "<result><category>%(category)s</category>"
        "<package>%(package)s</package><version>%(version)s</version>"
        "<class>%(class)s</class><msg>%(msg)s</msg></result>")

    threshold_map = {
        base.repository_feed: repo_template,
        base.category_feed: cat_template,
        base.package_feed: pkg_template,
        base.versioned_feed: ver_template,
        base.ebuild_feed: ver_template,
    }

    def __init__(self, *args, **kwargs):
        super(XmlReporter, self).__init__(*args, **kwargs)

    def start(self):
        self.out.write('<checks>')

    def process_report(self, result):
        d = dict((k, getattr(result, k, '')) for k in
                 ("category", "package", "version"))
        d["class"] = xml_escape(result.__class__.__name__)
        d["msg"] = xml_escape(result.desc)
        self.out.write(self.threshold_map[result.threshold] % d)

    def finish(self):
        self.out.write('</checks>')


class StreamHeader(object):

    def __init__(self, checks, criteria):
        self.checks = sorted((x for x in checks if x.known_results),
                             key=lambda x: x.__name__)
        self.known_results = set()
        for x in checks:
            self.known_results.update(x.known_results)

        self.known_results = tuple(sorted(self.known_results))
        self.criteria = str(criteria)


class PickleStream(base.Reporter):
    """Generate a stream of pickled objects.

    For each specific target for checks, a header is pickled
    detailing the checks used, possible results, and search
    criteria.
    """
    priority = -1001
    protocol = 0

    def __init__(self, *args, **kwargs):
        """Initialize.

        :type out: L{snakeoil.formatters.Formatter}.
        """
        super(PickleStream, self).__init__(*args, **kwargs)
        self.dump = pickling.dump

    def start(self):
        self.out.wrap = False
        self.out.autoline = False

    def start_check(self, checks, target):
        self.dump(StreamHeader(checks, target), self.out)

    def process_report(self, result):
        try:
            self.dump(result, self.out, self.protocol)
        except TypeError as t:
            raise TypeError(result, str(t))


class BinaryPickleStream(PickleStream):
    """Dump a binary pickle stream (highest protocol).

    For details of the stream, see PickleStream.
    """
    priority = -1002
    protocol = -1


class MultiplexReporter(base.Reporter):

    def __init__(self, reporters, *args, **kwargs):
        if len(reporters) < 2:
            raise ValueError("need at least two reporters")
        super(MultiplexReporter, self).__init__(*args, **kwargs)
        self.reporters = tuple(reporters)

    def start(self):
        for x in self.reporters:
            x.start()

    def process_report(self, result):
        for x in self.reporters:
            x.process_report(result)

    def finish(self):
        for x in self.reporters:
            x.finish()


def make_configurable_reporter_factory(klass):
    @configurable({'dest': 'str'}, typename='pkgcheck_reporter_factory')
    def configurable_reporter_factory(dest=None):
        if dest is None:
            return klass

        def reporter_factory(out):
            try:
                f = open(dest, 'w')
            except EnvironmentError as e:
                raise errors.ReporterInitError(
                    'Cannot write to %r (%s)' % (dest, e))
            return klass(formatters.PlainTextFormatter(f))

        return reporter_factory
    return configurable_reporter_factory


json_reporter = make_configurable_reporter_factory(JsonReporter)
json_reporter.__name__ = 'json_reporter'
xml_reporter = make_configurable_reporter_factory(XmlReporter)
xml_reporter.__name__ = 'xml_reporter'
plain_reporter = make_configurable_reporter_factory(StrReporter)
plain_reporter.__name__ = 'plain_reporter'
fancy_reporter = make_configurable_reporter_factory(FancyReporter)
fancy_reporter.__name__ = 'fancy_reporter'
picklestream_reporter = make_configurable_reporter_factory(PickleStream)
picklestream_reporter.__name__ = 'picklestream_reporter'
binarypicklestream_reporter = make_configurable_reporter_factory(BinaryPickleStream)
binarypicklestream_reporter.__name__ = 'binarypicklestream_reporter'
null_reporter = make_configurable_reporter_factory(NullReporter)
null_reporter.__name__ = 'null'


@configurable({'reporters': 'refs:pkgcheck_reporter_factory'},
              typename='pkgcheck_reporter_factory')
def multiplex_reporter(reporters):
    def make_multiplex_reporter(out):
        return MultiplexReporter([factory(out) for factory in reporters])
    return make_multiplex_reporter