#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
#系统路径
#sys.path.append(r"/home/jenkins0406/jenkins/sonar_script")
sys.path.append(r"E:\sonar0829")
import pymysql,os,sys
from jinja2 import FileSystemLoader,Environmentdef select_project_uuid(project_name):db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")cursor = db.cursor()select_p_uuid="SELECT project_uuid,kee FROM projects WHERE `name`= '%s'" %(project_name)cursor.execute(select_p_uuid)result = cursor.fetchone()p_uuid = result[0]projectKey = result[1]db.close()return(p_uuid, projectKey)def select_total_info(p_uuid):total_info=[]# 使用cursor()方法获取操作游标db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")cursor = db.cursor()select_p_links = "SELECT text_value FROM project_measures WHERE text_value LIKE 'java=%' and component_uuid=" + "\'" + p_uuid + "\'"cursor.execute(select_p_links)p_links = cursor.fetchone()[0].split("=")[1]sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =%s AND  status != 'CLOSED'"for leak in [2,3,1]:search_data = sql_info %(p_uuid, leak)cursor.execute(search_data)total_info.append(cursor.fetchone()[0])db.close()return p_links,total_infodef select_bugs(p_uuid):bugs=[]db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")cursor = db.cursor()sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =2 AND  status != 'CLOSED' AND severity ='%s'"for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:search_data=sql_info  % (p_uuid,leak)cursor.execute(search_data)bugs.append(cursor.fetchone()[0])db.close()return bugsdef select_leaks(p_uuid):leaks=[]db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")cursor = db.cursor()sql_info = "SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =3 AND status != 'CLOSED' AND severity ='%s'"for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:search_data=sql_info  % (p_uuid,leak)cursor.execute(search_data)leaks.append(cursor.fetchone()[0])db.close()return leaksdef select_bad_tastes(p_uuid):tastes=[]db = pymysql.connect(host="192.168.90.198", port=3306, user="sonar", passwd="sonar", db="sonar")cursor = db.cursor()sql_info="SELECT count(*) FROM issues WHERE project_uuid='%s' and issue_type =1 AND status != 'CLOSED' AND  severity ='%s'"for leak in ['BLOCKER','CRITICAL',"MAJOR",'MINOR','INFO']:search_data=sql_info  % (p_uuid,leak)cursor.execute(search_data)tastes.append(cursor.fetchone()[0])return tastesdb.close()curpath = os.getcwd()
table_tem_name="table.html"
def generate_errmsg_table(s_lines="", total_data=[], bugs=[],leaks=[],tastes=[],report_url=""):env = Environment(loader=FileSystemLoader(curpath, 'utf-8'))  # 创建一个包加载器对象template = env.get_template(table_tem_name)html_content = (template.render(lins=s_lines,total_data=total_data, bugs=bugs,leaks = leaks,tastes=tastes,report_url=report_url))fh = open(report_html_path, 'w')fh.write(html_content)fh.close()#外部传参
#project_name = sys.argv[1]
project_name = 'healthcode-sonar'
report_html_path=project_name+".html"
p_uuid, projectKey=select_project_uuid(project_name)
s_lines,total_data=select_total_info(p_uuid)
bugs=select_bugs(p_uuid)
leaks=select_leaks(p_uuid)
tastes=select_bad_tastes(p_uuid)
report_url="http://192.168.90.198:9009/dashboard?id=%s" %(projectKey)
generate_errmsg_table(s_lines,total_data,bugs,leaks,tastes,report_url)

1、执行环境 python 3.6

2、写入的模板html,table.html,如下

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<body>
<p style="font-weight:bold;">一、总体情况:</p>
<ul>
<li style="font-weight:bold;">整体运行情况:扫描代码行数:<span style="color:blue">{{lins}}</span>, bugs:<span style="color:red">{{total_data[0]}}</span>, 漏洞:<span style="color:red">{{total_data[1]}}</span>, 坏味道:<span style="color:red">{{total_data[2]}}</span></li>
<li style="font-weight:bold;">URL地址:<a style="font-weight:bold;" href={{report_url}} >{{report_url}}</a></li>
</ul>
<p style="font-weight:bold;">二、错误信息详情:</p>
<table border="1" cellpadding="10" width="540" height="120"><tr ><th></th><th>阻断</th><th>严重</th><th>主要</th><th>次要</th><th>提示</th><th>总数</th></tr><tr bgcolor=#ECFFFF><td>bugs</td><td align="center">{{bugs[0]}}</td><td align="center">{{bugs[1]}}</td><td align="center">{{bugs[2]}}</td><td align="center">{{bugs[3]}}</td><td align="center">{{bugs[4]}}</td><td align="center" style="color:red">{{total_data[0]}}</td></tr><tr bgcolor=#D2E9FF><td>漏洞</td><td align="center">{{leaks[0]}}</td><td align="center">{{leaks[1]}}</td><td align="center">{{leaks[2]}}</td><td align="center">{{leaks[3]}}</td><td align="center">{{leaks[4]}}</td><td align="center" style="color:red">{{total_data[1]}}</td></tr><tr bgcolor=#ECFFFF><td>坏味道</td><td align="center">{{tastes[0]}}</td><td align="center">{{tastes[1]}}</td><td align="center">{{tastes[2]}}</td><td align="center">{{tastes[3]}}</td><td align="center">{{tastes[4]}}</td><td align="center" style="color:red">{{total_data[2]}}</td></tr>
</table>
<br></br>
</body>
</html>

