summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2018-12-18 22:42:30 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2018-12-19 14:11:16 +0200
commit3c8886d6981e1238657cc6ee9dcf267f090fca41 (patch)
tree05cde0954294e329e13d0bcc4ec14be744f0dd9a /utils
parentcf04a496a6e70d180446b519a7f90618b36b4479 (diff)
utils: checkstyle.py: Strip trailing white spaces
As astyle doesn't strip trailing white spaces, strip them manually. Organize the code to allow for new additional formatting steps if needed. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'utils')
-rwxr-xr-xutils/checkstyle.py49
1 files changed, 43 insertions, 6 deletions
diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index dadb55df..32974815 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -38,6 +38,10 @@ source_extensions = (
'.h'
)
+# ------------------------------------------------------------------------------
+# Colour terminal handling
+#
+
class Colours:
Default = 0
Black = 0
@@ -79,6 +83,10 @@ class Colours:
return ''
+# ------------------------------------------------------------------------------
+# Diff parsing, handling and printing
+#
+
class DiffHunkSide(object):
"""A side of a diff hunk, recording line numbers"""
def __init__(self, start):
@@ -179,6 +187,33 @@ def parse_diff(diff):
return hunks
+# ------------------------------------------------------------------------------
+# Code reformatting
+#
+
+def formatter_astyle(data):
+ ret = subprocess.run(['astyle', *astyle_options],
+ input=data.encode('utf-8'), stdout=subprocess.PIPE)
+ return ret.stdout.decode('utf-8')
+
+
+def formatter_strip_trailing_space(data):
+ lines = data.split('\n')
+ for i in range(len(lines)):
+ lines[i] = lines[i].rstrip() + '\n'
+ return ''.join(lines)
+
+
+formatters = [
+ formatter_astyle,
+ formatter_strip_trailing_space,
+]
+
+
+# ------------------------------------------------------------------------------
+# Style checking
+#
+
def check_file(top_level, commit, filename):
# Extract the line numbers touched by the commit.
diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',
@@ -195,16 +230,18 @@ def check_file(top_level, commit, filename):
if len(lines) == 0:
return 0
- # Format the file after the commit with astyle and compute the diff between
- # the two files.
+ # Format the file after the commit with all formatters and compute the diff
+ # between the unformatted and formatted contents.
after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)],
stdout=subprocess.PIPE).stdout
- formatted = subprocess.run(['astyle', *astyle_options],
- input=after, stdout=subprocess.PIPE).stdout
+ after = after.decode('utf-8')
- after = after.decode('utf-8').splitlines(True)
- formatted = formatted.decode('utf-8').splitlines(True)
+ formatted = after
+ for formatter in formatters:
+ formatted = formatter(formatted)
+ after = after.splitlines(True)
+ formatted = formatted.splitlines(True)
diff = difflib.unified_diff(after, formatted)
# Split the diff in hunks, recording line number ranges for each hunk.