Completed
Pull Request — master (#1160)
by Mischa
02:07
created

check_prerequisites()   A

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 10
rs 9.4286
1
import shutil
2
from subprocess import Popen, PIPE
3
4
from coalib.bears.LocalBear import LocalBear
5
from coalib.results.Diff import Diff
6
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
7
from coalib.results.Result import Result
8
9
10
class CorrectionBasedBear(LocalBear):
11
    SEVERITY = RESULT_SEVERITY.NORMAL
12
    GET_REPLACEMENT = (lambda self, file, cli_options:
13
                       self.__run_process(file, cli_options))
14
15
    def __run_process(self, file, cli_options):
16
        process = Popen(self.BINARY + ' ' + cli_options,
17
                        shell=True,
18
                        stdin=PIPE,
19
                        stdout=PIPE,
20
                        stderr=PIPE,
21
                        universal_newlines=True)
22
        process.stdin.writelines(file)
23
        process.stdin.close()
24
        process.wait()
25
26
        corrected = process.stdout.readlines()
27
        errors = process.stderr.readlines()
28
29
        process.stdout.close()
30
        process.stderr.close()
31
32
        return corrected, errors
33
34
    @staticmethod
35
    def __yield_diffs(file, new_file):
36
        if new_file != file:
37
            wholediff = Diff.from_string_arrays(file, new_file)
38
39
            for diff in wholediff.split_diff():
40
                yield diff
41
42
    def __print_errors(self, errors):
43
        for line in filter(lambda error: bool(error.strip()), errors):
44
            self.warn(line)
45
46
    def retrieve_results(self, filename, file, **kwargs):
47
        """
48
        Yields results using the self.GET_REPLACEMENT function.
49
50
        :param filename: The filename, just pass it over as you got it!
51
        :param file:     The file, just pass it over as you got it!
52
        :param kwargs:   Any keyword arguments that will be passed to the
53
                         GET_REPLACEMENT function. Please provide cli_options
54
                         if you don't override the default.
55
        """
56
        new_file, errors = self.GET_REPLACEMENT(file=file, **kwargs)
57
        self.__print_errors(errors)
58
59
        for diff in self.__yield_diffs(file, new_file):
60
            yield Result(
61
                self,
62
                self.RESULT_MESSAGE,
63
                affected_code=(diff.range(filename),),
64
                diffs={filename: diff},
65
                severity=self.SEVERITY)
66
67
    @classmethod
68
    def check_prerequisites(cls):
69
        try:
70
            if shutil.which(cls.BINARY) is None:
71
                return repr(cls.BINARY) + " is not installed."
72
            else:
73
                return True
74
        except AttributeError:
75
            # Happens when `BINARY` does not exist in `cls`.
76
            return True
77