Completed
Pull Request — master (#2409)
by
unknown
02:06
created

Bear.log_message()   A

Complexity

Conditions 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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_decorators.decorators import (
13
    enforce_signature, classproperty, get_public_members)
14
from coalib.bears.requirements.PackageRequirement import PackageRequirement
0 ignored issues
show
Unused Code introduced by
Unused PackageRequirement imported from coalib.bears.requirements.PackageRequirement
Loading history...
15
from coalib.bears.requirements.PipRequirement import PipRequirement
0 ignored issues
show
Unused Code introduced by
Unused PipRequirement imported from coalib.bears.requirements.PipRequirement
Loading history...
16
from coalib.output.printers.LogPrinter import LogPrinter
17
from coalib.results.Result import Result
18
from coalib.settings.FunctionMetadata import FunctionMetadata
19
from coalib.settings.Section import Section
20
from coalib.settings.ConfigurationGathering import get_config_directory
21
22
23
class Bear(Printer, LogPrinter):
24
    """
25
    A bear contains the actual subroutine that is responsible for checking
26
    source code for certain specifications. However it can actually do
27
    whatever it wants with the files it gets. If you are missing some Result
28
    type, feel free to contact us and/or help us extending the coalib.
29
30
    This is the base class for every bear. If you want to write an bear, you
31
    will probably want to look at the GlobalBear and LocalBear classes that
32
    inherit from this class. In any case you'll want to overwrite at least the
33
    run method. You can send debug/warning/error messages through the
34
    debug(), warn(), err() functions. These will send the
35
    appropriate messages so that they are outputted. Be aware that if you use
36
    err(), you are expected to also terminate the bear run-through
37
    immediately.
38
39
    If you need some setup or teardown for your bear, feel free to overwrite
40
    the set_up() and tear_down() functions. They will be invoked
41
    before/after every run invocation.
42
43
    Settings are available at all times through self.section.
44
45
    To indicate which languages your bear supports, just give it the
46
    ``LANGUAGES`` value which should be a set of string(s):
47
48
    >>> class SomeBear(Bear):
49
    ...     LANGUAGES = {'C', 'CPP','C#', 'D'}
50
51
    To indicate the requirements of the bear, assign ``REQUIREMENTS`` a set
52
    with instances of ``PackageRequirements``.
53
54
    >>> class SomeBear(Bear):
55
    ...     REQUIREMENTS = {
56
    ...         PackageRequirement('pip', 'coala_decorators', '0.2.1')}
57
58
    If your bear uses requirements from a manager we have a subclass from,
59
    you can use the subclass, such as ``PipRequirement``, without specifying
60
    manager:
61
62
    >>> class SomeBear(Bear):
63
    ...     REQUIREMENTS = {PipRequirement('coala_decorators', '0.2.1')}
64
65
    To specify multiple requirements using ``pip``, you can use the multiple
66
    method. This can receive both tuples of strings, in case you want a specific
67
    version, or a simple string, in case you want the latest version to be
68
    specified.
69
70
    >>> class SomeBear(Bear):
71
    ...     REQUIREMENTS = PipRequirement.multiple(
72
    ...         ('colorama', '0.1'), 'coala_decorators')
73
74
    To specify additional attributes to your bear, use the following:
75
76
    >>> class SomeBear(Bear):
77
    ...     AUTHORS = {'Jon Snow'}
78
    ...     AUTHORS_EMAILS = {'[email protected]'}
79
    ...     MAINTAINERS = {'Catelyn Stark'}
80
    ...     MAINTAINERS_EMAILS = {'[email protected]'}
81
    ...     LICENSE = 'AGPL-3.0'
82
83
    If the maintainers are the same as the authors, they can be omitted:
84
85
    >>> class SomeBear(Bear):
86
    ...     AUTHORS = {'Jon Snow'}
87
    ...     AUTHORS_EMAILS = {'[email protected]'}
88
    >>> SomeBear.maintainers
89
    {'Jon Snow'}
90
    >>> SomeBear.maintainers_emails
91
    {'[email protected]'}
92
93
    If your bear needs to include local files, then specify it giving strings
94
    containing relative file paths to the INCLUDE_LOCAL_FILES set:
95
96
    >>> class SomeBear(Bear):
97
    ...     INCLUDE_LOCAL_FILES = {'checkstyle.jar', 'google_checks.xml'}
98
99
    To keep track easier of what a bear can do, simply tell it to the CAN_FIX
100
    and the CAN_DETECT sets. Possible values:
101
102
    >>> CAN_DETECT = {'Syntax', 'Formatting', 'Security', 'Complexity', 'Smell',
103
    ... 'Unused Code', 'Redundancy', 'Variable Misuse', 'Spelling',
104
    ... 'Memory Leak', 'Documentation', 'Duplication', 'Commented Code',
105
    ... 'Grammar', 'Missing Import', 'Unreachable Code', 'Undefined Element',
106
    ... 'Code Simplification'}
107
    >>> CAN_FIX = {'Syntax', ...}
108
109
    Specifying something to CAN_FIX makes it obvious that it can be detected
110
    too, so it may be omitted:
111
112
    >>> class SomeBear(Bear):
113
    ...     CAN_DETECT = {'Syntax', 'Security'}
114
    ...     CAN_FIX = {'Redundancy'}
115
    >>> list(sorted(SomeBear.can_detect))
116
    ['Redundancy', 'Security', 'Syntax']
117
118
    Every bear has a data directory which is unique to that particular bear:
119
120
    >>> class SomeBear(Bear): pass
121
    >>> class SomeOtherBear(Bear): pass
122
    >>> SomeBear.data_dir == SomeOtherBear.data_dir
123
    False
124
    """
125
126
    LANGUAGES = set()
127
    REQUIREMENTS = set()
128
    AUTHORS = set()
129
    AUTHORS_EMAILS = set()
130
    MAINTAINERS = set()
131
    MAINTAINERS_EMAILS = set()
132
    PLATFORMS = {'any'}
133
    LICENSE = ''
134
    INCLUDE_LOCAL_FILES = set()
135
    CAN_DETECT = set()
136
    CAN_FIX = set()
137
138
    @classproperty
139
    def name(cls):
140
        """
141
        :return: The name of the bear
142
        """
143
        return cls.__name__
144
145
    @classproperty
146
    def can_detect(cls):
147
        """
148
        :return: A set that contains everything a bear can detect, gathering
149
                 information from what it can fix too.
150
        """
151
        return cls.CAN_DETECT | cls.CAN_FIX
152
153
    @classproperty
154
    def maintainers(cls):
155
        """
156
        :return: A set containing ``MAINTAINERS`` if specified, else takes
157
                 ``AUTHORS`` by default.
158
        """
159
        return cls.AUTHORS if cls.MAINTAINERS == set() else cls.MAINTAINERS
160
161
    @classproperty
162
    def maintainers_emails(cls):
163
        """
164
        :return: A set containing ``MAINTAINERS_EMAILS`` if specified, else
165
                 takes ``AUTHORS_EMAILS`` by default.
166
        """
167
        return (cls.AUTHORS_EMAILS if cls.MAINTAINERS_EMAILS == set()
168
                else cls.MAINTAINERS)
169
170
    @enforce_signature
171
    def __init__(self,
172
                 section: Section,
173
                 message_queue,
174
                 timeout=0):
175
        """
176
        Constructs a new bear.
177
178
        :param section:       The section object where bear settings are
179
                              contained.
180
        :param message_queue: The queue object for messages. Can be ``None``.
181
        :param timeout:       The time the bear is allowed to run. To set no
182
                              time limit, use 0.
183
        :raises TypeError:    Raised when ``message_queue`` is no queue.
184
        :raises RuntimeError: Raised when bear requirements are not fulfilled.
185
        """
186
        Printer.__init__(self)
187
        LogPrinter.__init__(self, self)
188
189
        if message_queue is not None and not hasattr(message_queue, "put"):
190
            raise TypeError("message_queue has to be a Queue or None.")
191
192
        self.section = section
193
        self.message_queue = message_queue
194
        self.timeout = timeout
195
196
        self.setup_dependencies()
197
        cp = type(self).check_prerequisites()
198
        if cp is not True:
199
            error_string = ("The bear " + self.name +
200
                            " does not fulfill all requirements.")
201
            if cp is not False:
202
                error_string += " " + cp
203
204
            self.warn(error_string)
205
            raise RuntimeError(error_string)
206
207
    def _print(self, output, **kwargs):
208
        self.debug(output)
209
210
    def log_message(self, log_message, timestamp=None, **kwargs):
211
        if self.message_queue is not None:
212
            self.message_queue.put(log_message)
213
214
    def run(self, *args, dependency_results=None, **kwargs):
215
        raise NotImplementedError
216
217
    def run_bear_from_section(self, args, kwargs):
218
        try:
219
            kwargs.update(
220
                self.get_metadata().create_params_from_section(self.section))
221
        except ValueError as err:
222
            self.warn("The bear {} cannot be executed.".format(
223
                self.name), str(err))
224
            return
225
226
        return self.run(*args, **kwargs)
227
228
    def execute(self, *args, **kwargs):
229
        name = self.name
230
        try:
231
            self.debug("Running bear {}...".format(name))
232
            # If it's already a list it won't change it
233
            return list(self.run_bear_from_section(args, kwargs) or [])
234
        except:
235
            self.warn(
236
                "Bear {} failed to run. Take a look at debug messages (`-L "
237
                "DEBUG`) for further information.".format(name))
238
            self.debug(
239
                "The bear {bear} raised an exception. If you are the writer "
240
                "of this bear, please make sure to catch all exceptions. If "
241
                "not and this error annoys you, you might want to get in "
242
                "contact with the writer of this bear.\n\nTraceback "
243
                "information is provided below:\n\n{traceback}"
244
                "\n".format(bear=name, traceback=traceback.format_exc()))
245
246
    @staticmethod
247
    def kind():
248
        """
249
        :return: The kind of the bear
250
        """
251
        raise NotImplementedError
252
253
    @classmethod
254
    def get_metadata(cls):
255
        """
256
        :return: Metadata for the run function. However parameters like
257
                 ``self`` or parameters implicitly used by coala (e.g.
258
                 filename for local bears) are already removed.
259
        """
260
        return FunctionMetadata.from_function(
261
            cls.run,
262
            omit={"self", "dependency_results"})
263
264
    @classmethod
265
    def __json__(cls):
266
        """
267
        Override JSON export of ``Bear`` object.
268
        """
269
        _dict = get_public_members(cls)
270
        # json cannot serialize properties, so drop them
271
        _dict = {key: value for key, value in _dict.items()
272
                 if not isinstance(value, property)}
273
        metadata = cls.get_metadata()
274
        non_optional_params = metadata.non_optional_params
275
        optional_params = metadata.optional_params
276
        _dict["metadata"] = {
277
            "desc": metadata.desc,
278
            "non_optional_params": ({param: non_optional_params[param][0]}
279
                                    for param in non_optional_params),
280
            "optional_params": ({param: optional_params[param][0]}
281
                                for param in optional_params)}
282
283
        # Delete attributes that cannot be serialized
284
        unserializable_attributes = ["new_result", "printer"]
285
        for attribute in unserializable_attributes:
286
            _dict.pop(attribute, None)
287
        return _dict
288
289
    @classmethod
290
    def missing_dependencies(cls, lst):
291
        """
292
        Checks if the given list contains all dependencies.
293
294
        :param lst: A list of all already resolved bear classes (not
295
                    instances).
296
        :return:    A list of missing dependencies.
297
        """
298
        dep_classes = cls.get_dependencies()
299
300
        for item in lst:
301
            if item in dep_classes:
302
                dep_classes.remove(item)
303
304
        return dep_classes
305
306
    @staticmethod
307
    def get_dependencies():
308
        """
309
        Retrieves bear classes that are to be executed before this bear gets
310
        executed. The results of these bears will then be passed to the
311
        run method as a dict via the dependency_results argument. The dict
312
        will have the name of the Bear as key and the list of its results as
313
        results.
314
315
        :return: A list of bear classes.
316
        """
317
        return []
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 his 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