From patchwork Tue Dec 18 20:46:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 56 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 55C6260B23 for ; Tue, 18 Dec 2018 21:45:24 +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 C87CD53A for ; Tue, 18 Dec 2018 21:45:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1545165919; bh=0VoY01dXeq80F1bln/7FulDHCCh+T3RxzJFsWGDR1zg=; h=From:To:Subject:Date:From; b=VqwcOH3tJ4wHQluZl+Q8nnq+YyyNFsYwjV5UGC4KCT+um6g+XmVPYzUdglon9Q45u R0BGirRiTEgWJX8DiB3PeXPf9mmfpDLeLTmVpIukYlXoKOSO0DQXfWPe7mXgtSWcUm 6cMrsutoo5tjco1PQVwQanL47H8xsD+iVRd1EtpM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Tue, 18 Dec 2018 22:46:08 +0200 Message-Id: <20181218204609.22902-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/2] utils: checkstyle.py: Highlight trailing white space at end of line 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:24 -0000 In order to facilitate interpretation of diffs, highlight trailing white space at end of lines with a red background. Signed-off-by: Laurent Pinchart --- utils/checkstyle.py | 67 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index b7c4d876a145..dadb55dfb8c1 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -40,18 +40,43 @@ source_extensions = ( class Colours: Default = 0 + Black = 0 Red = 31 Green = 32 + Yellow = 33 + Blue = 34 + Magenta = 35 Cyan = 36 + LightGrey = 37 + DarkGrey = 90 + LightRed = 91 + LightGreen = 92 + Lightyellow = 93 + LightBlue = 94 + LightMagenta = 95 + LightCyan = 96 + White = 97 + + @staticmethod + def fg(colour): + if sys.stdout.isatty(): + return '\033[%um' % colour + else: + return '' -for attr in Colours.__dict__.keys(): - if attr.startswith('_'): - continue + @staticmethod + def bg(colour): + if sys.stdout.isatty(): + return '\033[%um' % (colour + 10) + else: + return '' - if sys.stdout.isatty(): - setattr(Colours, attr, '\033[0;%um' % getattr(Colours, attr)) - else: - setattr(Colours, attr, '') + @staticmethod + def reset(): + if sys.stdout.isatty(): + return '\033[0m' + else: + return '' class DiffHunkSide(object): @@ -82,20 +107,30 @@ class DiffHunk(object): def __repr__(self): s = '%s@@ -%u,%u +%u,%u @@\n' % \ - (Colours.Cyan, + (Colours.fg(Colours.Cyan), self.__from.start, len(self.__from), self.__to.start, len(self.__to)) for line in self.lines: if line[0] == '-': - s += Colours.Red + s += Colours.fg(Colours.Red) elif line[0] == '+': - s += Colours.Green - else: - s += Colours.Default + s += Colours.fg(Colours.Green) + + if line[0] == '-': + spaces = 0 + for i in range(len(line)): + if line[-i-1].isspace(): + spaces += 1 + else: + break + spaces = len(line) - spaces + line = line[0:spaces] + Colours.bg(Colours.Red) + line[spaces:] + s += line + s += Colours.reset() + s += '\n' - s += Colours.Default return s def append(self, line): @@ -111,7 +146,7 @@ class DiffHunk(object): self.__to.touched.append(self.__to_line) self.__to_line += 1 - self.lines.append(line) + self.lines.append(line.rstrip('\n')) def intersects(self, lines): for line in lines: @@ -180,8 +215,8 @@ def check_file(top_level, commit, filename): if len(formatted_diff) == 0: return 0 - print('%s---' % Colours.Red, filename) - print('%s+++' % Colours.Green, filename) + print('%s---' % Colours.fg(Colours.Red), filename) + print('%s+++' % Colours.fg(Colours.Green), filename) for hunk in formatted_diff: print(hunk) 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.