Python实现获取sonarqube数据
作者:Michaelwubo 发布时间:2023-02-26 04:38:30
标签:Python,sonarqube,数据
1.sonarqube是一款代码分析的工具,通过soanrScanner扫描后的数据传递给sonarqube进行分析
2.sonarqube社区版没有对c++类代码的分析,但是可以找到一个开源的包,安装即可,扫描的话可以使用cppcheck来进行扫描
安装python对于sonarqube的api包:python-sonarqube-api
建立sonarqube连接
from sonarqube import SonarQubeClient
sonar = SonarQubeClient(
sonarqube_url="http://192.168.xx.xx:9000",
username='admin',
password='admin'
)
使用:建议大家先参考sonarqube的python-api
Welcome to SonarQube Client with Python’s documentation! — SonarQube Client with Python 1.3.5 documentation
使用示例
# 通过项目名称获取id
# 传递参数:创建分析配置文件时候的项目名称
component = sonar.components.get_project_component_and_ancestors("python_test")
# 获取任务
# 参数1:上一步获取的component值
# 参数2:逗号分割的状态值
tasks1 = sonar.ce.search_tasks(
componentId="AX5v36mo0Npud3J2od3a",
status="FAILED,CANCELED,PENDING,IN_PROGRESS,SUCCESS"
)
# 获取所有项目
projects = list(sonar.projects.search_projects())
# 获取这个项目下最近一次分析任务的详情
"""
componentKeys:某一个项目名称
types:参数类型
CODE_SMELL==异常
BUG==bug
VULNERABILITY==漏洞
SECURITY_HOTSPOT==
"""
issues2 = list(sonar.issues.search_issues(componentKeys="python_s", types="CODE_SMELL"))
通过metricKeys参数获取这个项目中需要的值
# 参数1:component 项目名称
# 参数2:metricKeys 想要获取的某个值,逗号分割
component_data = sonar.measures.get_component_with_specified_measures(
component="python_test",
metricKeys="functions,classes"
)['component']['measures']
# 目前已收集的值和含义
'''
ncloc==总代码长度
ncloc_language_distribution==其中每种语言的行数
bugs==bug数
vulnerabilities==漏洞数
code_smells==异常数
duplicated_lines_density==重复度百分比
coverage==代码覆盖率百分比
files==文件数量
functions==方法数量
classes==类数量
'''
[root@localhost data]# cat wubo.py
#!/bin/python3
# encoding: utf-8
from sonarqube import SonarQubeClient
from operator import itemgetter
import json
import csv
import os
import time
import shutil
class SonarQube:
def __init__(self,url,username="admin",password="123456aA") -> None:
username = username
password = password
sonarqube_url = url
self.client = SonarQubeClient(username = username,password = password,sonarqube_url = sonarqube_url)
def getProjects(self):
"""获取项目列表"""
projects = self.client.projects.search_projects().get("components")
return projects
def getIssues(self,jettech_project_name,issues_type):
"""获取项目问题列表"""
#projects = self.client.issues.search_issues(componentKeys="jettoloader-pressurecommon",types="BUG",s="FILE_LINE",resolved="false",ps=1,organization="default-organization",facets="authors",additionalFields="_all")
projects = self.client.issues.search_issues(componentKeys=jettech_project_name,types=issues_type,s="FILE_LINE",resolved="false",ps=1,organization="default-organization",facets="authors",additionalFields="_all")
list_issues = projects["facets"][0]["values"]
#list_issues_name_count = []
#for list_issues_item in list_issues:
# list_issues_name_count.append(list_issues_item["val"])
# list_issues_name_count.append(list_issues_item["count"])
#print(list_issues)
#list_issues[0]["val"])
#list_issues[0]["count"])
return list_issues
def getMessages(self,component):
""" 获取项目各个参数数据"""
#metricKeys = "alert_status,bugs,,vulnerabilities,security_rating,code_smells,duplicated_lines_density,coverage,ncloc"
metricKeys = "bugs,,vulnerabilities"
messages = []
messages.append(self.client.measures.get_component_with_specified_measures(component, metricKeys))
return messages[0]
def getMeasures(self,component,message):
measures = []
measures.insert(0,component)
measures_all = message.get("component").get("measures")
for measure_item in measures_all:
measures_type = measure_item.get("metric")
if "bugs" == measures_type:
measures.insert(1,measure_item.get("value"))
if "vulnerabilities" == measures_type:
measures.insert(2,measure_item.get("value"))
return measures
class CSV:
def __init__(self,filepath,filename) -> None:
self.filepath = filepath
self.filename = filename
def csv_write(self,project_measure_all):
#header = ['1project_name','2bugs','3vulnerabilities']
with open(self.filepath+"/"+self.filename, 'a') as file_obj:
writer = csv.writer(file_obj)
#writer.writerow(header)
for p in project_measure_all:
writer.writerow(p)
def csv_sort(self):
datas=[]
with open(self.filepath+"/"+self.filename, 'r') as f:
table = []
for line in f:
line = line.replace("\n","").replace("\r","")
col = line.split(',')
col[0] = str(col[0])
col[1] = col[1].strip("\n")
table.append(col)
table_sorted = sorted(table, key=itemgetter(0), reverse=False) # 精确的按照第1列排序
for row in table_sorted:
datas.append(row)
f.close()
with open(self.filepath+"/"+self.filename,"w", newline='') as csvfile:
writer = csv.writer(csvfile)
for data in datas:
writer.writerow(data)
csvfile.close()
def csv_insert(self):
header = 'project_name,bugs,vulnerabilities'
with open(self.filepath+"/"+self.filename, 'r+', encoding='utf-8') as f:
content = f.read()
f.seek(0, 0)
f.write(header + '\n' + content)
f.close()
def csv_delete(self):
if (os.path.exists(self.filepath)):
shutil.rmtree(self.filepath,ignore_errors=True)
def csv_sum(self):
with open(self.filepath+"/"+self.filename) as fin:
readline_item=fin.readline()
total_bug_api = 0
total_bug_manager = 0
total_bug_loader = 0
total_bug_ui = 0
total_vulnerabilities_api = 0
total_vulnerabilities_manager = 0
total_vulnerabilities_loader = 0
total_vulnerabilities_ui = 0
for row in csv.reader(fin):
row_project_name=row[0].split("-")[0]
if "jettoapi" == row_project_name:
total_bug_api += int(row[1])
total_vulnerabilities_api += int(row[2])
if "jettoloader" == row_project_name:
total_bug_loader += int(row[1])
total_vulnerabilities_loader += int(row[2])
if "jettomanager" == row_project_name:
total_bug_manager += int(row[1])
total_vulnerabilities_manager += int(row[2])
if "jettoui" == row_project_name:
total_bug_ui += int(row[1])
total_vulnerabilities_ui += int(row[2])
fin.close()
header_kong = ['','','']
header_api = ['jettoapi','bug总数',str(total_bug_api),'vulnerabilities总数',str(total_vulnerabilities_api)]
header_loader = ['jettoloader','bug总数',str(total_bug_loader),'vulnerabilities总数',str(total_vulnerabilities_loader)]
header_manager = ['jettomanager','bug总数',str(total_bug_manager),'vulnerabilities总数',str(total_vulnerabilities_manager)]
header_ui = ['jettoui','bug总数',str(total_bug_ui),'vulnerabilities总数',str(total_vulnerabilities_ui)]
with open(self.filepath+"/"+self.filename, 'a') as file_obj:
writer = csv.writer(file_obj)
writer.writerow(header_kong)
writer.writerow(header_api)
writer.writerow(header_loader)
writer.writerow(header_manager)
writer.writerow(header_ui)
file_obj.close()
class SCP:
def __init__(self,localdir,remoteip,remotedir) -> None:
self.localdir = localdir
self.remoteip = remoteip
self.remotedir = remotedir
def scp_operate(self):
os.system('scp -r "%s" "%s:%s"' % (self.localdir, self.remoteip, self.remotedir))
def main():
sonarQube = SonarQube(url='http://172.16.10.1:9000/')
all_project_info = sonarQube.getProjects()
project_measure_all=[]
project_issues_all=[]
for project_info in all_project_info:
component = project_info.get("key")
message = sonarQube.getMessages(component)
measure = sonarQube.getMeasures(component,message)
project_issues=[]
list_issues_s = sonarQube.getIssues(component,"BUG")
project_issues.append(component)
for list_issues_s_item in list_issues_s:
project_issues.append(list_issues_s_item["val"])
project_issues.append(list_issues_s_item["count"])
project_measure_all.extend([tuple(measure)])
project_issues_all.extend([tuple(project_issues)])
print([tuple(measure)])
#print(project_issues_all)
filepath=time.strftime("%Y-%m-%d")
filename="jettech_sornar_"+filepath+"_projects.csv"
filename_isuess="jettech_sornar_"+filepath+"_projects_iseuss.csv"
if not os.path.exists(filepath):
os.makedirs(filepath)
if os.path.exists(filepath+"/"+filename):
os.remove(filepath+"/"+filename)
if os.path.exists(filepath+"/"+filename_isuess):
os.remove(filepath+"/"+filename_isuess)
csv_obj = CSV(filepath=filepath,filename=filename)
csv_obj_isuess = CSV(filepath=filepath,filename=filename_isuess)
csv_obj.csv_write(project_measure_all)
csv_obj_isuess.csv_write(project_issues_all)
csv_obj.csv_sort()
csv_obj.csv_insert()
csv_obj.csv_sum()
localdir=filepath
remoteip="192.168.1.99"
remotedir="/product/gohttpserver/data/sornar/"
scp_obj = SCP(localdir,remoteip,remotedir)
scp_obj.scp_operate()
csv_obj.csv_delete()
if __name__== "__main__" :
main()
来源:https://blog.csdn.net/Michaelwubo/article/details/130637317


