I am using sonarqube/soanrpython on windows to analyse my python code and would like to be able to initiate the scan using setuptools instead of calling the scanner from the DOS prompt. Is this possible?. I have searched the web and cannot find anything.
I call the scanner using the following command
C:> sonar-scanner -Dsonar.projectKey=TL:python -Dsonar.sources=mypackage
But would like to be able to call
C:> python setup.py sonar
Or something similar
Edit:
To get this to work I put the following code in my setup.py file
A class:
class SonarPython(Command):
""" Run sonar-scanner via setuptools.
"""
description = 'running sonar-scanner for project '+name
user_options = [
('project-key=', 'k', 'project key (eg TL:python)'),
('source-dir=', 's', 'source dir location'),
]
def initialize_options(self):
self.project_key = None
self.source_dir = None
def finalize_options(self):
print("Sonar project_key is", self.project_key)
if self.project_key is None:
raise Exception("Parameter --project-key is missing (e.g. TL:python)")
print("Sonar using source_dir ", self.source_dir)
if self.source_dir is None:
raise Exception("Parameter --source-dir is missing (relative to setup.py)")
def run(self):
"""Run command.
"""
command = ['cmd', '/c', ]
command.append('sonar-scanner'+' -Dsonar.projectKey='+self.project_key+' -Dsonar.sources='+self.source_dir)
self.announce('Running command: %s' % str(command),level=distutils.log.INFO)
subprocess.check_call(command)
and and alteration to cmdclass and command_options
cmdclass={'sonar' : SonarPython},
command_options={
'sonar': {
'project_key': ('setup.py', 'TL:python'),
'source_dir': ('setup.py', 'mypackage')
}
},
You can then call sonar with the command
python setup.py sonar
You could leave out the command_options entry and just pass them on the command line as
python setup.py sonar --project_key 'TL:python' --source_dir 'myproject'
You can create a new distutils/setuptools command:
from distutils.core import setup, Command
import os
class SonarCommand(Command):
description = "Run sonarqube's sonar"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
os.system('sonar-scanner -Dsonar.projectKey=TL:python -Dsonar.sources=mypackage')
To enable the command you must reference it in setup():
setup(
# stuff omitted for conciseness
cmdclass={
'sonar': SonarCommand
}