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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
"""Basic result reporters."""
import csv
import json
from collections import defaultdict
from string import Formatter
from xml.sax.saxutils import escape as xml_escape
from snakeoil.decorators import coroutine
from . import base
from .results import BaseLinesResult, InvalidResult, Result
class Reporter:
"""Generic result reporter."""
def __init__(self, out):
"""Initialize
:type out: L{snakeoil.formatters.Formatter}
"""
self.out = out
# initialize result processing coroutines
self.report = self._process_report().send
def __enter__(self):
self._start()
return self
def __exit__(self, *excinfo):
self._finish()
# flush output buffer
self.out.stream.flush()
@coroutine
def _process_report(self):
"""Render and output a report result.."""
raise NotImplementedError(self._process_report)
def _start(self):
"""Initialize reporter output."""
def _finish(self):
"""Finalize reporter output."""
class StrReporter(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: rdepend 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 ]
"""
priority = 0
@coroutine
def _process_report(self):
# scope to result prefix mapping
scope_prefix_map = {
base.version_scope: "{category}/{package}-{version}: ",
base.package_scope: "{category}/{package}: ",
base.category_scope: "{category}: ",
}
while True:
result = yield
prefix = scope_prefix_map.get(result.scope, "").format(**vars(result))
self.out.write(f"{prefix}{result.desc}")
self.out.stream.flush()
class FancyReporter(Reporter):
"""Colored output grouped by result scope.
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: rdepend ppc-macos: unsolvable default-darwin/macos/10.4, solutions: [ >=app-misc/pax-utils-0.1.13 ]
StableRequest: sys-apps/portage-2.1-r2: no change in 75 days, keywords [ ~x86 ]
"""
priority = 1
@coroutine
def _process_report(self):
prev_key = None
while True:
result = yield
if result.scope in (base.version_scope, base.package_scope):
key = f"{result.category}/{result.package}"
elif result.scope == base.category_scope:
key = result.category
else:
key = result.scope.desc
if key != prev_key:
if prev_key is not None:
self.out.write()
self.out.write(self.out.bold, self.out.fg("blue"), key, self.out.reset)
prev_key = key
self.out.first_prefix.append(" ")
self.out.later_prefix.append(" ")
s = ""
if result.scope == base.version_scope:
s = f"version {result.version}: "
self.out.write(
self.out.fg(result.color), result.name, self.out.reset, ": ", s, result.desc
)
self.out.first_prefix.pop()
self.out.later_prefix.pop()
self.out.stream.flush()
class JsonReporter(Reporter):
"""Feed of newline-delimited JSON records.
Note that the format is newline-delimited JSON with each line being related
to a separate report. To merge the objects together a tool such as jq can
be leveraged similar to the following:
.. code::
jq -c -s 'reduce.[]as$x({};.*$x)' orig.json > new.json
"""
priority = -1000
@coroutine
def _process_report(self):
# arbitrarily nested defaultdicts
json_dict = lambda: defaultdict(json_dict)
# scope to data conversion mapping
scope_map = {
base.version_scope: lambda data, r: data[r.category][r.package][r.version],
base.package_scope: lambda data, r: data[r.category][r.package],
base.category_scope: lambda data, r: data[r.category],
}
while True:
result = yield
data = json_dict()
d = scope_map.get(result.scope, lambda x, y: x)(data, result)
d["_" + result.level][result.name] = result.desc
self.out.write(json.dumps(data))
# flush output so partial objects aren't written
self.out.stream.flush()
class XmlReporter(Reporter):
"""Feed of newline-delimited XML reports."""
priority = -1000
def _start(self):
self.out.write("<checks>")
def _finish(self):
self.out.write("</checks>")
@coroutine
def _process_report(self):
result_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>"
)
scope_map = {
base.category_scope: cat_template,
base.package_scope: pkg_template,
base.version_scope: ver_template,
}
while True:
result = yield
d = {k: getattr(result, k, "") for k in ("category", "package", "version")}
d["class"] = xml_escape(result.name)
d["msg"] = xml_escape(result.desc)
self.out.write(scope_map.get(result.scope, result_template) % d)
class CsvReporter(Reporter):
"""Comma-separated value reporter, convenient for shell processing.
Example::
,,,"global USE flag 'big-endian' is a potential local, used by 1 package: dev-java/icedtea-bin"
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,"rdepend 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 ]"
"""
priority = -1001
@coroutine
def _process_report(self):
writer = csv.writer(self.out, doublequote=False, escapechar="\\", lineterminator="")
while True:
result = yield
writer.writerow(
(
getattr(result, "category", ""),
getattr(result, "package", ""),
getattr(result, "version", ""),
result.desc,
)
)
class _ResultFormatter(Formatter):
"""Custom string formatter that collapses unmatched variables."""
def get_value(self, key, args, kwds):
"""Retrieve a given field value, an empty string is returned for unmatched fields."""
if isinstance(key, str):
try:
return kwds[key]
except KeyError:
return ""
raise base.PkgcheckUserException("FormatReporter: integer indexes are not supported")
class FormatReporter(Reporter):
"""Custom format string reporter.
This formatter uses custom format string passed using the ``--format``
command line argument."""
priority = -1001
def __init__(self, format_str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.format_str = format_str
@coroutine
def _process_report(self):
formatter = _ResultFormatter()
# provide expansions for result desc, level, and output name properties
properties = ("desc", "level", "name")
while True:
result = yield
attrs = vars(result)
attrs.update((k, getattr(result, k)) for k in properties)
s = formatter.format(self.format_str, **attrs)
# output strings with at least one valid expansion or non-whitespace character
if s.strip():
self.out.write(s)
self.out.stream.flush()
class DeserializationError(Exception):
"""Exception occurred while deserializing a data stream."""
class JsonStream(Reporter):
"""Generate a stream of result objects serialized in JSON."""
priority = -1001
@staticmethod
def to_json(obj):
"""Serialize results and other objects to JSON."""
if isinstance(obj, Result):
d = {"__class__": obj.__class__.__name__}
d.update(obj._attrs)
return d
return str(obj)
@staticmethod
def from_iter(iterable):
"""Deserialize results from a given iterable."""
# avoid circular import issues
from . import objects
try:
for data in map(json.loads, iterable):
cls = objects.KEYWORDS[data.pop("__class__")]
yield cls._create(**data)
except (json.decoder.JSONDecodeError, UnicodeDecodeError, DeserializationError) as e:
raise DeserializationError("failed loading") from e
except (KeyError, InvalidResult):
raise DeserializationError("unknown result")
@coroutine
def _process_report(self):
while True:
result = yield
self.out.write(json.dumps(result, default=self.to_json))
class FlycheckReporter(Reporter):
"""Simple line reporter done for easier integration with flycheck [#]_ .
.. [#] https://github.com/flycheck/flycheck
"""
priority = -1001
@coroutine
def _process_report(self):
while True:
result = yield
file = f'{getattr(result, "package", "")}-{getattr(result, "version", "")}.ebuild'
message = f'{getattr(result, "name")}: {getattr(result, "desc")}'
if isinstance(result, BaseLinesResult):
message = message.replace(result.lines_str, "").strip()
for lineno in result.lines:
self.out.write(f'{file}:{lineno}:{getattr(result, "level")}:{message}')
else:
lineno = getattr(result, "lineno", 0)
self.out.write(f'{file}:{lineno}:{getattr(result, "level")}:{message}')
|