import jenkins
import datetime
import xlwt
now = datetime.datetime.now()
zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,
microseconds=now.microsecond)
yestday = zeroToday - datetime.timedelta(seconds=1)
server = jenkins.Jenkins('http://192.168.10.1:8080', username='name', password='passwd')
all_jobs_li = server.get_all_jobs()
count = 0
book = xlwt.Workbook()
sheet = book.add_sheet('jobname')
sheet.write(0, 0, 'jobname')
sheet.write(0, 1, 'projectname')
sheet.write(0, 2, 'codepath')
sheet.write(0, 3, '负责人')
for item in all_jobs_li:
a = item['name']
if a.startswith('Timer'):
count += 1
last_build_number = server.get_job_info(item['name'])['lastCompletedBuild']['number']
build_info = server.get_build_info(item['name'], last_build_number)
sss = build_info['timestamp']
result = build_info['result']
buildNumber = build_info['number']
# print(build_info['number'])
dateArray = datetime.datetime.utcfromtimestamp(float(sss) / 1000) + datetime.timedelta(hours=8)
eob = server.get_build_info(item['name'], build_info['number'])
# for i in eob:
# print(i)
jobname = item['name']
print(jobname)
yyy= eob['actions']
# print(jobname)
ls2 = ['causes']
ls4 =['parameters']
ls3=[]
ls5 = []
for i in yyy:
if ls2[0] in i:
ls3.append(i)
tttt=ls3[0]
ggg=tttt['causes']
jjjj=ggg[0]
builduser = jjjj['shortDescription']
# queryset = Jrate.objects.all().filter(pub_date__range=(start_date, end_date))
for param in yyy:
if ls4[0] in param:
ls5.append(param)
rrrr = ls5[0]
kkk =rrrr['parameters']
arr = []
for itm in kkk:
# print(itm['name'])
arr.append(itm['name'])
if itm['name'] == 'projectname':
# print(itm['value'])
pname =itm['value']
# print(pname)
if itm['name'] == 'code_path':
cname =itm['value']
# print(cname)
if "code_path" in arr:
cname = cname
else:
cname = "kong"
print(count)
# print(jobname + ":" + pname + ":" + cname)
sheet.write(count,0, jobname)
sheet.write(count, 1, pname)
sheet.write(count, 2, cname)
book.save('TimerInfo.xls')