Skip to content

Commit 50fb0a9

Browse files
committed
Allow passing custom checks to checkstyle
Bug: 32998581 Change-Id: Icf349a8ff47ebc434958b872f5729b126e67bcbb
1 parent da07dcd commit 50fb0a9

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tools/checkstyle/checkstyle.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,27 @@
4242
ERROR_UNTRACKED = 'You have untracked java files that are not being checked:\n'
4343

4444

45-
def RunCheckstyleOnFiles(java_files, config_xml=CHECKSTYLE_STYLE):
45+
def RunCheckstyleOnFiles(java_files, classpath=CHECKSTYLE_JAR, config_xml=CHECKSTYLE_STYLE):
4646
"""Runs Checkstyle checks on a given set of java_files.
4747
4848
Args:
4949
java_files: A list of files to check.
50+
classpath: The colon-delimited list of JARs in the classpath.
5051
config_xml: Path of the checkstyle XML configuration file.
5152
5253
Returns:
5354
A tuple of errors and warnings.
5455
"""
5556
print 'Running Checkstyle on inputted files'
5657
java_files = map(os.path.abspath, java_files)
57-
stdout = _ExecuteCheckstyle(java_files, config_xml)
58+
stdout = _ExecuteCheckstyle(java_files, classpath, config_xml)
5859
(errors, warnings) = _ParseAndFilterOutput(stdout)
5960
_PrintErrorsAndWarnings(errors, warnings)
6061
return errors, warnings
6162

6263

6364
def RunCheckstyleOnACommit(commit,
65+
classpath=CHECKSTYLE_JAR,
6466
config_xml=CHECKSTYLE_STYLE,
6567
file_whitelist=None):
6668
"""Runs Checkstyle checks on a given commit.
@@ -71,6 +73,7 @@ def RunCheckstyleOnACommit(commit,
7173
7274
Args:
7375
commit: A full 40 character SHA-1 of a commit to check.
76+
classpath: The colon-delimited list of JARs in the classpath.
7477
config_xml: Path of the checkstyle XML configuration file.
7578
file_whitelist: A list of whitelisted file paths that should be checked.
7679
@@ -92,7 +95,7 @@ def RunCheckstyleOnACommit(commit,
9295
commit_modified_files.keys(), commit)
9396

9497
java_files = tmp_file_map.keys()
95-
stdout = _ExecuteCheckstyle(java_files, config_xml)
98+
stdout = _ExecuteCheckstyle(java_files, classpath, config_xml)
9699

97100
# Remove all the temporary files.
98101
shutil.rmtree(tmp_dir)
@@ -127,11 +130,12 @@ def _PrintErrorsAndWarnings(errors, warnings):
127130
print '\n'.join(warnings)
128131

129132

130-
def _ExecuteCheckstyle(java_files, config_xml):
133+
def _ExecuteCheckstyle(java_files, classpath, config_xml):
131134
"""Runs Checkstyle to check give Java files for style errors.
132135
133136
Args:
134137
java_files: A list of Java files that needs to be checked.
138+
classpath: The colon-delimited list of JARs in the classpath.
135139
config_xml: Path of the checkstyle XML configuration file.
136140
137141
Returns:
@@ -141,8 +145,7 @@ def _ExecuteCheckstyle(java_files, config_xml):
141145
checkstyle_env = os.environ.copy()
142146
checkstyle_env['JAVA_CMD'] = 'java'
143147
try:
144-
check = subprocess.Popen(['java', '-cp',
145-
CHECKSTYLE_JAR,
148+
check = subprocess.Popen(['java', '-cp', classpath,
146149
'com.puppycrawl.tools.checkstyle.Main', '-c',
147150
config_xml, '-f', 'xml'] + java_files,
148151
stdout=subprocess.PIPE, env=checkstyle_env)
@@ -300,18 +303,25 @@ def main(args=None):
300303
parser.add_argument('--sha', '-s')
301304
parser.add_argument('--config_xml', '-c')
302305
parser.add_argument('--file_whitelist', '-fw', nargs='+')
306+
parser.add_argument('--add_classpath', '-p')
303307
args = parser.parse_args()
304308

305309
config_xml = args.config_xml or CHECKSTYLE_STYLE
310+
306311
if not os.path.exists(config_xml):
307312
print 'Java checkstyle configuration file is missing'
308313
sys.exit(1)
309314

315+
classpath = CHECKSTYLE_JAR
316+
317+
if args.add_classpath:
318+
classpath = args.add_classpath + ':' + classpath
319+
310320
if args.file:
311321
# Files to check were specified via command line.
312-
(errors, warnings) = RunCheckstyleOnFiles(args.file, config_xml)
322+
(errors, warnings) = RunCheckstyleOnFiles(args.file, classpath, config_xml)
313323
else:
314-
(errors, warnings) = RunCheckstyleOnACommit(args.sha, config_xml,
324+
(errors, warnings) = RunCheckstyleOnACommit(args.sha, classpath, config_xml,
315325
args.file_whitelist)
316326

317327
if errors or warnings:

0 commit comments

Comments
 (0)