运行 来自 setup.py 的声纳扫描仪
Running sonar-scanner from setup.py
我在 windows 上使用 sonarqube/soanrpython 来分析我的 python 代码,并希望能够使用 setuptools 启动扫描,而不是从 DOS 提示符调用扫描程序。这可能吗?。我已经在网上搜索过,但找不到任何内容。
我使用以下命令调用扫描仪
C:> sonar-scanner -Dsonar.projectKey=TL:python -Dsonar.sources=mypackage
但希望能够打电话
C:> python setup.py sonar
或类似的东西
编辑:
为了让它工作,我将以下代码放入我的 setup.py 文件
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 和对 cmdclass 和 command_options
的修改
cmdclass={'sonar' : SonarPython},
command_options={
'sonar': {
'project_key': ('setup.py', 'TL:python'),
'source_dir': ('setup.py', 'mypackage')
}
},
然后您可以使用命令调用声纳
python setup.py sonar
您可以省略 command_options 条目,将它们作为
在命令行中传递
python setup.py sonar --project_key 'TL:python' --source_dir 'myproject'
很遗憾,这不可用。
您可以创建一个新的 distutils/setuptools 命令:
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')
要启用该命令,您必须在 setup() 中引用它:
setup(
# stuff omitted for conciseness
cmdclass={
'sonar': SonarCommand
}
我在 windows 上使用 sonarqube/soanrpython 来分析我的 python 代码,并希望能够使用 setuptools 启动扫描,而不是从 DOS 提示符调用扫描程序。这可能吗?。我已经在网上搜索过,但找不到任何内容。
我使用以下命令调用扫描仪
C:> sonar-scanner -Dsonar.projectKey=TL:python -Dsonar.sources=mypackage
但希望能够打电话
C:> python setup.py sonar
或类似的东西
编辑:
为了让它工作,我将以下代码放入我的 setup.py 文件
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 和对 cmdclass 和 command_options
的修改cmdclass={'sonar' : SonarPython},
command_options={
'sonar': {
'project_key': ('setup.py', 'TL:python'),
'source_dir': ('setup.py', 'mypackage')
}
},
然后您可以使用命令调用声纳
python setup.py sonar
您可以省略 command_options 条目,将它们作为
在命令行中传递python setup.py sonar --project_key 'TL:python' --source_dir 'myproject'
很遗憾,这不可用。
您可以创建一个新的 distutils/setuptools 命令:
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')
要启用该命令,您必须在 setup() 中引用它:
setup(
# stuff omitted for conciseness
cmdclass={
'sonar': SonarCommand
}