猜你喜欢
- 前言??在vue项目中我们常常需要用到computed和watch,那么我们究竟在什么场景下使用computed和watch呢?他们之间又有
- 我就废话不多说了,大家还是直接看代码吧~func ReadLine(fileName string) ([]string,error){f,
- python中for循环用于针对集合中的每个元素的一个代码块,而while循环能实现满足条件下的不断运行。使用while循环时,由于whil
- 相比SQL Server 2000提供的FOR XML查询,SQL Server 2005版本对现有功能增强的基础上增加了不少新功能,最为吸
- 使用QMoive方法实现导入库文件from PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.
- 背景简介ImageAI是一个面向计算机视觉编程的Python库,支持最先进的机器学习算法。主要图像预测,物体检测,视频对象检测与跟踪等多个应
- 前言Qt 自带的工具提示样式不太好看,就算加了样式表也时不时会失效,同时工具提示没有阴影,看起来就更难受了。所以本篇博客将会介绍自定义工具提
- 我就废话不多说了,大家还是直接看代码吧~'''Created on 2018-4-16'''
- 本文做的是基于opencv将视频帧转成图片输出,由于一个视频包含的帧数过多,经常我们并不是需要它的全部帧转成图片,因此我们希望可以设置每隔多
- 报错如下:TabError: inconsistent use of tabs and spaces in indentation我推荐一种
- 一般情况下,mysql会默认提供多种存储引擎,可以通过下面的查看:1)查看mysql是否安装了innodb插件。通过下面的命令结果可知,已经
- 编这个程序是想过节过年,一些重要的纪念日,给亲戚好友发祝福之类的,但要凌晨0点才显得比较有诚意,可我又比较贪睡,常常忘了,所以就有了编个微信
- python序列类型包括哪三种python序列类型包括:列表、元组、字典列表:有序可变序列创建:userlist = [1,2,3,4,5,
- 1.问题描述当我们在实用ElementUI组件完成项目的时候可能会遇到这样的需求,比如:新建一个活动,需要定义活动的时间范围;因此我们在新建
- //方法1:$ip = $_SERVER["REMOTE_ADDR"];echo $ip;//方法2:$user_IP
- 工作中偶尔需要做客流分析,用pyplot 库绘图。一般情况下, x 轴刻度默认显示为数字。例如:我希望x 轴刻度显示为星期日期。查询pypl
- 今天在做项目时,遇到了需要创建JavaScript对象的情况。所以Bing了一篇老外写的关于3种创建JavaScript对象的文章,看后跟着
- Python 调用JS文件中的函数方法如下1、安装PyExecJS第三方库2、导入库:import execjs3、调用JS文件中的方法Pa
- 标题显而易见,要说两种情况:重新打开页面或者返回某个页面时滚动到上次离开时的位置,以及不滚动保持在顶部。滚动这也有两种情况:页面重新打开,与
- 1.基本介绍在OpenCV中,图像通道是按照 B 通道→G 通道→R 通道的顺序存储的。在图像处理过程中,可以根据需要对通道进行拆分和合并。