From patchwork Tue Dec 18 20:46:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 55 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 972A260B0C for ; Tue, 18 Dec 2018 21:45:21 +0100 (CET) Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 22FD9549 for ; Tue, 18 Dec 2018 21:45:20 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545165920; bh=k67WhWz0yJkn+wjdZyxfq8bu5iTbKSxsdxKjI97LGgA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fjPak4XPCXtiHHsAANPm0N2ky//4prFSL496wHmNKuEaUZirrcnVw8HG3a2tjHMA/ +pY1O2DgFaUzf8g2UbU9wGr95qOSZsg/6v+yrBhef3SUyYXuitDptW2hI5lhiGNpaR JIJbskOUnAMXgXN9lIEGK1dm6Bla8oiLREqBqBE0= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 18 Dec 2018 22:46:09 +0200 Message-Id: <20181218204609.22902-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181218204609.22902-1-laurent.pinchart@ideasonboard.com> References: <20181218204609.22902-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] utils: checkstyle.py: Strip trailing white spaces X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Dec 2018 20:45:21 -0000 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(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index dadb55dfb8c1..32974815048a 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.