python-导出Jenkins任务

from jenkins import Jenkins
import xlwt,re,html

jen = Jenkins(url="访问地址", username="账号", password="密码")
all_jobs = jen.get_all_jobs()

def export_excel():
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('test',cell_overwrite_ok=True)
style = xlwt.XFStyle()
# 设置字体
font = xlwt.Font()
style.font = font
# 单元格对齐
alignment = xlwt.Alignment()
# 水平对齐方式和垂直对齐方式
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
# 自动换行
alignment.wrap = 1
style.alignment = alignment
ws.write(0, 0, '_class')
ws.write(0, 1, 'name')
ws.write(0, 2, 'url')
ws.write(0, 3, 'color')
ws.write(0, 4, 'fullname')
ws.write(0, 5, 'gitUrl')
ws.write(0, 6, 'shell')
try:
for i in range(len(all_jobs)):
ws.write(i+1,0,all_jobs[i].get('_class'))
ws.write(i+1,1,all_jobs[i].get('name'))
ws.write(i+1,2,all_jobs[i].get('url'))
ws.write(i+1,3,all_jobs[i].get('color'))
ws.write(i+1,4,all_jobs[i].get('fullname'))
print(all_jobs[i].get('name'))
name_list = ['compile', # name_list是我拉取到所有应用里请求git地址和shell脚本时报错的应用名'knowledge',
'database',
'docker-images','python-runtime-build']
if all_jobs[i].get('name') in name_list:
pass
else:
conf = jen.get_job_config(name=all_jobs[i].get('name'))
url = re.finditer(r"<url>.*?</url>", conf)
for match in url:
gitUrl = match.group()
ws.write(i+1, 5, gitUrl.lstrip('<url>').rstrip('</url>'))
comm = re.finditer(r"<command>[sS]*?</command>", conf)
for match in comm:
shellComm = match.group()
#shellStr = shellComm.lstrip('<command>').rstrip('</command>') # 去除标签
shellstr = re.sub(r"<command>","",shellComm)
shellstr = re.sub(r"</command>","",shellstr)
shellstr = re.sub(r'"', '"', shellstr) # 双引号乱码替换
#shellstr = re.sub(r'&apos;',"'",shellstr) # 单引号乱码替换
#shellstr = re.sub(r'&amp;',"&",shellstr)   # &乱码替换
ws.write(i + 1, 6, html.unescape(shellstr))
pass

#if ws.loc[i+1,'shell']is np.nan:

buildScript = re.finditer(r"<preScript>[sS]*?</preScript>", conf)
for match in buildScript:
bscript = match.group()
#shellStr1 = bscript.lstrip('<preScript>').rstrip('</preScript>') # 去除标签
shellStr1 = re.sub(r"<preScript>","",bscript)
shellStr1 = re.sub(r"</preScript>","",shellStr1)
shellStr1 = re.sub(r'"', '"', shellStr1) # 双引号乱码替换
#shellStr1 = re.sub(r'&apos;',"'",shellStr1) # 单引号乱码替换
#shellstr1 = re.sub(r'&amp;',"&",shellStr1)   # &乱码替换
ws.write(i + 1, 6, html.unescape(shellStr1))

except Exception as e:
print(e)
# 保存excel文件
wb.save('./test.xls')

if __name__ == '__main__':
export_excel()

原文地址:https://www.cnblogs.com/xy51/p/14233974.html