从 setup.py 运行声纳扫描仪 [英] Running sonar-scanner from setup.py

查看:82
本文介绍了从 setup.py 运行声纳扫描仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 windows 上使用 sonarqube/soanrpython 来分析我的 python 代码,并希望能够使用 setuptools 启动扫描,而不是从 DOS 提示符下调用扫描仪.这可能吗?.我在网上搜索过,没有找到任何东西.

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

但是希望能够调用

C:> python setup.py sonar

或类似的东西

为了让它工作,我把下面的代码放在我的 setup.py 文件中

To get this to work I put the following code in my setup.py file

一类:

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)

和更改 cmdclass 和 command_options

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

您可以省略 command_options 条目,并在命令行中将它们作为

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'

推荐答案

您可以新建一个 distutils/setuptools 命令:

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')

要启用该命令,您必须在 setup() 中引用它:

To enable the command you must reference it in setup():

setup(
     # stuff omitted for conciseness
     cmdclass={
        'sonar': SonarCommand
}

请参阅文档和示例(12).

这篇关于从 setup.py 运行声纳扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