-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
94 lines (79 loc) · 3.47 KB
/
Copy pathpre-commit
File metadata and controls
94 lines (79 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /usr/bin/env python
# -*- encoding: utf-8 -*-
import os.path as path
import os
import git
import re
import subprocess
import sys
import functools
# Config
#Repository
repo_root = path.abspath(path.join(__file__ ,"../../..")) + "/"
repo = git.Repo.init(repo_root)
#JAVA Check Style config
checkstyle_jar = repo.config_reader().get_value("checkstyle", "jar")
checkstyle_cfg = repo.config_reader().get_value("checkstyle", "config")
#PMD config
pmd = repo.config_reader().get_value("checkstyle", "pmd")
pmd_cfg = repo.config_reader().get_value("checkstyle", "pmdconfig")
#KOTLIN Check Style config
ktlint = repo.config_reader().get_value("checkstyle", "ktlint")
ktconfig = repo.config_reader().get_value("checkstyle", "ktconfig");
java_cmd = "java"
git_cmd = "git"
print = functools.partial(print, flush=True)
# Init
if not os.path.isfile(checkstyle_jar):
sys.stderr.write("checkstyle JAR not found (%s)\n" % checkstyle_jar)
sys.exit(1)
if not os.path.isfile(checkstyle_cfg):
sys.stderr.write("checkstyle config not found (%s)\n" % checkstyle_cfg)
sys.exit(1)
if "check_output" not in dir( subprocess ):
sys.stderr.write("python >= 2.7 is required\n")
sys.exit(1)
# Fetch files to commit
file_list = subprocess.check_output([git_cmd, "diff", "--cached", "--name-only", "--diff-filter=ACM"])
file_list = [f for f in file_list.splitlines() if f.endswith(b'.java')]
k_file_list = subprocess.check_output([git_cmd, "diff", "--cached", "--name-only", "--diff-filter=ACM"])
k_file_list = [f for f in k_file_list.splitlines() if f.endswith(b'.kt')]
subprocess.check_output([git_cmd, "add", '.'], shell=True)
exclude_config = open(repo_root + 'codecheck-config', 'r')
exclude_files = exclude_config.read().splitlines()
def is_skip_file(file, exclude_files):
for exclude_file in exclude_files:
if file.decode().startswith(exclude_file):
return True
return False
def check(command, file_list, prefix, regexp):
print("\n")
failed_checks = 0
for source_file in file_list:
if is_skip_file(source_file, exclude_files):
continue
try:
subprocess.check_output(' '.join(map(str, command)) % source_file.decode(), shell=True)
print(prefix + source_file.decode() + " OK")
except subprocess.CalledProcessError as err:
failed_checks = failed_checks + 1
print(prefix, source_file.decode(), ' FAILED')
checkRegexp = re.compile(regexp)
messages = [checkRegexp.split(m)[-1] for m in err.output.decode().splitlines() if checkRegexp.search(m) is not None]
for m in messages:
print(" " + m)
return failed_checks
# Run Checkstyle
failed_checkstyle_checks = check([java_cmd, '-jar', checkstyle_jar, '-c', checkstyle_cfg, (repo_root + "%s")], file_list, 'Checkstyle => ', r'\.java:')
failed_pmd_checks = check([pmd, '-d', (repo_root + "%s"), '-f', 'text', '-R', pmd_cfg, '-no-cache'], file_list, 'PMD => ', r'\.java:')
failed_kt_checks = check([java_cmd, "-jar", ktlint, (repo_root + "%s"), "-a", "--editorconfig=" + ktconfig], k_file_list, 'KT => ', r'\.kt:')
total_failed_checks = failed_checkstyle_checks + failed_pmd_checks + failed_kt_checks
print("\n")
# Decisions
if (total_failed_checks) == 0:
print("%d Checkstyle tests passed" % len(file_list))
else:
print("%d Checkstyle tests failed" % failed_checkstyle_checks)
print("%d PMD tests failed" % failed_pmd_checks)
print("%d KT tests failed" % failed_kt_checks)
sys.exit(2)