从 setup.py 运行声纳扫描仪

2024-01-13

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

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

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

但希望能够打电话

C:> python setup.py sonar

或者类似的东西

Edit:

为了让它工作,我将以下代码放入我的 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)

以及对 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
}

See the docs https://docs.python.org/2/distutils/apiref.html#creating-a-new-distutils-command和例子(1 https://stackoverflow.com/a/1712544/7976758, 2 https://seasonofcode.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy.html).

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 setup.py 运行声纳扫描仪 的相关文章

随机推荐