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

bears.java.CheckstyleBear.check_prerequisites()   A

Complexity

Conditions 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 11
rs 9.4285
1
from os.path import abspath, dirname, join
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):  # pragma: no cover
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 != 0:
27
                return ("jar file {} is invalid and cannot be used"
28
                        .format(cls.jar))
29
            else:
30
                return True
31
32
    def run(self, filename, file):
33
        """
34
        Checks the code using `checkstyle` using the Google codestyle
35
        specification.
36
        """
37
        self.output_regex = re.compile(
38
            r'\[(?P<severity>WARN|INFO)\]\s*' + re.escape(abspath(filename)) +
39
            r':(?P<line>\d+)(:(?P<col>\d+))?:\s*'
40
            r'(?P<message>.*?)\s*\[(?P<origin>[a-zA-Z]+?)\]')
41
        self.arguments = '-jar ' + self.jar + ' -c ' + self.google_checks
42
        self.arguments += " {filename}"
43
        return self.lint(filename)
44