#https://jenkins-ftl-rglab.xm.citrite.net/view/All/job/XMS/job/Automation-Framework/job/Develop/lastSuccessfulBuild/api/xml
import ConfigParser
import os
Config = ConfigParser.ConfigParser()
Config.read("upload.properties")



def createfolder(foldername):
    if not os.path.isdir(foldername):
        os.mkdir(foldername)

def downloader():
    createfolder("archive")
    for section_name in Config.sections():
        jobName=[]
        jobName.append((Config.items(section_name))[0])
        print ("Job Name",jobName[0][1])
        buildpath=[]
        buildpath.append((Config.items(section_name))[1])
        print ("Jenkin Build Path : " ,buildpath[0][1])
        fileName=buildpath[0][1].split("/")[-1]
        print ("File Name : ",fileName)
        branch=[]
        branch.append((Config.items(section_name)))
        print ("Branch ==>" ,branch[0][2][1])
        username="svcacct_mpgci:44385114f30bc605507fea0e9d7c14fb"
        download='curl --user '+username+' https://jenkins-ftl-rglab.xm.citrite.net/job/XMS/job/'+jobName[0][1]+'/job/Master/lastSuccessfulBuild/artifact/'+buildpath[0][1]+' --output archive/'+fileName
        print ("Download",download)
        # cfgfile = open("upload.properties",'w')
        # Config.set('NIGHTWATCH','Age', 50)
        # Config.write(cfgfile)
        # cfgfile.close()
        # retval = os.system(download)


if __name__ == '__main__':
    downloader()









