| Live example If you want to see a live example of the capabilities of the Python plugin, you can have a look at the analysis of the PyFFI project on Nemo. |
Description / Features
This plugin enables the analysis of Python projects.
It is compatible with the Sonar Eclipse plugin to track violations while coding. It is also compatible with the Issues Report plugin to run pre-commit local analysis.
Dependencies
This plugin has the following dependencies:
- Pylint must be installed to check Pylint coding rules.
Installation
- Install the Python plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
- Restart the Sonar server
Usage
Run a Sonar Analysis with the Sonar Runner (Recommended Way)
To launch a Sonar analysis of your Python project, use the Sonar Runner.
A sample project is available on github that can be browsed or downloaded: /projects/languages/python/python-sonar-runner.
Run a Sonar Analysis with the other Analyzers
Maven and Ant can also be used to launch analysis on Python projects.
Advanced Settings
Property | Scope | Example | Description |
|---|---|---|---|
| sonar.python.pylint | System-wide | /usr/local/bin/pylint | Path to the pylint executable to use in pylint analysis. Set to empty to use the default one. It has to be set on Windows, use either masked backslashes or slashes as path separators; examples: C:\\Python26\\Scripts\\pylint.bat or C:/Python/26/Scripts/pylint.bat |
| sonar.python.pylint_config | Project-wide | .pylintrc | Path to the pylint configuration file (relative to project root or absolute) to use in pylint analysis. Set to empty to use the default. |
| sonar.python.xunit.reportPath | Project-wide | nosetests.xml | Since version 1.1. Ant pattern describing the path to unit test execution reports, relative to projects root. Leave unset to use the default ("xunit-reports/xunit-result-*.xml"). The reports have to conform to the junitreport XML format. |
| sonar.python.coverage.reportPath | Project-wide | coverage.xml | Since version 1.1. Ant pattern describing the path to coverage reports, relative to projects root. Leave unset to use the default ("coverage-reports/coverage-*.xml"). The reports have to conform to the Cobertura XML format. |
| sonar.python.coverage.itReportPath | Project-wide | it-coverage.xml | Since version 1.1. Ant pattern describing the path to coverage reports for integration tests, relative to projects root. Leave unset to use the default ("coverage-reports/it-coverage-*.xml"). The reports have to conform to the Cobertura XML format. |
Make sure to set up the environment properly (PYTHONPATH etc.) before running the analyzer, if your project requires it.
External report generation
Here is a quick guide how to generate the reports using external tools.
Test execution
nosetests --with-xunit [tests to execute]
To get a more accurate execution time statistics make sure to use nose 1.1.0 or newer.
Coverage
Use Ned Batchelders coverage package like this:
coverage erase coverage run --branch --sources=<python packages> <program> coverage xml -i
Make sure to put all packages to measure into the --source parameter. That ensures that coverage will report zero coverage on all untouched files, as you most probably want to. To make this work, make sure to meet two prerequisites:
- use coverage => 3.4
- the packages should be packages, i.e. every directory on the way to the sources should contain an __init__.py file.
Metrics
See Metrics documentation page.
Extending Coding Rules using XPath
New coding rules can be added using XPath. See the related documentation.
To navigate the AST, download the SSLR Python Toolkit.
Change Log
| Version 1.2 (unreleased) (3 issues) | ||||
|---|---|---|---|---|
| Type | Key | Summary | Priority | Status |
|
|
SONARPLUGINS-2408 | Remove dependencies on deprecated API extensions |
|
|
|
|
SONARPLUGINS-2341 | Do not embed Ant as a library for plugin |
|
|
|
|
SONARPLUGINS-2823 | XPath rule should not lead to NPE, when parse error happens |
|
|

