Most projects have a checkpatch facility, which can be used as a pre-commit
sanity check. This introduces such a mechanism to the Open vSwitch project
to catch some of the more silly formatting mistakes which can occur. It is
not meant to replace good code review practices, but it can help eliminate
the silly code review issues which get added.

Suggested-by: Mauricio Vásquez <mauricio.vasquezber...@studenti.polito.it>
Signed-off-by: Aaron Conole <acon...@redhat.com>
---
 CONTRIBUTING.md      |   4 ++
 python/checkpatch.py | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+)
 create mode 100644 python/checkpatch.py

diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 439c56a..25f203d 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -347,6 +347,10 @@ Please follow the style used in the code that you are 
modifying.  The
 [CodingStyle.md] file describes the coding style used in most of Open
 vSwitch. Use Linux kernel coding style for Linux kernel code.
 
+You can use the `python/checkpatch.py` utility as a quick check for
+certain commonly occuring mistakes (improper leading/trailing whitespace,
+missing signoffs, some improper formatted patch files).
+
 Example
 -------
 
diff --git a/python/checkpatch.py b/python/checkpatch.py
new file mode 100644
index 0000000..2bd13f0
--- /dev/null
+++ b/python/checkpatch.py
@@ -0,0 +1,137 @@
+# Copyright (c) 2016 Red Hat, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import re
+import sys
+import email
+
+__errors = 0
+__warnings = 0
+
+def print_error(message, lineno = None):
+    global __errors
+    if lineno != None:
+        print "E(%d): %s" % (lineno, message)
+    else:
+        print "E: %s" % (message)
+
+    __errors = __errors + 1
+
+def print_warning(message, lineno = None):
+    global __warnings
+    if lineno != None:
+        print "W(%d): %s" % (lineno, message)
+    else:
+        print "W: %s" % (message)
+
+    __warnings = __warnings + 1
+
+__regex_added_line = re.compile(r'^\+{1,2}[^\+][\w\W]*')
+__regex_leading_with_whitespace_at_all = re.compile(r'^\s+')
+__regex_leading_with_spaces = re.compile(r'^ +[\S]+')
+__regex_trailing_whitespace = re.compile(r'[^\S]+$')
+__regex_for_if_missing_whitespace = re.compile(r'(if|for|while)[\(]')
+__regex_for_if_too_much_whitespace = re.compile(r'(if|for|while)  +[\(]')
+__regex_for_if_parens_whitespace = re.compile(r'(if|for|while) \( +[\s\S]+\)')
+
+def is_added_line(line):
+    """Returns TRUE if the line in question is an added line.
+    """
+    return __regex_added_line.search(line) != None
+
+def leading_whitespace_is_spaces(line):
+    """Returns TRUE if the leading whitespace in added lines is spaces
+    """
+    if __regex_leading_with_whitespace_at_all.search(line) != None:
+        return __regex_leading_with_spaces.search(line) != None
+    return True
+
+def trailing_whitespace_or_crlf(line):
+    """Returns TRUE if the trailing characters is whitespace
+    """
+    return __regex_trailing_whitespace.search(line) != None
+
+def if_and_for_whitespace_checks(line):
+    """Return TRUE if there is appropriate whitespace after if and for"""
+    if (__regex_for_if_missing_whitespace.search(line) != None or
+        __regex_for_if_too_much_whitespace.search(line) != None or
+        __regex_for_if_parens_whitespace.search(line)):
+       return False
+    return True
+
+def ovs_checkpatch_parse(text):
+    lineno = 0
+    signatures = 0
+    extra_people = 0
+    parse = 0
+
+    scissors = re.compile(r'^[\w]*---[\w]*')
+    hunks = re.compile('^(---|\+\+\+) (\S+)')
+    is_signature = re.compile(r'Signed-off-by: .*$')
+
+    is_extra_people = re.compile(
+        r'^(Tested|Reviewed|Acked|Nacked|Reported)-by: .*$', re.M | re.I)
+
+    for line in text.split('\n'):
+        lineno = lineno + 1
+        if len(line) <= 0:
+            continue
+
+        if parse == 1:
+            if hunks.match(line):
+                parse = parse + 1
+            continue
+        elif parse == 0:
+            if scissors.match(line):
+                parse = parse + 1
+                if signatures == 0:
+                    print_error("No signatures found.")
+            elif is_signature.match(line):
+                signatures = signatures + 1
+            elif is_extra_people.match(line):
+                extra_people = extra_people + 1
+        elif parse == 2 and is_added_line(line):
+            print_line = False
+            if not leading_whitespace_is_spaces(line[1:]):
+                print_line = True
+                print_warning("Line has non-spaces leading whitespace",
+                              lineno)
+            if trailing_whitespace_or_crlf(line[1:]):
+                print_line = True
+                print_warning("Line has trailing whitespace", lineno)
+            if len(line[1:]) > 80:
+                print_line = True
+                print_warning("Line is greater than 80-characters long",
+                              lineno)
+            if not if_and_for_whitespace_checks(line[1:]):
+                print_line = True
+                print_warning("Improper whitespace around control block",
+                              lineno);
+            if print_line:
+                print line
+
+def ovs_checkpatch_file(filename):
+    try:
+        mail = email.message_from_file(open(filename, 'r'))
+    except:
+        print_error("Unable to parse file '%s'. Is it a patch?" % filename)
+        return -1
+
+    for part in mail.walk():
+        if part.get_content_maintype() == 'multipart':
+            continue
+        return ovs_checkpatch_parse(part.get_payload(decode=True))
+
+if __name__ == '__main__':
+    ovs_checkpatch_file(sys.argv[1])
-- 
2.8.0.rc2.35.gc2c5f6b.dirty

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to