3、执行命令

python3 sonar.py healthcode-sonar

healthcode-sonar是你jenkins中创建的项目名称

4、执行结果

python脚本 sonar报告相关推荐

  1. python运维脚本部署jdk_Jenkins自动执行Python脚本,并输出测试报告

    这段时间,在家时间自由(除了睡觉8小时,其他时间都在工作和学习),有大把的时间实操练习一些硬技能: 今天,更新一篇利用Jenkins这套框架,调用Python自动化脚本,并输出测试报告的手把手实操文章 ...

  2. 【Python】如何在Excel中调用Python脚本,实现数据自动化处理

    这次我们会介绍如何使用xlwings将Python和Excel两大数据工具进行集成,更便捷地处理日常工作. 说起Excel,那绝对是数据处理领域王者般的存在,尽管已经诞生三十多年了,现在全球仍有7.5 ...

  3. VS2017 C++工程 执行python脚本

    我解决了哪怕很小的一个问题,我也想记录下来来见证我的经历. 背景: 一.使用libhuru库生成pdf报告 最近参与一些测试工作,希望测试结束后能够根据测试得到的数据和图像自动生成测试报告,最开始调研 ...

  4. python db2查询_如何将DB2查询转换为python脚本

    python db2查询 Many companies are running common data analytics tasks using python scripts. They are a ...

  5. python自动化数据报告_如何:使用Python将实时数据自动化到您的网站

    python自动化数据报告 This tutorial will be helpful for people who have a website that hosts live data on a ...

  6. jenkins 手动执行_想知道如何用Jenkins自动执行Python脚本输出测试报告?

    前言在用python做自动化测试时,我们写好代码,然后需要执行才能得到测试报告,这时我们可以通过 Jenkins 来进一步完成自动化工作.借助Jenkins,我们可以结合 Git/SVN 自动拉取代码 ...

  7. 一切尽在掌控之中:这个Python脚本,让工作自动向你汇报进度!

    图源:unsplash 笔者经常编写Python脚本来进行数据处理.数据传输和模型训练.随着数据量和数据复杂性的增加,运行脚本可能需要一些时间.在等待数据处理完成时可以同时做一些其他工作. 很多人学习 ...

  8. SQL Server中数据透视表的Python脚本

    This article talks about Python scripts for creating pivot tables in multiple ways. 本文讨论了以多种方式创建数据透视 ...

  9. python中的tail()_让VASP实现固定应力张量计算的python脚本

    最近在研究指定的应力张量下晶体缺陷的行为.本来想用VASP来进行相应的DFT模拟.但查了半天,发现VASP并不支持指定应力的计算(除非是isotropic的应力). 具体来说,你可以输入一个应变,VA ...

  10. go调用python脚本_谁能取代Python?我使用Go来部署机器学习模型的原因

    全文共1881字,预计学习时长7分钟 图源:unsplash 毋庸置疑,Python是如今最受欢迎的机器学习语言.虽然机器学习框架使用CUDA C / C ++之类的语言进行实际运算,但它们都提供了P ...

最新文章

  1. 一次 MySQL 千万级大表的优化过程
  2. thymeleaf 使用页面报错_异常处理-SpringBoot中thymeleaf对应前台页面大于号\小于号使用问题...
  3. html怎么把图片放到数组,HTML5中的图像数组
  4. Linux串口应用编程
  5. oracle 12.2.0.1 搭建 active dataguard
  6. 同时多个axios请求_用 React+Antd 封装 Axios 实现全局 Loading 效果
  7. 过长内容分成了多次发送 问题 LengthFieldBasedFrameDecoder使用
  8. git 常用操作,撤销修改
  9. Oracle 无备份情况下的恢复--控制文件/数据文件
  10. 在浏览器中输入url地址 - 显示主页的过程
  11. STM32——HAL版——串口发送字符串函数
  12. java开发实训报告范文_java实训报告总结
  13. python安装Jpype
  14. 2020年小米高级 PHP 工程师面试题
  15. 计算机文件大小排序工具,如何对电脑文件夹进行排序
  16. 极光短信推送-java使用
  17. 网络核心交换机和普通交换机有什么区别?
  18. js写给定k个字符串数组,从这k个数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合。(不要使用库方法)
  19. 表格css样式 ——表格背景,隔行变色,触摸表格变色
  20. 目的地址,源地址防火墙双向nat转换

热门文章

  1. Axure 安装图标字体元件库
  2. C#+.Net使用RemObjects建立客户端服务端
  3. matlab 3.BPF封装 巴特沃斯带通滤波器
  4. 晶振为什么不封装进芯片内部?
  5. 解读核磁共振射频系统架构、模块结构及功能介绍
  6. [笔记] 《Windows网络编程(第2版)》
  7. QQ客户端聊天窗口输入/自动变成表情 - 解决方案
  8. 利用.jou文件将建筑shp文件导入Gambit方法
  9. android 开发书签大全
  10. 搜索。深搜学习。深度优先搜索之数字全排列。nyoj,组合数