#!/usr/bin/python

import MySQLdb
import re
import os
import datetime
import os, sys
import urllib2
import urllib
import paramiko

from datetime import datetime, timedelta

db_stage=sys.argv[3]

jenkins_ip="135.249.27.140"
jname=sys.argv[1]
print jname
stage_name=sys.argv[2]
print stage_name



db = MySQLdb.connect("localhost","deenacld","alcatel01","pipeline_db")
cursor = db.cursor()

url="http://%s:8080/job/%s/lastBuild/api/python?pretty=true"%(jenkins_ip,jname)
print url
fRead = eval(urllib.urlopen(url).read())
console = fRead["number"]

print jname
print console

burl="http://%s:8080/job/%s/%s/api/python" %(jenkins_ip,jname,console)
print burl
jsonResponse1 = eval(urllib.urlopen(burl).read())
for i in range (0,len(jsonResponse1["actions"])):
	if 'parameters' in jsonResponse1["actions"][i]:
		for j in range (0,len(jsonResponse1["actions"][i]["parameters"])):		
			if jsonResponse1["actions"][i]["parameters"][j]["name"] == "BUILD" :
				build_tar=jsonResponse1["actions"][i]["parameters"][j]["value"]
				break

print build_tar
nbuild=re.search('SD_(.*).tar',build_tar)
jbuild=nbuild.group(1)
total_build=jsonResponse1["displayName"]
print total_build
total_build=total_build.rstrip()
nbuild1=total_build.split(" ")
abuild=nbuild1[0]
print abuild
if len(nbuild1) == 1:
	print "Not a rerun"
	jrerun="default"
else:
	rbuild=nbuild1[1]
	if rbuild == "RERUN":
		jrerun="RERUN"



console_log_url="http://%s:8080/job/%s/%s/consoleText"%(jenkins_ip,jname,console)
print console_log_url
console_log=urllib2.Request(console_log_url)
response = urllib2.urlopen(console_log)
response_text=response.read()
response_text=response_text.split("\n")
for k in range (len(response_text)-1,-1,-1):
	download_stage=re.search(r'Starting building: ' + stage_name + ' #(.*)',response_text[k])	
	if download_stage:
		print download_stage.group()
		
		download_stage_url="http://" + jenkins_ip + ":8080/job/" +stage_name + "/" + download_stage.group(1)	
		print download_stage_url
		
		duration_link=eval(urllib.urlopen(download_stage_url+ "/api/python").read())
		end_duration= duration_link["duration"]  
		print end_duration
		timestamp= duration_link["timestamp"]
		break


jstime = datetime.fromtimestamp(timestamp/1000)

##########end_time############

jetime = jstime + timedelta(seconds=end_duration/1000)
print jetime
print " gfshghr"

jobStatus=eval(urllib.urlopen(download_stage_url + "/api/python").read())
jobStatus=jobStatus["result"]
print "jobStatus"
print jobStatus
if jobStatus == "SUCCESS":
	jstatus = "PASS"
elif jobStatus == "FAILURE" or jobStatus == "ABORTED":
	jstatus = "FAIL"



cursor.execute("select MAX(s_no) FROM %s"%db_stage)
row = cursor.fetchone()
jid = row[0]
jid = jid + 1
print jname
print jbuild
print jstime
print jetime
print jstatus
print jrerun
if jbuild == abuild:
        try:
                cursor.execute("insert into %s VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s')" %(db_stage,jid, jname, jbuild, jstime, jetime, jstatus, jrerun))
                db.commit()
        except MySQLdb.Error, e:
                print(e)
else:
        print "This is not official proposal build.Hence skipping DB operation"



