import pymssql
import sys
import datetime
from string import Template
cxn = pymssql.connect(host=r'ip', user=r'username', password=r'pwd', database=r'NCI',charset="utf8")
cur = cxn.cursor()
#conn.commit() # you must call commit() to persist your data if you don't set autocommit to True
start_list=list()
Job_name_list=list()
Job_name_dict=dict()
Job_Build_Number_dict=dict()
new_Build_list=list()
new_Build_dict=dict()
new_Build_Id_dict=dict()
runtime_list=list()
sub_job_list=list()
entry_build_id=dict()
## first step execute SQL to export list of duplicate Build_ID
cur.execute('SELECT builds.Build_ID, Job_Name, Build_Number, Start_Time \
FROM Jenkins_Builds builds INNER JOIN ( \
SELECT Build_ID FROM Jenkins_Builds \
WHERE Build_ID IN (SELECT Build_ID FROM CII_Builds) \
GROUP BY Build_ID HAVING Count(Build_ID) > 1 \
) duplicate ON builds.Build_ID = duplicate.Build_ID \
ORDER BY builds.Job_Name, builds.Build_Number ')
for user in cur.fetchall():
start_list.append(user)
for x in start_list:
if Job_name_dict.has_key(x[1]):
Job_name_dict[x[1]].append(x)
Job_name_dict[x[1]]=sorted(Job_name_dict[x[1]],key=lambda x : x[2])
else:
Job_name_dict[x[1]] = list()
Job_name_dict[x[1]].append(x)
if entry_build_id.has_key(x[1]):
continue
else:
entry_build_id[x[1]] = list()
entry_build_id[x[1]].append(x[0])
for x in start_list:
if Job_Build_Number_dict.has_key(x[1]):
Job_Build_Number_dict[x[1]].append(x[2])
else:
Job_Build_Number_dict[x[1]] = list()
Job_Build_Number_dict[x[1]].append(x[2])
##second 2 pick one duplicated Build_ID, (with job_name & build_number in hand),
for a in Job_name_dict:
i=1
for x in Job_name_dict[a]:
lenx=len(Job_name_dict[a])
if i<lenx:
if new_Build_dict.has_key(Job_name_dict[a][i][1]):
new_Build_dict[Job_name_dict[a][i][1]].append(Job_name_dict[a][i][2])
else:
new_Build_dict[Job_name_dict[a][i][1]] = list()
new_Build_dict[Job_name_dict[a][i][1]].append(Job_name_dict[a][i][2])
try:
str_duplicate="P_DUPUUID '%s', @Do_Delete=1" % Job_name_dict[a][i][0]
except:
print 'none'
cur.execute(str_duplicate)
i=i+1
else:
break
##second 3 get the new Build_ID
for x in new_Build_dict:
for y in new_Build_dict[x]:
str_build="select Build_ID from Jenkins_Builds where Job_Name='%s' and Build_Number=%d" % (x,y)
cur.execute(str_build)
for user in cur.fetchall():
if new_Build_Id_dict.has_key(x):
new_Build_Id_dict[x].append(user)
else:
new_Build_Id_dict[x] = list()
new_Build_Id_dict[x].append(user)
new_Build_list.append(user)
##step4 get the runtime Platform ,Language and Start_Time
for x in new_Build_list:
str_new_Build="SELECT jenkins.Display_Name, Build_Type, Platform, Language, jenkins.Start_Time FROM CII_Builds cii \
INNER JOIN Jenkins_Builds jenkins ON jenkins.Build_ID = cii.Build_ID \
WHERE cii.Build_ID = '%s' \
" % x
cur.execute(str_new_Build)
for user in cur.fetchall():
runtime_list.append(user)
##step 5 find sub job by platform and start_time
for x in runtime_list:
mytime = str(x[4]).split(".")[0]
sub_job_build="SELECT jenkins.Start_Time, jenkins.Build_Number, jenkins.Duration, jenkins.Result ,* FROM Jenkins_Builds jenkins\
WHERE Job_Name = '%s' \
AND Start_Time >= '%s' \
ORDER BY jenkins.Build_Number\
"%(x[0],mytime)
cur.execute(sub_job_build)
for user in cur.fetchall():
sub_job_list.append(user)
## step 6 create relation
for x in entry_build_id:
for m in entry_build_id[x]:
cur.execute('DELETE FROM CII_Build_Detail WHERE Entry_Build_ID = %s',m)
#print 'ok'
for y in new_Build_Id_dict[x]:
cur.execute("INSERT INTO CII_Build_Detail SELECT j.Build_ID, %s, 'linux', 'en', j.Duration, j.Result \
FROM Jenkins_Builds j WHERE j.Build_ID = %s ",(m,y))
#print 'right'
cur.close()
cxn.commit()
cxn.close()
python中pymssql模块操作SQL Server
最新推荐文章于 2024-04-13 20:36:18 发布