Completed
Pull Request — master (#1522)
by Abdeali
02:39
created

bears.java.CheckstyleBear.check_prerequisites()   A

Complexity

Conditions 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 12
rs 9.2
1
from os.path import abspath, dirname, join, isfile
2
import re
3
import shutil
4
import subprocess
5
6
from coalib.bearlib.abstractions.Lint import Lint
7
from coalib.bears.LocalBear import LocalBear
8
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
9
10
11
class CheckstyleBear(LocalBear, Lint):
12
    executable = 'java'
13
    google_checks = join(dirname(abspath(__file__)), 'google_checks.xml')
14
    jar = join(dirname(abspath(__file__)), 'checkstyle.jar')
15
16
    severity_map = {
17
        "INFO": RESULT_SEVERITY.INFO,
18
        "WARN": RESULT_SEVERITY.NORMAL}
19
20
    @classmethod
21
    def check_prerequisites(cls):
22
        if shutil.which("java") is None:
23
            return "java is not installed."
24
        else:
25
            exitcode = subprocess.call(["java", "-jar", cls.jar, "-v"])
26
            if exitcode == 1:
27
                return "jar file {} was not found".format(cls.jar)
28
            elif not isfile(cls.google_checks):
29
                return "checks file {} was not found".format(cls.google_checks)
30
            else:
31
                return True
32
33
    def run(self, filename, file):
34
        """
35
        Checks the code using `checkstyle` using the Google codestyle
36
        specification.
37
        """
38
        self.output_regex = re.compile(
39
            r'\[(?P<severity>WARN|INFO)\]\s*' + re.escape(abspath(filename)) +
40
            r':(?P<line>\d+)(:(?P<col>\d+))?:\s*'
41
            r'(?P<message>.*?)\s*\[(?P<origin>[a-zA-Z]+?)\]')
42
        self.arguments = '-jar ' + self.jar + ' -c ' + self.google_checks
43
        return self.lint(filename)
44