|
1
|
|
|
import traceback |
|
2
|
|
|
from functools import partial |
|
3
|
|
|
from os import makedirs |
|
4
|
|
|
from os.path import join, abspath, exists |
|
5
|
|
|
from shutil import copyfileobj |
|
6
|
|
|
from urllib.request import urlopen |
|
7
|
|
|
|
|
8
|
|
|
from appdirs import user_data_dir |
|
9
|
|
|
|
|
10
|
|
|
from pyprint.Printer import Printer |
|
11
|
|
|
|
|
12
|
|
|
from coala_utils.decorators import (enforce_signature, classproperty, |
|
13
|
|
|
get_public_members) |
|
14
|
|
|
|
|
15
|
|
|
from coalib.bears.requirements.PackageRequirement import PackageRequirement |
|
|
|
|
|
|
16
|
|
|
from coalib.bears.requirements.PipRequirement import PipRequirement |
|
|
|
|
|
|
17
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
|
18
|
|
|
from coalib.results.Result import Result |
|
19
|
|
|
from coalib.settings.FunctionMetadata import FunctionMetadata |
|
20
|
|
|
from coalib.settings.Section import Section |
|
21
|
|
|
from coalib.settings.ConfigurationGathering import get_config_directory |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class Bear(Printer, LogPrinter): |
|
25
|
|
|
""" |
|
26
|
|
|
A bear contains the actual subroutine that is responsible for checking |
|
27
|
|
|
source code for certain specifications. However it can actually do |
|
28
|
|
|
whatever it wants with the files it gets. If you are missing some Result |
|
29
|
|
|
type, feel free to contact us and/or help us extending the coalib. |
|
30
|
|
|
|
|
31
|
|
|
This is the base class for every bear. If you want to write an bear, you |
|
32
|
|
|
will probably want to look at the GlobalBear and LocalBear classes that |
|
33
|
|
|
inherit from this class. In any case you'll want to overwrite at least the |
|
34
|
|
|
run method. You can send debug/warning/error messages through the |
|
35
|
|
|
debug(), warn(), err() functions. These will send the |
|
36
|
|
|
appropriate messages so that they are outputted. Be aware that if you use |
|
37
|
|
|
err(), you are expected to also terminate the bear run-through |
|
38
|
|
|
immediately. |
|
39
|
|
|
|
|
40
|
|
|
If you need some setup or teardown for your bear, feel free to overwrite |
|
41
|
|
|
the set_up() and tear_down() functions. They will be invoked |
|
42
|
|
|
before/after every run invocation. |
|
43
|
|
|
|
|
44
|
|
|
Settings are available at all times through self.section. |
|
45
|
|
|
|
|
46
|
|
|
To indicate which languages your bear supports, just give it the |
|
47
|
|
|
``LANGUAGES`` value which should be a set of string(s): |
|
48
|
|
|
|
|
49
|
|
|
>>> class SomeBear(Bear): |
|
50
|
|
|
... LANGUAGES = {'C', 'CPP','C#', 'D'} |
|
51
|
|
|
|
|
52
|
|
|
To indicate the requirements of the bear, assign ``REQUIREMENTS`` a set |
|
53
|
|
|
with instances of ``PackageRequirements``. |
|
54
|
|
|
|
|
55
|
|
|
>>> class SomeBear(Bear): |
|
56
|
|
|
... REQUIREMENTS = { |
|
57
|
|
|
... PackageRequirement('pip', 'coala_decorators', '0.2.1')} |
|
58
|
|
|
|
|
59
|
|
|
If your bear uses requirements from a manager we have a subclass from, |
|
60
|
|
|
you can use the subclass, such as ``PipRequirement``, without specifying |
|
61
|
|
|
manager: |
|
62
|
|
|
|
|
63
|
|
|
>>> class SomeBear(Bear): |
|
64
|
|
|
... REQUIREMENTS = {PipRequirement('coala_decorators', '0.2.1')} |
|
65
|
|
|
|
|
66
|
|
|
To specify multiple requirements using ``pip``, you can use the multiple |
|
67
|
|
|
method. This can receive both tuples of strings, in case you want a specific |
|
68
|
|
|
version, or a simple string, in case you want the latest version to be |
|
69
|
|
|
specified. |
|
70
|
|
|
|
|
71
|
|
|
>>> class SomeBear(Bear): |
|
72
|
|
|
... REQUIREMENTS = PipRequirement.multiple( |
|
73
|
|
|
... ('colorama', '0.1'), 'coala_decorators') |
|
74
|
|
|
|
|
75
|
|
|
To specify additional attributes to your bear, use the following: |
|
76
|
|
|
|
|
77
|
|
|
>>> class SomeBear(Bear): |
|
78
|
|
|
... AUTHORS = {'Jon Snow'} |
|
79
|
|
|
... AUTHORS_EMAILS = {'[email protected]'} |
|
80
|
|
|
... MAINTAINERS = {'Catelyn Stark'} |
|
81
|
|
|
... MAINTAINERS_EMAILS = {'[email protected]'} |
|
82
|
|
|
... LICENSE = 'AGPL-3.0' |
|
83
|
|
|
... ASCIINEMA_URL = 'https://asciinema.org/a/80761' |
|
84
|
|
|
|
|
85
|
|
|
If the maintainers are the same as the authors, they can be omitted: |
|
86
|
|
|
|
|
87
|
|
|
>>> class SomeBear(Bear): |
|
88
|
|
|
... AUTHORS = {'Jon Snow'} |
|
89
|
|
|
... AUTHORS_EMAILS = {'[email protected]'} |
|
90
|
|
|
>>> SomeBear.maintainers |
|
91
|
|
|
{'Jon Snow'} |
|
92
|
|
|
>>> SomeBear.maintainers_emails |
|
93
|
|
|
{'[email protected]'} |
|
94
|
|
|
|
|
95
|
|
|
If your bear needs to include local files, then specify it giving strings |
|
96
|
|
|
containing relative file paths to the INCLUDE_LOCAL_FILES set: |
|
97
|
|
|
|
|
98
|
|
|
>>> class SomeBear(Bear): |
|
99
|
|
|
... INCLUDE_LOCAL_FILES = {'checkstyle.jar', 'google_checks.xml'} |
|
100
|
|
|
|
|
101
|
|
|
To keep track easier of what a bear can do, simply tell it to the CAN_FIX |
|
102
|
|
|
and the CAN_DETECT sets. Possible values: |
|
103
|
|
|
|
|
104
|
|
|
>>> CAN_DETECT = {'Syntax', 'Formatting', 'Security', 'Complexity', 'Smell', |
|
105
|
|
|
... 'Unused Code', 'Redundancy', 'Variable Misuse', 'Spelling', |
|
106
|
|
|
... 'Memory Leak', 'Documentation', 'Duplication', 'Commented Code', |
|
107
|
|
|
... 'Grammar', 'Missing Import', 'Unreachable Code', 'Undefined Element', |
|
108
|
|
|
... 'Code Simplification'} |
|
109
|
|
|
>>> CAN_FIX = {'Syntax', ...} |
|
110
|
|
|
|
|
111
|
|
|
Specifying something to CAN_FIX makes it obvious that it can be detected |
|
112
|
|
|
too, so it may be omitted: |
|
113
|
|
|
|
|
114
|
|
|
>>> class SomeBear(Bear): |
|
115
|
|
|
... CAN_DETECT = {'Syntax', 'Security'} |
|
116
|
|
|
... CAN_FIX = {'Redundancy'} |
|
117
|
|
|
>>> list(sorted(SomeBear.can_detect)) |
|
118
|
|
|
['Redundancy', 'Security', 'Syntax'] |
|
119
|
|
|
|
|
120
|
|
|
Every bear has a data directory which is unique to that particular bear: |
|
121
|
|
|
|
|
122
|
|
|
>>> class SomeBear(Bear): pass |
|
123
|
|
|
>>> class SomeOtherBear(Bear): pass |
|
124
|
|
|
>>> SomeBear.data_dir == SomeOtherBear.data_dir |
|
125
|
|
|
False |
|
126
|
|
|
|
|
127
|
|
|
BEAR_DEPS contains bear classes that are to be executed before this bear |
|
128
|
|
|
gets executed. The results of these bears will then be passed to the |
|
129
|
|
|
run method as a dict via the dependency_results argument. The dict |
|
130
|
|
|
will have the name of the Bear as key and the list of its results as |
|
131
|
|
|
results: |
|
132
|
|
|
|
|
133
|
|
|
>>> class SomeBear(Bear): |
|
134
|
|
|
... BEAR_DEPS = ["AnotherBear"] |
|
135
|
|
|
>>> SomeBear.BEAR_DEPS |
|
136
|
|
|
['AnotherBear'] |
|
137
|
|
|
""" |
|
138
|
|
|
|
|
139
|
|
|
LANGUAGES = set() |
|
140
|
|
|
REQUIREMENTS = set() |
|
141
|
|
|
AUTHORS = set() |
|
142
|
|
|
AUTHORS_EMAILS = set() |
|
143
|
|
|
MAINTAINERS = set() |
|
144
|
|
|
MAINTAINERS_EMAILS = set() |
|
145
|
|
|
PLATFORMS = {'any'} |
|
146
|
|
|
LICENSE = '' |
|
147
|
|
|
INCLUDE_LOCAL_FILES = set() |
|
148
|
|
|
CAN_DETECT = set() |
|
149
|
|
|
CAN_FIX = set() |
|
150
|
|
|
ASCIINEMA_URL = '' |
|
151
|
|
|
BEAR_DEPS = [] |
|
152
|
|
|
|
|
153
|
|
|
@classproperty |
|
154
|
|
|
def name(cls): |
|
155
|
|
|
""" |
|
156
|
|
|
:return: The name of the bear |
|
157
|
|
|
""" |
|
158
|
|
|
return cls.__name__ |
|
159
|
|
|
|
|
160
|
|
|
@classproperty |
|
161
|
|
|
def can_detect(cls): |
|
162
|
|
|
""" |
|
163
|
|
|
:return: A set that contains everything a bear can detect, gathering |
|
164
|
|
|
information from what it can fix too. |
|
165
|
|
|
""" |
|
166
|
|
|
return cls.CAN_DETECT | cls.CAN_FIX |
|
167
|
|
|
|
|
168
|
|
|
@classproperty |
|
169
|
|
|
def maintainers(cls): |
|
170
|
|
|
""" |
|
171
|
|
|
:return: A set containing ``MAINTAINERS`` if specified, else takes |
|
172
|
|
|
``AUTHORS`` by default. |
|
173
|
|
|
""" |
|
174
|
|
|
return cls.AUTHORS if cls.MAINTAINERS == set() else cls.MAINTAINERS |
|
175
|
|
|
|
|
176
|
|
|
@classproperty |
|
177
|
|
|
def maintainers_emails(cls): |
|
178
|
|
|
""" |
|
179
|
|
|
:return: A set containing ``MAINTAINERS_EMAILS`` if specified, else |
|
180
|
|
|
takes ``AUTHORS_EMAILS`` by default. |
|
181
|
|
|
""" |
|
182
|
|
|
return (cls.AUTHORS_EMAILS if cls.MAINTAINERS_EMAILS == set() |
|
183
|
|
|
else cls.MAINTAINERS) |
|
184
|
|
|
|
|
185
|
|
|
@enforce_signature |
|
186
|
|
|
def __init__(self, |
|
187
|
|
|
section: Section, |
|
188
|
|
|
message_queue, |
|
189
|
|
|
timeout=0): |
|
190
|
|
|
""" |
|
191
|
|
|
Constructs a new bear. |
|
192
|
|
|
|
|
193
|
|
|
:param section: The section object where bear settings are |
|
194
|
|
|
contained. |
|
195
|
|
|
:param message_queue: The queue object for messages. Can be ``None``. |
|
196
|
|
|
:param timeout: The time the bear is allowed to run. To set no |
|
197
|
|
|
time limit, use 0. |
|
198
|
|
|
:raises TypeError: Raised when ``message_queue`` is no queue. |
|
199
|
|
|
:raises RuntimeError: Raised when bear requirements are not fulfilled. |
|
200
|
|
|
""" |
|
201
|
|
|
Printer.__init__(self) |
|
202
|
|
|
LogPrinter.__init__(self, self) |
|
203
|
|
|
|
|
204
|
|
|
if message_queue is not None and not hasattr(message_queue, "put"): |
|
205
|
|
|
raise TypeError("message_queue has to be a Queue or None.") |
|
206
|
|
|
|
|
207
|
|
|
self.section = section |
|
208
|
|
|
self.message_queue = message_queue |
|
209
|
|
|
self.timeout = timeout |
|
210
|
|
|
|
|
211
|
|
|
self.setup_dependencies() |
|
212
|
|
|
cp = type(self).check_prerequisites() |
|
213
|
|
|
if cp is not True: |
|
214
|
|
|
error_string = ("The bear " + self.name + |
|
215
|
|
|
" does not fulfill all requirements.") |
|
216
|
|
|
if cp is not False: |
|
217
|
|
|
error_string += " " + cp |
|
218
|
|
|
|
|
219
|
|
|
self.warn(error_string) |
|
220
|
|
|
raise RuntimeError(error_string) |
|
221
|
|
|
|
|
222
|
|
|
def _print(self, output, **kwargs): |
|
223
|
|
|
self.debug(output) |
|
224
|
|
|
|
|
225
|
|
|
def log_message(self, log_message, timestamp=None, **kwargs): |
|
226
|
|
|
if self.message_queue is not None: |
|
227
|
|
|
self.message_queue.put(log_message) |
|
228
|
|
|
|
|
229
|
|
|
def run(self, *args, dependency_results=None, **kwargs): |
|
230
|
|
|
raise NotImplementedError |
|
231
|
|
|
|
|
232
|
|
|
def run_bear_from_section(self, args, kwargs): |
|
233
|
|
|
try: |
|
234
|
|
|
kwargs.update( |
|
235
|
|
|
self.get_metadata().create_params_from_section(self.section)) |
|
236
|
|
|
except ValueError as err: |
|
237
|
|
|
self.warn("The bear {} cannot be executed.".format( |
|
238
|
|
|
self.name), str(err)) |
|
239
|
|
|
return |
|
240
|
|
|
|
|
241
|
|
|
return self.run(*args, **kwargs) |
|
242
|
|
|
|
|
243
|
|
|
def execute(self, *args, **kwargs): |
|
244
|
|
|
name = self.name |
|
245
|
|
|
try: |
|
246
|
|
|
self.debug("Running bear {}...".format(name)) |
|
247
|
|
|
# If it's already a list it won't change it |
|
248
|
|
|
result = self.run_bear_from_section(args, kwargs) |
|
249
|
|
|
return [] if result is None else list(result) |
|
250
|
|
|
except: |
|
251
|
|
|
self.warn( |
|
252
|
|
|
"Bear {} failed to run. Take a look at debug messages (`-L " |
|
253
|
|
|
"DEBUG`) for further information.".format(name)) |
|
254
|
|
|
self.debug( |
|
255
|
|
|
"The bear {bear} raised an exception. If you are the writer " |
|
256
|
|
|
"of this bear, please make sure to catch all exceptions. If " |
|
257
|
|
|
"not and this error annoys you, you might want to get in " |
|
258
|
|
|
"contact with the writer of this bear.\n\nTraceback " |
|
259
|
|
|
"information is provided below:\n\n{traceback}" |
|
260
|
|
|
"\n".format(bear=name, traceback=traceback.format_exc())) |
|
261
|
|
|
|
|
262
|
|
|
@staticmethod |
|
263
|
|
|
def kind(): |
|
264
|
|
|
""" |
|
265
|
|
|
:return: The kind of the bear |
|
266
|
|
|
""" |
|
267
|
|
|
raise NotImplementedError |
|
268
|
|
|
|
|
269
|
|
|
@classmethod |
|
270
|
|
|
def get_metadata(cls): |
|
271
|
|
|
""" |
|
272
|
|
|
:return: Metadata for the run function. However parameters like |
|
273
|
|
|
``self`` or parameters implicitly used by coala (e.g. |
|
274
|
|
|
filename for local bears) are already removed. |
|
275
|
|
|
""" |
|
276
|
|
|
return FunctionMetadata.from_function( |
|
277
|
|
|
cls.run, |
|
278
|
|
|
omit={"self", "dependency_results"}) |
|
279
|
|
|
|
|
280
|
|
|
@classmethod |
|
281
|
|
|
def __json__(cls): |
|
282
|
|
|
""" |
|
283
|
|
|
Override JSON export of ``Bear`` object. |
|
284
|
|
|
""" |
|
285
|
|
|
_dict = get_public_members(cls) |
|
286
|
|
|
metadata = cls.get_metadata() |
|
287
|
|
|
non_optional_params = metadata.non_optional_params |
|
288
|
|
|
optional_params = metadata.optional_params |
|
289
|
|
|
_dict["metadata"] = { |
|
290
|
|
|
"desc": metadata.desc, |
|
291
|
|
|
"non_optional_params": ({param: non_optional_params[param][0]} |
|
292
|
|
|
for param in non_optional_params), |
|
293
|
|
|
"optional_params": ({param: optional_params[param][0]} |
|
294
|
|
|
for param in optional_params)} |
|
295
|
|
|
|
|
296
|
|
|
# Delete attributes that cannot be serialized |
|
297
|
|
|
unserializable_attributes = ["new_result", "printer"] |
|
298
|
|
|
for attribute in unserializable_attributes: |
|
299
|
|
|
_dict.pop(attribute, None) |
|
300
|
|
|
return _dict |
|
301
|
|
|
|
|
302
|
|
|
@classmethod |
|
303
|
|
|
def missing_dependencies(cls, lst): |
|
304
|
|
|
""" |
|
305
|
|
|
Checks if the given list contains all dependencies. |
|
306
|
|
|
|
|
307
|
|
|
:param lst: A list of all already resolved bear classes (not |
|
308
|
|
|
instances). |
|
309
|
|
|
:return: A list of missing dependencies. |
|
310
|
|
|
""" |
|
311
|
|
|
dep_classes = cls.BEAR_DEPS |
|
312
|
|
|
|
|
313
|
|
|
for item in lst: |
|
314
|
|
|
if item in dep_classes: |
|
315
|
|
|
dep_classes.remove(item) |
|
316
|
|
|
|
|
317
|
|
|
return dep_classes |
|
318
|
|
|
|
|
319
|
|
|
@classmethod |
|
320
|
|
|
def get_non_optional_settings(cls): |
|
321
|
|
|
""" |
|
322
|
|
|
This method has to determine which settings are needed by this bear. |
|
323
|
|
|
The user will be prompted for needed settings that are not available |
|
324
|
|
|
in the settings file so don't include settings where a default value |
|
325
|
|
|
would do. |
|
326
|
|
|
|
|
327
|
|
|
:return: A dictionary of needed settings as keys and a tuple of help |
|
328
|
|
|
text and annotation as values |
|
329
|
|
|
""" |
|
330
|
|
|
return cls.get_metadata().non_optional_params |
|
331
|
|
|
|
|
332
|
|
|
@staticmethod |
|
333
|
|
|
def setup_dependencies(): |
|
334
|
|
|
""" |
|
335
|
|
|
This is a user defined function that can download and set up |
|
336
|
|
|
dependencies (via download_cached_file or arbitary other means) in an OS |
|
337
|
|
|
independent way. |
|
338
|
|
|
""" |
|
339
|
|
|
|
|
340
|
|
|
@classmethod |
|
341
|
|
|
def check_prerequisites(cls): |
|
342
|
|
|
""" |
|
343
|
|
|
Checks whether needed runtime prerequisites of the bear are satisfied. |
|
344
|
|
|
|
|
345
|
|
|
This function gets executed at construction and returns True by |
|
346
|
|
|
default. |
|
347
|
|
|
|
|
348
|
|
|
Section value requirements shall be checked inside the ``run`` method. |
|
349
|
|
|
|
|
350
|
|
|
:return: True if prerequisites are satisfied, else False or a string |
|
351
|
|
|
that serves a more detailed description of what's missing. |
|
352
|
|
|
""" |
|
353
|
|
|
return True |
|
354
|
|
|
|
|
355
|
|
|
def get_config_dir(self): |
|
356
|
|
|
""" |
|
357
|
|
|
Gives the directory where the configuration file is |
|
358
|
|
|
|
|
359
|
|
|
:return: Directory of the config file |
|
360
|
|
|
""" |
|
361
|
|
|
return get_config_directory(self.section) |
|
362
|
|
|
|
|
363
|
|
|
def download_cached_file(self, url, filename): |
|
364
|
|
|
""" |
|
365
|
|
|
Downloads the file if needed and caches it for the next time. If a |
|
366
|
|
|
download happens, the user will be informed. |
|
367
|
|
|
|
|
368
|
|
|
Take a sane simple bear: |
|
369
|
|
|
|
|
370
|
|
|
>>> from queue import Queue |
|
371
|
|
|
>>> bear = Bear(Section("a section"), Queue()) |
|
372
|
|
|
|
|
373
|
|
|
We can now carelessly query for a neat file that doesn't exist yet: |
|
374
|
|
|
|
|
375
|
|
|
>>> from os import remove |
|
376
|
|
|
>>> if exists(join(bear.data_dir, "a_file")): |
|
377
|
|
|
... remove(join(bear.data_dir, "a_file")) |
|
378
|
|
|
>>> file = bear.download_cached_file("http://gitmate.com/", "a_file") |
|
379
|
|
|
|
|
380
|
|
|
If we download it again, it'll be much faster as no download occurs: |
|
381
|
|
|
|
|
382
|
|
|
>>> newfile = bear.download_cached_file("http://gitmate.com/", "a_file") |
|
383
|
|
|
>>> newfile == file |
|
384
|
|
|
True |
|
385
|
|
|
|
|
386
|
|
|
:param url: The URL to download the file from. |
|
387
|
|
|
:param filename: The filename it should get, e.g. "test.txt". |
|
388
|
|
|
:return: A full path to the file ready for you to use! |
|
389
|
|
|
""" |
|
390
|
|
|
filename = join(self.data_dir, filename) |
|
391
|
|
|
if exists(filename): |
|
392
|
|
|
return filename |
|
393
|
|
|
|
|
394
|
|
|
self.info("Downloading {filename!r} for bear {bearname} from {url}." |
|
395
|
|
|
.format(filename=filename, bearname=self.name, url=url)) |
|
396
|
|
|
|
|
397
|
|
|
with urlopen(url) as response, open(filename, 'wb') as out_file: |
|
398
|
|
|
copyfileobj(response, out_file) |
|
399
|
|
|
return filename |
|
400
|
|
|
|
|
401
|
|
|
@classproperty |
|
402
|
|
|
def data_dir(cls): |
|
403
|
|
|
""" |
|
404
|
|
|
Returns a directory that may be used by the bear to store stuff. Every |
|
405
|
|
|
bear has an own directory dependent on their name. |
|
406
|
|
|
""" |
|
407
|
|
|
data_dir = abspath(join(user_data_dir('coala-bears'), cls.name)) |
|
408
|
|
|
|
|
409
|
|
|
makedirs(data_dir, exist_ok=True) |
|
410
|
|
|
return data_dir |
|
411
|
|
|
|
|
412
|
|
|
@property |
|
413
|
|
|
def new_result(self): |
|
414
|
|
|
""" |
|
415
|
|
|
Returns a partial for creating a result with this bear already bound. |
|
416
|
|
|
""" |
|
417
|
|
|
return partial(Result.from_values, self) |
|
418
|
|
|
|