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>
This commit is contained in:
Laurent Pinchart 2018-12-18 22:42:30 +02:00
parent cf04a496a6
commit 3c8886d698

View file

@ -38,6 +38,10 @@ source_extensions = (
'.h' '.h'
) )
# ------------------------------------------------------------------------------
# Colour terminal handling
#
class Colours: class Colours:
Default = 0 Default = 0
Black = 0 Black = 0
@ -79,6 +83,10 @@ class Colours:
return '' return ''
# ------------------------------------------------------------------------------
# Diff parsing, handling and printing
#
class DiffHunkSide(object): class DiffHunkSide(object):
"""A side of a diff hunk, recording line numbers""" """A side of a diff hunk, recording line numbers"""
def __init__(self, start): def __init__(self, start):
@ -179,6 +187,33 @@ def parse_diff(diff):
return hunks 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): def check_file(top_level, commit, filename):
# Extract the line numbers touched by the commit. # Extract the line numbers touched by the commit.
diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, 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: if len(lines) == 0:
return 0 return 0
# Format the file after the commit with astyle and compute the diff between # Format the file after the commit with all formatters and compute the diff
# the two files. # between the unformatted and formatted contents.
after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)], after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)],
stdout=subprocess.PIPE).stdout stdout=subprocess.PIPE).stdout
formatted = subprocess.run(['astyle', *astyle_options], after = after.decode('utf-8')
input=after, stdout=subprocess.PIPE).stdout
after = after.decode('utf-8').splitlines(True) formatted = after
formatted = formatted.decode('utf-8').splitlines(True) for formatter in formatters:
formatted = formatter(formatted)
after = after.splitlines(True)
formatted = formatted.splitlines(True)
diff = difflib.unified_diff(after, formatted) diff = difflib.unified_diff(after, formatted)
# Split the diff in hunks, recording line number ranges for each hunk. # Split the diff in hunks, recording line number ranges for each hunk.