Script to run pylint on our code.  Patches up pylint's import mechanism to work with our imports, adds default options to show only errors, and implements an file blacklist mechanism to exclude certain files that make pylint blow up.

Signed-off-by: Steve Howard <showard@google.com>



git-svn-id: http://test.kernel.org/svn/autotest/trunk@1731 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/run_pylint.py b/run_pylint.py
new file mode 100644
index 0000000..902b051
--- /dev/null
+++ b/run_pylint.py
@@ -0,0 +1,71 @@
+#!/usr/bin/python -u
+
+import os, sys, fnmatch
+import common
+
+pylintrc_path = os.path.expanduser('~/.pylintrc')
+if not os.path.exists(pylintrc_path):
+    open(pylintrc_path, 'w').close()
+
+import pylint.lint
+from pylint.checkers import imports
+
+ROOT_MODULE = 'autotest_lib.'
+
+# need to put autotest root dir on sys.path so pylint will be happy
+autotest_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+sys.path.insert(0, autotest_root)
+
+# patch up pylint import checker to handle our importing magic
+RealImportsChecker = imports.ImportsChecker
+
+class CustomImportsChecker(imports.ImportsChecker):
+    def visit_from(self, node):
+        if node.modname.startswith(ROOT_MODULE):
+            node.modname = node.modname[len(ROOT_MODULE):]
+        return RealImportsChecker.visit_from(self, node)
+
+imports.ImportsChecker = CustomImportsChecker
+
+# some files make pylint blow up, so make sure we ignore them
+blacklist = ['/contrib/*', '/frontend/afe/management.py']
+
+# only show errors
+pylint_base_opts = ['--disable-msg-cat=warning,refactor,convention',
+                    '--reports=no',
+                    '--include-ids=y']
+
+file_list = sys.argv[1:]
+if '--' in file_list:
+    index = file_list.index('--')
+    pylint_base_opts.extend(file_list[index+1:])
+    file_list = file_list[:index]
+
+
+def check_file(file_path):
+    if not file_path.endswith('.py'):
+        return
+    for blacklist_pattern in blacklist:
+        if fnmatch.fnmatch(os.path.abspath(file_path),
+                           '*' + blacklist_pattern):
+            return
+    pylint.lint.Run(pylint_base_opts + [file_path])
+
+
+def visit(arg, dirname, filenames):
+    for filename in filenames:
+        check_file(os.path.join(dirname, filename))
+
+
+def check_dir(dir_path):
+    os.path.walk(dir_path, visit, None)
+
+
+if len(file_list) > 0:
+    for path in file_list:
+        if os.path.isdir(path):
+            check_dir(path)
+        else:
+            check_file(path)
+else:
+    check_dir('.')