Merged changes in the trunk up to revision 42116.
[blender.git] / source / tests / pep8.py
index 3ccd7dd79b6a500d09a75803e69244e08f5eab1d..4b957f2090706a83be36b5a75782c8cd24713436 100644 (file)
@@ -16,7 +16,7 @@
 #
 # ##### END GPL LICENSE BLOCK #####
 
-# <pep8 compliant>
+# <pep8-80 compliant>
 
 import os
 
@@ -31,23 +31,32 @@ import os
 # in debian install pylint pyflakes pep8 with apt-get/aptitude/etc
 #
 # on *nix run
-#   python release/test/pep8.py > pep8_error.txt 2>&1
+#   python source/tests/pep8.py > test_pep8.log 2>&1
 
 # how many lines to read into the file, pep8 comment
 # should be directly after the licence header, ~20 in most cases
 PEP8_SEEK_COMMENT = 40
 SKIP_PREFIX = "./tools", "./config", "./scons", "./extern"
+FORCE_PEP8_ALL = False
 
 
 def file_list_py(path):
     for dirpath, dirnames, filenames in os.walk(path):
         for filename in filenames:
-            if filename.endswith(".py"):
+            if filename.endswith(".py") or filename.endswith(".cfg"):
                 yield os.path.join(dirpath, filename)
 
 
 def is_pep8(path):
     print(path)
+    if open(path, 'rb').read(3) == b'\xef\xbb\xbf':
+        print("\nfile contains BOM, remove first 3 bytes: %r\n" % path)
+
+    # templates dont have a header but should be pep8
+    for d in ("presets", "templates", "examples"):
+        if ("%s%s%s" % (os.sep, d, os.sep)) in path:
+            return 1
+
     f = open(path, 'r', encoding="utf8")
     for i in range(PEP8_SEEK_COMMENT):
         line = f.readline()
@@ -67,7 +76,7 @@ def main():
         if [None for prefix in SKIP_PREFIX if f.startswith(prefix)]:
             continue
 
-        pep8_type = is_pep8(f)
+        pep8_type = FORCE_PEP8_ALL or is_pep8(f)
 
         if pep8_type:
             # so we can batch them for each tool.
@@ -79,7 +88,15 @@ def main():
     for f in files_skip:
         print("    %s" % f)
 
-    # pyflakes
+    # strict imports
+    print("\n\n\n# running pep8...")
+    import re
+    import_check = re.compile(r"\s*from\s+[A-z\.]+\s+import \*\s*")
+    for f, pep8_type in files:
+        for i, l in enumerate(open(f, 'r', encoding='utf8')):
+            if import_check.match(l):
+                print("%s:%d:0: global import bad practice" % (f, i + 1))
+
     print("\n\n\n# running pep8...")
     for f, pep8_type in files:
         if pep8_type == 1:
@@ -88,6 +105,7 @@ def main():
         else:
             os.system("pep8 --repeat '%s'" % (f))
 
+    # pyflakes
     print("\n\n\n# running pyflakes...")
     for f, pep8_type in files:
         os.system("pyflakes '%s'" % f)
@@ -95,7 +113,28 @@ def main():
     print("\n\n\n# running pylint...")
     for f, pep8_type in files:
         # let pep8 complain about line length
-        os.system("pylint --reports=n --max-line-length=1000 '%s'" % f)
+        os.system("pylint "
+                  "--disable="
+                  "C0111,"  # missing docstring
+                  "C0103,"  # invalid name
+                  "W0613,"  # unused argument, may add this back
+                            # but happens a lot for 'context' for eg.
+                  "W0232,"  # class has no __init__, Operator/Panel/Menu etc
+                  "W0142,"  # Used * or ** magic
+                            # even needed in some cases
+                  "R0902,"  # Too many instance attributes
+                  "R0903,"  # Too many statements
+                  "R0911,"  # Too many return statements
+                  "R0912,"  # Too many branches
+                  "R0913,"  # Too many arguments
+                  "R0914,"  # Too many local variables
+                  "R0915,"  # Too many statements
+                  " "
+                  "--include-ids=y "
+                  "--output-format=parseable "
+                  "--reports=n "
+                  "--max-line-length=1000"
+                  " '%s'" % f)
 
 if __name__ == "__main__":
     main()