42
42
ERROR_UNTRACKED = 'You have untracked java files that are not being checked:\n '
43
43
44
44
45
- def RunCheckstyleOnFiles (java_files , config_xml = CHECKSTYLE_STYLE ):
45
+ def RunCheckstyleOnFiles (java_files , classpath = CHECKSTYLE_JAR , config_xml = CHECKSTYLE_STYLE ):
46
46
"""Runs Checkstyle checks on a given set of java_files.
47
47
48
48
Args:
49
49
java_files: A list of files to check.
50
+ classpath: The colon-delimited list of JARs in the classpath.
50
51
config_xml: Path of the checkstyle XML configuration file.
51
52
52
53
Returns:
53
54
A tuple of errors and warnings.
54
55
"""
55
56
print 'Running Checkstyle on inputted files'
56
57
java_files = map (os .path .abspath , java_files )
57
- stdout = _ExecuteCheckstyle (java_files , config_xml )
58
+ stdout = _ExecuteCheckstyle (java_files , classpath , config_xml )
58
59
(errors , warnings ) = _ParseAndFilterOutput (stdout )
59
60
_PrintErrorsAndWarnings (errors , warnings )
60
61
return errors , warnings
61
62
62
63
63
64
def RunCheckstyleOnACommit (commit ,
65
+ classpath = CHECKSTYLE_JAR ,
64
66
config_xml = CHECKSTYLE_STYLE ,
65
67
file_whitelist = None ):
66
68
"""Runs Checkstyle checks on a given commit.
@@ -71,6 +73,7 @@ def RunCheckstyleOnACommit(commit,
71
73
72
74
Args:
73
75
commit: A full 40 character SHA-1 of a commit to check.
76
+ classpath: The colon-delimited list of JARs in the classpath.
74
77
config_xml: Path of the checkstyle XML configuration file.
75
78
file_whitelist: A list of whitelisted file paths that should be checked.
76
79
@@ -92,7 +95,7 @@ def RunCheckstyleOnACommit(commit,
92
95
commit_modified_files .keys (), commit )
93
96
94
97
java_files = tmp_file_map .keys ()
95
- stdout = _ExecuteCheckstyle (java_files , config_xml )
98
+ stdout = _ExecuteCheckstyle (java_files , classpath , config_xml )
96
99
97
100
# Remove all the temporary files.
98
101
shutil .rmtree (tmp_dir )
@@ -127,11 +130,12 @@ def _PrintErrorsAndWarnings(errors, warnings):
127
130
print '\n ' .join (warnings )
128
131
129
132
130
- def _ExecuteCheckstyle (java_files , config_xml ):
133
+ def _ExecuteCheckstyle (java_files , classpath , config_xml ):
131
134
"""Runs Checkstyle to check give Java files for style errors.
132
135
133
136
Args:
134
137
java_files: A list of Java files that needs to be checked.
138
+ classpath: The colon-delimited list of JARs in the classpath.
135
139
config_xml: Path of the checkstyle XML configuration file.
136
140
137
141
Returns:
@@ -141,8 +145,7 @@ def _ExecuteCheckstyle(java_files, config_xml):
141
145
checkstyle_env = os .environ .copy ()
142
146
checkstyle_env ['JAVA_CMD' ] = 'java'
143
147
try :
144
- check = subprocess .Popen (['java' , '-cp' ,
145
- CHECKSTYLE_JAR ,
148
+ check = subprocess .Popen (['java' , '-cp' , classpath ,
146
149
'com.puppycrawl.tools.checkstyle.Main' , '-c' ,
147
150
config_xml , '-f' , 'xml' ] + java_files ,
148
151
stdout = subprocess .PIPE , env = checkstyle_env )
@@ -300,18 +303,25 @@ def main(args=None):
300
303
parser .add_argument ('--sha' , '-s' )
301
304
parser .add_argument ('--config_xml' , '-c' )
302
305
parser .add_argument ('--file_whitelist' , '-fw' , nargs = '+' )
306
+ parser .add_argument ('--add_classpath' , '-p' )
303
307
args = parser .parse_args ()
304
308
305
309
config_xml = args .config_xml or CHECKSTYLE_STYLE
310
+
306
311
if not os .path .exists (config_xml ):
307
312
print 'Java checkstyle configuration file is missing'
308
313
sys .exit (1 )
309
314
315
+ classpath = CHECKSTYLE_JAR
316
+
317
+ if args .add_classpath :
318
+ classpath = args .add_classpath + ':' + classpath
319
+
310
320
if args .file :
311
321
# 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 )
313
323
else :
314
- (errors , warnings ) = RunCheckstyleOnACommit (args .sha , config_xml ,
324
+ (errors , warnings ) = RunCheckstyleOnACommit (args .sha , classpath , config_xml ,
315
325
args .file_whitelist )
316
326
317
327
if errors or warnings :
0 commit comments