Failed Conditions
Pull Request — master (#1522)
by Abdeali
02:00
created

bears/java/CheckstyleBear.py (1 issue)

Severity
1
from os.path import abspath, dirname, join, isfile
0 ignored issues
show
Unused isfile imported from os.path
Loading history...
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