从setup.py运行声纳扫描仪

时间:2017-08-31 13:15:36

标签: python sonarqube setuptools sonarqube-scan

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

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

    <artifactId>itaras</artifactId>
 <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

 <name>itaras</name>
 <url>http://maven.apache.org</url>


   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>


    <build>
   <plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.2</version>
 </plugin>

   <plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <version>2.3.2</version>
        <configuration>
            <finalName>MyEarFile</finalName>
            <version>5</version>
             <generatedDescriptorLocation>$D:/itaras
  /ITARAS_Branch_3Aug2017/itaras/WebContent
 </generatedDescriptorLocation>
            <modules>
                    <webModule>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <uri>appITARAS.war</uri>
                    <bundleFileName>appITARAS.war</bundleFileName>
                    <contextRoot>/ITARAS</contextRoot>
                    </webModule>
               </modules>
           </configuration>
       </plugin>          
   </plugins>
  </build>
   <groupId>itaras</groupId>
  <profiles>
   <profile>
 <modules>
   <module>itaras-ear</module>
    <module>itaras-war</module>
    <module>?</module>
 </modules>
 </profile>
  </profiles>

 <dependencyManagement>
    <dependencies>
    <dependency>
        <groupId>itaras</groupId>
        <artifactId>itaras-war</artifactId>
        <version>${project.version}</version>
    </dependency>
    </dependencies>
   </dependencyManagement>
   </project>

但希望能够致电

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

或类似的东西

修改

为了实现这一点,我将以下代码放在我的setup.py文件中

一堂课:

C:> python setup.py sonar

和更改cmdclass和command_options

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={'sonar' : SonarPython},

command_options={
    'sonar': {
        'project_key': ('setup.py', 'TL:python'),
        'source_dir': ('setup.py',  'mypackage')       
         }                     
    },

您可以省略command_options条目,只需将它们作为

传递给命令行
python setup.py sonar

2 个答案:

答案 0 :(得分:1)

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

$recipient .= ',name@example.com';

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

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

请参阅docs和示例(12)。

答案 1 :(得分:-1)

不幸的是,这不可用。