Merge "Allow passing custom checks to checkstyle" am: 32abb71122
am: d5f8802f6c
Change-Id: I4808fba588df2790379c5aab88e75675bae55db9
This commit is contained in:
@@ -42,11 +42,12 @@ ERROR_UNCOMMITTED = 'You need to commit all modified files before running Checks
|
||||
ERROR_UNTRACKED = 'You have untracked java files that are not being checked:\n'
|
||||
|
||||
|
||||
def RunCheckstyleOnFiles(java_files, config_xml=CHECKSTYLE_STYLE):
|
||||
def RunCheckstyleOnFiles(java_files, classpath=CHECKSTYLE_JAR, config_xml=CHECKSTYLE_STYLE):
|
||||
"""Runs Checkstyle checks on a given set of java_files.
|
||||
|
||||
Args:
|
||||
java_files: A list of files to check.
|
||||
classpath: The colon-delimited list of JARs in the classpath.
|
||||
config_xml: Path of the checkstyle XML configuration file.
|
||||
|
||||
Returns:
|
||||
@@ -54,13 +55,14 @@ def RunCheckstyleOnFiles(java_files, config_xml=CHECKSTYLE_STYLE):
|
||||
"""
|
||||
print 'Running Checkstyle on inputted files'
|
||||
java_files = map(os.path.abspath, java_files)
|
||||
stdout = _ExecuteCheckstyle(java_files, config_xml)
|
||||
stdout = _ExecuteCheckstyle(java_files, classpath, config_xml)
|
||||
(errors, warnings) = _ParseAndFilterOutput(stdout)
|
||||
_PrintErrorsAndWarnings(errors, warnings)
|
||||
return errors, warnings
|
||||
|
||||
|
||||
def RunCheckstyleOnACommit(commit,
|
||||
classpath=CHECKSTYLE_JAR,
|
||||
config_xml=CHECKSTYLE_STYLE,
|
||||
file_whitelist=None):
|
||||
"""Runs Checkstyle checks on a given commit.
|
||||
@@ -71,6 +73,7 @@ def RunCheckstyleOnACommit(commit,
|
||||
|
||||
Args:
|
||||
commit: A full 40 character SHA-1 of a commit to check.
|
||||
classpath: The colon-delimited list of JARs in the classpath.
|
||||
config_xml: Path of the checkstyle XML configuration file.
|
||||
file_whitelist: A list of whitelisted file paths that should be checked.
|
||||
|
||||
@@ -92,7 +95,7 @@ def RunCheckstyleOnACommit(commit,
|
||||
commit_modified_files.keys(), commit)
|
||||
|
||||
java_files = tmp_file_map.keys()
|
||||
stdout = _ExecuteCheckstyle(java_files, config_xml)
|
||||
stdout = _ExecuteCheckstyle(java_files, classpath, config_xml)
|
||||
|
||||
# Remove all the temporary files.
|
||||
shutil.rmtree(tmp_dir)
|
||||
@@ -127,11 +130,12 @@ def _PrintErrorsAndWarnings(errors, warnings):
|
||||
print '\n'.join(warnings)
|
||||
|
||||
|
||||
def _ExecuteCheckstyle(java_files, config_xml):
|
||||
def _ExecuteCheckstyle(java_files, classpath, config_xml):
|
||||
"""Runs Checkstyle to check give Java files for style errors.
|
||||
|
||||
Args:
|
||||
java_files: A list of Java files that needs to be checked.
|
||||
classpath: The colon-delimited list of JARs in the classpath.
|
||||
config_xml: Path of the checkstyle XML configuration file.
|
||||
|
||||
Returns:
|
||||
@@ -141,8 +145,7 @@ def _ExecuteCheckstyle(java_files, config_xml):
|
||||
checkstyle_env = os.environ.copy()
|
||||
checkstyle_env['JAVA_CMD'] = 'java'
|
||||
try:
|
||||
check = subprocess.Popen(['java', '-cp',
|
||||
CHECKSTYLE_JAR,
|
||||
check = subprocess.Popen(['java', '-cp', classpath,
|
||||
'com.puppycrawl.tools.checkstyle.Main', '-c',
|
||||
config_xml, '-f', 'xml'] + java_files,
|
||||
stdout=subprocess.PIPE, env=checkstyle_env)
|
||||
@@ -300,18 +303,25 @@ def main(args=None):
|
||||
parser.add_argument('--sha', '-s')
|
||||
parser.add_argument('--config_xml', '-c')
|
||||
parser.add_argument('--file_whitelist', '-fw', nargs='+')
|
||||
parser.add_argument('--add_classpath', '-p')
|
||||
args = parser.parse_args()
|
||||
|
||||
config_xml = args.config_xml or CHECKSTYLE_STYLE
|
||||
|
||||
if not os.path.exists(config_xml):
|
||||
print 'Java checkstyle configuration file is missing'
|
||||
sys.exit(1)
|
||||
|
||||
classpath = CHECKSTYLE_JAR
|
||||
|
||||
if args.add_classpath:
|
||||
classpath = args.add_classpath + ':' + classpath
|
||||
|
||||
if args.file:
|
||||
# Files to check were specified via command line.
|
||||
(errors, warnings) = RunCheckstyleOnFiles(args.file, config_xml)
|
||||
(errors, warnings) = RunCheckstyleOnFiles(args.file, classpath, config_xml)
|
||||
else:
|
||||
(errors, warnings) = RunCheckstyleOnACommit(args.sha, config_xml,
|
||||
(errors, warnings) = RunCheckstyleOnACommit(args.sha, classpath, config_xml,
|
||||
args.file_whitelist)
|
||||
|
||||
if errors or warnings:
|
||||
|
||||
Reference in New Issue
Block a user