Static code analysis in Python with Jenkins

I am checking the code quality of a python script called which is in the root folder of my code .

To do so I installed the following packages (I am with Python 27 on Windows machine):

pip install pylint
easy_install -U clonedigger
pip install flake8

On Jenkins make sure you have setup Warning plugin and Violation plugin.

In Jenkins configuration, under Compiler Warning section we add a parser for flake8 with:

Name: flake8
Link name: Flake8 warnings
Trend report name: Flake8 warnings trend
Regular expression: ^(.*):([0-9]*):([0-9]*):(.[CE][0-9]*)(.*)$
Mapping script:
import hudson.plugins.warnings.parser.Warning
import hudson.plugins.analysis.util.model.Priority

String fileName =
String lineNumber =
String category =
String message =

return new Warning(fileName, Integer.parseInt(lineNumber), category, "PyFlakes Parser", message, Priority.NORMAL);;

Note from the regular expression the [CE] pattern in order to find Complexity and Error id problems. If you are not interested in the complexity just replace [CE] with E.

Now on the jenkins job configuration we add a build step “Execute Windows batch command” (you could do it similarly for Linux) with the following content:

rmdir /s /q output
mkdir output
pylint --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" --reports=y >> output/pylint.log
clonedigger --cpd-output -o output/clonedigger.xml
flake8 --max-complexity 12 --output-file output/flake8-output.txt

The batch creates an output folder where pylint, clonedigger and flake8 generate their files (if you want you could put the script inside in a bat file having as parameters the output folder and the file).

Add a post build action “Scan for compiler warnings”, select “scan workspace files” and with the following parameters:

File patterns: output/flake8-output.txt
Parser: flake8

Add a post build action “Report violations”  with the following parameters:

cpd: output/clonedigger.xml
pylint: output/pylint.log

Run the build and you should see the trend graph for Flake8 and the violations for cpd (clonedigger) and pylint!


If Flake8 exits with exit 1 failing your build, you might probably change the file:


At the end of the def main(): function you should have:

if exit_code > 0:
    raise SystemExit(exit_code > 0)

Which you can replace with:

raise SystemExit(0)

Now Flake8 should exit without problem.







4 thoughts on “Static code analysis in Python with Jenkins

  1. Really bad practice to encourage changing system files like this. Especially when you can trivially work around the shell issue by throwing away flake8’s return code: flake8 || exit 0

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s