From 3c8886d6981e1238657cc6ee9dcf267f090fca41 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 18 Dec 2018 22:42:30 +0200 Subject: 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 --- utils/checkstyle.py | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'utils') 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. -- cgit v1.2.1