【问题标题】:Running sonar-scanner from setup.py从 setup.py 运行声纳扫描仪
【发布时间】:2018-02-09 11:07:34
【问题描述】:

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

我使用以下命令调用扫描仪

C:> sonar-scanner -Dsonar.projectKey=TL:python -Dsonar.sources=mypackage

但希望能打电话

C:> python setup.py sonar

或类似的东西

编辑:

为了让它工作,我将以下代码放入我的 setup.py 文件中

一个类:

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 的更改

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'

【问题讨论】:

    标签: python sonarqube setuptools sonarqube-scan


    【解决方案1】:

    你可以新建一个 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
    }
    

    查看docs 和示例(12)。

    【讨论】:

    • 谢谢,我把我的实现放在上面
    【解决方案2】:

    很遗憾,这不可用。

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 1970-01-01
      • 2020-08-30
      • 2017-08-19
      • 2017-04-27
      • 2019-10-27
      • 2021-01-10
      • 2018-09-27
      • 2017-10-26
      相关资源
      最近更新 更多