[PATCH 5 of 6] code-code.py: Add a logfunc argument to checkfile to help external tool customisation

pierre-yves.david at logilab.fr pierre-yves.david at logilab.fr
Tue Mar 16 13:54:31 CDT 2010


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1268765580 -3600
# Branch stable
# Node ID 9f69798445cb5ed81a9a569b649bf722c5f5739c
# Parent  2184338434fdbf1e517c9800c5bb9a9e34d2c168
code-code.py: Add a logfunc argument to checkfile to help external tool customisation.
This changeset adds a logfunc argument to the checkfile function. This argument
must be a function called to report errors. A new logger class is also added to
keep the old behaviour by default.

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -132,8 +132,39 @@
     ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats),
     ('c', r'.*\.c$', cfilters, cpats),
 ]
-def checkfile(f, maxerr=None):
-    """checks style and portability of a given file"""
+
+class norepeatlogger(object):
+    """This object provide a log function displaying error with file and line
+    information"""
+    def __init__(self):
+        self._lastseen = None
+
+    def log(self, fname, lineno, line, msg):
+        """print error related a to given line of a given file.
+
+        The faulty line will also be printed but only once in the case of
+        multiple error.
+        :fname: filename
+        :lineno: line number
+        :line: actual content of the line
+        :msg: error message"""
+        msgid = fname, lineno, line
+        if msgid != self._lastseen:
+            print "%s:%d:" % (fname, lineno)
+            print " > %s" % line
+            self._lastseen = msgid
+        print " "+ msg
+
+_defaultlogger = norepeatlogger()
+
+def checkfile(f, logfunc=_defaultlogger.log, maxerr=None):
+    """checks style and portability of a given file
+
+    :f: filepath
+    :logfunc: function used to report error
+               logfunc(filename, linenumber, linecontent, errormessage
+    :maxerr: number of error to display before arborting.
+               Set to None (default) to report all errors"""
     for name, match, filters, pats in checks:
         fc = 0
         if not re.match(match, f):
@@ -148,14 +179,9 @@
         for n, l in z:
             if "check-code" + "-ignore" in l[0]:
                 continue
-            lc = 0
             for p, msg in pats:
                 if re.search(p, l[1]):
-                    if not lc:
-                        print "%s:%d:" % (f, n + 1)
-                        print " > %s" % l[0]
-                    print " %s" % msg
-                    lc += 1
+                    logfunc(f, n+1, l[0], msg)
                     fc += 1
             if maxerr is not None and fc >= maxerr:
                 print " (too many errors, giving up)"
@@ -168,6 +194,5 @@
         check = glob.glob("*")
     else:
         check = sys.argv[1:]
-
     for f in check:
         checkfile(f, maxerr=15)


More information about the Mercurial-devel mailing list