Make sure python scripts run with python 3
Make sure these scripts run well with python 2 and 3.
Change-Id: Ibf175c5122fb41963a6277c5dc3cebfcd60eed43
diff --git a/checkstyle.py b/checkstyle.py
index 97c4e93..e570327 100755
--- a/checkstyle.py
+++ b/checkstyle.py
@@ -80,7 +80,7 @@
Returns:
A tuple of errors and warnings.
"""
- print 'Running Checkstyle on inputted files'
+ print('Running Checkstyle on inputted files')
java_files = map(os.path.abspath, java_files)
stdout = _ExecuteCheckstyle(java_files, classpath, config_xml)
(errors, warnings) = _ParseAndFilterOutput(stdout)
@@ -108,17 +108,17 @@
A tuple of errors and warnings.
"""
if not git.repository_root():
- print 'FAILURE: not inside a git repository'
+ print('FAILURE: not inside a git repository')
sys.exit(1)
explicit_commit = commit is not None
if not explicit_commit:
_WarnIfUntrackedFiles()
commit = git.last_commit()
- print 'Running Checkstyle on %s commit' % commit
+ print('Running Checkstyle on %s commit' % commit)
commit_modified_files = _GetModifiedFiles(commit, explicit_commit)
commit_modified_files = _FilterFiles(commit_modified_files, file_whitelist)
if not commit_modified_files.keys():
- print 'No Java files to check'
+ print('No Java files to check')
return [], []
(tmp_dir, tmp_file_map) = _GetTempFilesForCommit(
@@ -153,11 +153,9 @@
def _PrintErrorsAndWarnings(errors, warnings):
"""Prints given errors and warnings."""
if errors:
- print 'ERRORS:'
- print '\n'.join(errors)
+ print('ERRORS:\n'.join(errors))
if warnings:
- print 'WARNINGS:'
- print '\n'.join(warnings)
+ print('WARNINGS:\n%s'.join(warnings))
def _ExecuteCheckstyle(java_files, classpath, config_xml):
@@ -174,22 +172,22 @@
# Run checkstyle
checkstyle_env = os.environ.copy()
checkstyle_env['JAVA_CMD'] = 'java'
+
try:
check = subprocess.Popen(['java', '-cp', classpath,
'com.puppycrawl.tools.checkstyle.Main', '-c',
config_xml, '-f', 'xml'] + java_files,
stdout=subprocess.PIPE, env=checkstyle_env)
stdout, _ = check.communicate()
+ # A work-around for Checkstyle printing error count to stdio.
+ if 'Checkstyle ends with' in stdout.splitlines()[-1]:
+ stdout = '\n'.join(stdout.splitlines()[:-1])
+ return stdout
except OSError as e:
if e.errno == errno.ENOENT:
- print 'Error running Checkstyle!'
+ print('Error running Checkstyle!')
sys.exit(1)
- # A work-around for Checkstyle printing error count to stdio.
- if 'Checkstyle ends with' in stdout.splitlines()[-1]:
- stdout = '\n'.join(stdout.splitlines()[:-1])
- return stdout
-
def _ParseAndFilterOutput(stdout,
sha=None,
@@ -339,7 +337,7 @@
config_xml = args.config_xml or CHECKSTYLE_STYLE
if not os.path.exists(config_xml):
- print 'Java checkstyle configuration file is missing'
+ print('Java checkstyle configuration file is missing')
sys.exit(1)
classpath = CHECKSTYLE_JAR
@@ -357,7 +355,7 @@
if errors or warnings:
sys.exit(1)
- print 'SUCCESS! NO ISSUES FOUND'
+ print('SUCCESS! NO ISSUES FOUND')
sys.exit(0)
diff --git a/tests.py b/tests.py
index 74b325e..7c9c7e4 100755
--- a/tests.py
+++ b/tests.py
@@ -18,7 +18,11 @@
"""Tests the Checkstyle script used to run style checks on Java files."""
-from StringIO import StringIO
+try:
+ from StringIO import StringIO
+except ImportError:
+ # python3 use io instead of StringIO
+ from io import StringIO
import unittest
import checkstyle