python模式匹配与字符串连接

时间:2018-02-21 11:08:20

标签: python python-2.7 jenkins

我想从jenkins job>下载构建文件;上次成功建造。 文件名如下: DevPackage-123.pkg其中123是最后一个成功的内部版本号。 在我使用urllib的python脚本中,如何将此文件名作为常规变量传递。 我的意思是 filename =" DevPackage - " +< 3位> +" .pkg" 运行脚本时,我不知道这3个数字代表内部版本号。我正在寻找文件名中添加3个数字的模式。请帮忙!

if jenkins == "branch" and build == "lastsuccessful":
    baselink = "http://" + credentials + "XXX.XXX.XXX.XXX/job/My_Build_Job/lastSuccessfulBuild/artifact/results/"
    filelist = ["DevPackage-XXX.pkg","MIBS.zip","udpServer.sh"]

for index in range(len(filelist)):
    urllib.urlretrieve(baselink+filelist[index],newdir+filelist[index])
    print filelist[index]+" Copied!"

1 个答案:

答案 0 :(得分:0)

您首先要从Jenkins获得最后一次成功构建的作业:

import shutils
import os

import requests

res = requests.get("http://{}@XXX.XXX.XXX.XXX/job/My_Build_Job/lastSuccessfulBuild/buildNumber".format(credentials))
build_number = res.text

之后,你可以获得最后的文物内容:

baselink = "http://{}@XXX.XXX.XXX.XXX/job/My_Build_Job/lastSuccessfulBuild/artifact/results/{}"
filelist = ["DevPackage-{}.pkg".format(build_number),"MIBS.zip","udpServer.sh"]
for filename in filelist:
    res = requests.get(baselink.format(credentials, filename), stream=True)
    if res.status_code == 200: 

        with open(os.path.join(newdir, filename), "wb") as localfile:
            shutil.copyfileobj(res.raw, localfile)