summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYun Mao <yunmao@gmail.com>2012-08-15 18:26:41 -0400
committerYun Mao <yunmao@gmail.com>2012-08-24 15:36:47 -0400
commit09eb54c48f97e7a7b9118f54c00a78fa1cbcb61d (patch)
tree6d68eb2aa9df68dfbadf06aecbf5da50c003ece0
parent4b7c87a17affe530a8c196df993992cfe5202911 (diff)
downloadnova-09eb54c48f97e7a7b9118f54c00a78fa1cbcb61d.tar.gz
nova-09eb54c48f97e7a7b9118f54c00a78fa1cbcb61d.tar.xz
nova-09eb54c48f97e7a7b9118f54c00a78fa1cbcb61d.zip
Add lintstack error checker based on pylint
Add a new silent test target lintstack based on pylint in Jenkins. lintstack will compare the pylint errors between HEAD and HEAD~1, filter out known false positives and report violations in pylint original parseable format for Jenkins to consume. Change-Id: Ic2962cb42a174bfe9535aea88475ff4ede3bf9ff
-rwxr-xr-xtools/lintstack.py199
-rwxr-xr-xtools/lintstack.sh36
-rw-r--r--tools/test-requires1
-rw-r--r--tox.ini8
4 files changed, 243 insertions, 1 deletions
diff --git a/tools/lintstack.py b/tools/lintstack.py
new file mode 100755
index 000000000..ce9b6f8a6
--- /dev/null
+++ b/tools/lintstack.py
@@ -0,0 +1,199 @@
+#!/usr/bin/env python
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright (c) 2012, AT&T Labs, Yun Mao <yunmao@gmail.com>
+# All Rights Reserved.
+#
+# 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.
+
+"""pylint error checking"""
+
+import cStringIO as StringIO
+import json
+import re
+import sys
+
+from pylint import lint
+from pylint.reporters import text
+
+# Note(maoy): E1103 is error code related to partial type inference
+ignore_codes = ["E1103"]
+# Note(maoy): the error message is the pattern of E0202. It should be ignored
+# for nova.tests modules
+ignore_messages = ["An attribute affected in nova.tests"]
+# Note(maoy): we ignore all errors in openstack.common because it should be
+# checked elsewhere. We also ignore nova.tests for now due to high false
+# positive rate.
+ignore_modules = ["nova/openstack/common/", "nova/tests/"]
+
+KNOWN_PYLINT_EXCEPTIONS_FILE = "tools/pylint_exceptions"
+
+
+class LintOutput(object):
+
+ _cached_filename = None
+ _cached_content = None
+
+ def __init__(self, filename, lineno, line_content, code, message,
+ lintoutput):
+ self.filename = filename
+ self.lineno = lineno
+ self.line_content = line_content
+ self.code = code
+ self.message = message
+ self.lintoutput = lintoutput
+
+ @classmethod
+ def from_line(cls, line):
+ m = re.search(r"(\S+):(\d+): \[(\S+)(, \S+)?] (.*)", line)
+ matched = m.groups()
+ filename, lineno, code, message = (matched[0], int(matched[1]),
+ matched[2], matched[-1])
+ if cls._cached_filename != filename:
+ with open(filename) as f:
+ cls._cached_content = list(f.readlines())
+ cls._cached_filename = filename
+ line_content = cls._cached_content[lineno - 1].rstrip()
+ return cls(filename, lineno, line_content, code, message,
+ line.rstrip())
+
+ @classmethod
+ def from_msg_to_dict(cls, msg):
+ """From the output of pylint msg, to a dict, where each key
+ is a unique error identifier, value is a list of LintOutput
+ """
+ result = {}
+ for line in msg.splitlines():
+ obj = cls.from_line(line)
+ if obj.is_ignored():
+ continue
+ key = obj.key()
+ if key not in result:
+ result[key] = []
+ result[key].append(obj)
+ return result
+
+ def is_ignored(self):
+ if self.code in ignore_codes:
+ return True
+ if any(self.filename.startswith(name) for name in ignore_modules):
+ return True
+ if any(msg in self.message for msg in ignore_messages):
+ return True
+ return False
+
+ def key(self):
+ if self.code in ["E1101", "E1103"]:
+ # These two types of errors are like Foo class has no member bar.
+ # We discard the source code so that the error will be ignored
+ # next time another Foo.bar is encountered.
+ return self.message, ""
+ return self.message, self.line_content.strip()
+
+ def json(self):
+ return json.dumps(self.__dict__)
+
+ def review_str(self):
+ return ("File %(filename)s\nLine %(lineno)d:%(line_content)s\n"
+ "%(code)s: %(message)s" % self.__dict__)
+
+
+class ErrorKeys(object):
+
+ @classmethod
+ def print_json(cls, errors, output=sys.stdout):
+ print >>output, "# automatically generated by tools/lintstack.py"
+ for i in sorted(errors.keys()):
+ print >>output, json.dumps(i)
+
+ @classmethod
+ def from_file(cls, filename):
+ keys = set()
+ for line in open(filename):
+ if line and line[0] != "#":
+ d = json.loads(line)
+ keys.add(tuple(d))
+ return keys
+
+
+def run_pylint():
+ buff = StringIO.StringIO()
+ reporter = text.ParseableTextReporter(output=buff)
+ args = ["--include-ids=y", "-E", "nova"]
+ lint.Run(args, reporter=reporter, exit=False)
+ val = buff.getvalue()
+ buff.close()
+ return val
+
+
+def generate_error_keys(msg=None):
+ print "Generating", KNOWN_PYLINT_EXCEPTIONS_FILE
+ if msg is None:
+ msg = run_pylint()
+ errors = LintOutput.from_msg_to_dict(msg)
+ with open(KNOWN_PYLINT_EXCEPTIONS_FILE, "w") as f:
+ ErrorKeys.print_json(errors, output=f)
+
+
+def validate(newmsg=None):
+ print "Loading", KNOWN_PYLINT_EXCEPTIONS_FILE
+ known = ErrorKeys.from_file(KNOWN_PYLINT_EXCEPTIONS_FILE)
+ if newmsg is None:
+ print "Running pylint. Be patient..."
+ newmsg = run_pylint()
+ errors = LintOutput.from_msg_to_dict(newmsg)
+
+ print "Unique errors reported by pylint: was %d, now %d." \
+ % (len(known), len(errors))
+ passed = True
+ for err_key, err_list in errors.items():
+ for err in err_list:
+ if err_key not in known:
+ print err.lintoutput
+ print
+ passed = False
+ if passed:
+ print "Congrats! pylint check passed."
+ redundant = known - set(errors.keys())
+ if redundant:
+ print "Extra credit: some known pylint exceptions disappeared."
+ for i in sorted(redundant):
+ print json.dumps(i)
+ print "Consider regenerating the exception file if you will."
+ else:
+ print ("Please fix the errors above. If you believe they are false"
+ " positives, run 'tools/lintstack.py generate' to overwrite.")
+ sys.exit(1)
+
+
+def usage():
+ print """Usage: tools/lintstack.py [generate|validate]
+ To generate pylint_exceptions file: tools/lintstack.py generate
+ To validate the current commit: tools/lintstack.py
+ """
+
+
+def main():
+ option = "validate"
+ if len(sys.argv) > 1:
+ option = sys.argv[1]
+ if option == "generate":
+ generate_error_keys()
+ elif option == "validate":
+ validate()
+ else:
+ usage()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/lintstack.sh b/tools/lintstack.sh
new file mode 100755
index 000000000..848a16fa5
--- /dev/null
+++ b/tools/lintstack.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2012, AT&T Labs, Yun Mao <yunmao@gmail.com>
+# All Rights Reserved.
+#
+# 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.
+
+# Use lintstack.py to compare pylint errors between HEAD and HEAD~1
+
+set -e
+TOOLS_DIR=$(cd $(dirname "$0") && pwd)
+GITHEAD=`git rev-parse HEAD`
+cp -f $TOOLS_DIR/lintstack.py $TOOLS_DIR/lintstack.head.py
+git checkout HEAD~1
+# First generate tools/pylint_exceptions from HEAD~1
+$TOOLS_DIR/lintstack.head.py generate
+# Then use that as a reference to compare against HEAD
+git checkout $GITHEAD
+$TOOLS_DIR/lintstack.head.py
+echo "Check passed. FYI: the pylint exceptions are:"
+cat $TOOLS_DIR/pylint_exceptions
+echo
+echo "You are in detached HEAD mode. If you are a developer"
+echo "and not very familiar with git, you might want to do"
+echo "'git checkout branch-name' to go back to your branch."
+
diff --git a/tools/test-requires b/tools/test-requires
index 279e87636..0591d5026 100644
--- a/tools/test-requires
+++ b/tools/test-requires
@@ -6,5 +6,6 @@ mox==0.5.3
nose
openstack.nose_plugin>=0.7
pep8==1.1
+pylint==0.25.2
sphinx>=1.1.2
feedparser
diff --git a/tox.ini b/tox.ini
index 4957ce39a..9e454472b 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist = py26,py27,pep8
+envlist = py26,py27,pep8,pylint
[testenv]
setenv = VIRTUAL_ENV={envdir}
@@ -26,6 +26,12 @@ commands =
python tools/hacking.py --ignore=N4 --repeat --show-source \
--exclude=.venv,.tox,dist,doc,*openstack/common*,*lib/python*,*egg .
+[testenv:pylint]
+setenv = VIRTUAL_ENV={envdir}
+deps = -r{toxinidir}/tools/pip-requires
+ pylint==0.25.2
+commands = bash tools/lintstack.sh
+
[testenv:cover]
setenv = NOSE_WITH_COVERAGE=1