Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current ·  View Page History
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.

Dependencies

This plugin has the following dependencies:

  • Pylint must be installed to check Pylint coding rules.

Installation

  1. Install the Python plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
  2. 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

 

The features "Execution of unit tests" and "Coverage metrics" and thus the settings "sonar.python.xunit.reportPath" and "sonar.python.coverage.*" will be available in Sonar Python 1.1 (version currently in development)

Property

Scope

Example

Description

sonar.python.pylintSystem-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_configProject-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.reportPathProject-widenosetests.xmlAnt 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.reportPathProject-widecoverage.xmlAnt 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.itReportPathProject-wideit-coverage.xmlAnt 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.

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.1 (12 issues)
Type Key Summary Priority
Bug SONARPLUGINS-1691 Analysis is blocking sporadically Critical
New Feature SONARPLUGINS-2042 Rule: Use of non-existent pre-increment or pre-decrement operator Major
Improvement SONARPLUGINS-2083 Output of Pylint should be available as file after analysis Major
New Feature SONARPLUGINS-2184 Make the Python plugin support the Developer Cockpit plugin by providing the measures comments and loc by line Major
New Feature SONARPLUGINS-2154 Coverage metrics for Python code Major
Bug SONARPLUGINS-2212 Prevent conflicts in l10n of description for Python rules Major
New Feature SONARPLUGINS-2209 Embed the sonar-common-rules library Major
Task SONARPLUGINS-2198 Remove metric comment_blank_lines Major
Bug SONARPLUGINS-2137 Failure to pass custom configuration file to pylint correctly Major
Bug SONARPLUGINS-2122 Python plugin failes to parse a source file with a non-blank last line Major
New Feature SONARPLUGINS-2248 Provide new version of sslr-python-toolkit instead of python-devkit Major
New Feature SONARPLUGINS-1637 Integration of unit tests Major

 

Version 1.0 (26 issues)
Type Key Summary Priority
Bug SONARPLUGINS-1721 pygenie stops the code analyse if it tries to analyse a file not ending with a empty row Critical
New Feature SONARPLUGINS-2049 Rule: Avoid usage of print statement Major
New Feature SONARPLUGINS-2041 Rule: Avoid too complex file Major
New Feature SONARPLUGINS-2036 Rule: Avoid too complex class Major
New Feature SONARPLUGINS-2035 Metric: classes Major
New Feature SONARPLUGINS-2032 Rule: Always use upper case "L" for long integers Major
New Feature SONARPLUGINS-2031 Rule: Regular expression on comment Major
New Feature SONARPLUGINS-2028 Rule: Avoid too complex function Major
New Feature SONARPLUGINS-2030 Rule: Avoid deeply nested if statements Major
New Feature SONARPLUGINS-2024 Rule: Avoid usage of <> operator Major
New Feature SONARPLUGINS-2025 Rule: Line too long Major
New Feature SONARPLUGINS-2022 Rule: Avoid usage of exec statement Major
New Feature SONARPLUGINS-2026 Rule: One statement per line Major
New Feature SONARPLUGINS-2023 Rule: Avoid usage of `` (backtick) operator Major
New Feature SONARPLUGINS-2021 Rule: When there is a parsing error, this error should be added like a violation on that Python file Major
Improvement SONARPLUGINS-2020 Replace ANTLR-based lexer by our own Major
Improvement SONARPLUGINS-2019 Rename default quality profile to "Sonar way" to be consistent with other plugins Major
Bug SONARPLUGINS-2018 Python Plugin not compatible with SCM Activity Plugin Major
New Feature SONARPLUGINS-2016 Metric: blank comments Major
New Feature SONARPLUGINS-2013 Metric: statements Major
New Feature SONARPLUGINS-2014 Rule: Generic Python rule to define some homemade checks with an XPath expression Major
New Feature SONARPLUGINS-2012 Provide Python rule engine based on SSLR Major
New Feature SONARPLUGINS-2015 Metric: functions Major
New Feature SONARPLUGINS-1830 Implement Python parser based on SSLR Major
New Feature SONARPLUGINS-2006 Provide a default SQALE mapping for the Python rules Major
Bug SONARPLUGINS-1722 Absolut paths in sonar.python.pylint_config are prefixed with the path of the project Major

 

Version 0.1 (5 issues)
Type Key Summary Priority
Improvement SONARPLUGINS-1547 Make the pylint integration configurable Major
New Feature SONARPLUGINS-1639 Support of code duplication detection Major
Improvement SONARPLUGINS-1635 Rework the recognition of basic program entities Major
Improvement SONARPLUGINS-1634 Improve the pylint integration Major
New Feature SONARPLUGINS-1573 Add a colorizer for python code Major

 

Labels
  • None