[PATCH 2 of 6] check-code.py: Only call check-code if __name__ = "__main__"

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


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1268765576 -3600
# Branch stable
# Node ID 128c0f0fcdced5b3012728218db424d556faf956
# Parent  f273bb861993c68dce35048e09549e5222b66f0b
check-code.py: Only call check-code if __name__ = "__main__".

This changeset moves the code that actually executes something to the "if
__name__ = '__main__'" section to allow the file to be imported as a module.

diff --git a/contrib/check-code.py b/contrib/check-code.py
--- a/contrib/check-code.py
+++ b/contrib/check-code.py
@@ -133,36 +133,37 @@
     ('c', r'.*\.c$', cfilters, cpats),
 ]
 
-if len(sys.argv) == 1:
-    check = glob.glob("*")
-else:
-    check = sys.argv[1:]
+if __name__ == "__main__":
+    if len(sys.argv) == 1:
+        check = glob.glob("*")
+    else:
+        check = sys.argv[1:]
 
-for f in check:
-    for name, match, filters, pats in checks:
-        fc = 0
-        if not re.match(match, f):
-            continue
-        pre = post = open(f).read()
-        if "no-" + "check-code" in pre:
+    for f in check:
+        for name, match, filters, pats in checks:
+            fc = 0
+            if not re.match(match, f):
+                continue
+            pre = post = open(f).read()
+            if "no-" + "check-code" in pre:
+                break
+            for p, r in filters:
+                post = re.sub(p, r, post)
+            # print post # uncomment to show filtered version
+            z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
+            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
+                        fc += 1
+                if fc == 15:
+                    print " (too many errors, giving up)"
+                    break
             break
-        for p, r in filters:
-            post = re.sub(p, r, post)
-        # print post # uncomment to show filtered version
-        z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
-        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
-                    fc += 1
-            if fc == 15:
-                print " (too many errors, giving up)"
-                break
-        break


More information about the Mercurial-devel mailing list