If you write a lot of Python code it pays to setup ways to automate code quality control. Jenkins, an open source continuous integration server, is a great platform for: automated testing, code coverage reports, and static analysis with Pylint. In this blog post, I am only going to focus on Pylint, but stay tuned for more Jenkins topics in the future.
Pylint, if you haven’t used it before, is the defacto static analysis tool for Python programmers. It isn’t perfect, and it does need a decent amount of configuration and tuning, but it is quite useful. At Loggly we are using Jenkins to automatically run Pylint on git pushes to Master. You can see a gist of our Pylint command here: https://gist.github.com/1175983.
One strategy to get Pylint integrated into an automated build of your software is to set a low bar. More then likely you will need to turn off many warnings, initially, to get things working on every checkin. Once you have this automated, you can then get fancier and slowly turn up the “heat” on your code. If you look at the pylint command above, you will notice that the following warnings were turned off: W0622,W0611,F0401,R0914,W0221,W0222,W0142,F0010,W0703,R0911.For example, one of the disabled warnings above is: R0911: Too many return statements (%s/%s) Used when a function or method has too many return statement, making it hard to follow. Once things are stabilized for a few days under the current Pylint settings, we may look to refactor the code to eliminate some of these return statements and turn the warning back on.
Jenkins has an interesting plugin called Violations, which can reject a build, based on a Pylint score threshold. If you look at the following Pylint output, you will see a Pylint score of 10/10: https://gist.github.com/1176006. If the Pylint score dropped below 10 on a git push, a build failure would occur as shown in picture below.
I hope you enjoyed the blog post, and if you have a Jenkins/Pylint, we would love to hear from you.
1. Writing clean, testable, high quality code in Python: http://www.ibm.com/developerworks/aix/library/au-cleancode/
2. Jenkins CI: http://jenkins-ci.org/
3. Pylint: http://www.logilab.org/857