【Python】從Jenkins下載最近一次成功構建的安裝包

awesome@qa發表於2018-11-16
#-*- coding:utf-8 -*-
from case.beatles.test_beatles import TestBeatles
from framework.logger import Logger
from page.ios.beatles.jenkins_page import Jenkins_Tool

class DownloadApp(TestBeatles):

def testDownloadfromJenkins(self):
try:
jp = Jenkins_Tool()
jp.download_app_from_jenkins()
except Exception, e:
Logger.error(e)
self.save_screenshot()
self.fail(e.message)

#-*- coding:utf-8 -*-
from datetime import datetime
import urllib
import jenkins
import os

job_names = [`job-ios`,`job_android`]
username = `Jenkins使用者名稱`
password = `Jenkins密碼`
base_ios_debug_download_url = `https://xxx/ios/Debug/`
base_android_debug_download_url = `https://xxx/android/Debug/`
ios_app_suffix = `.ipa`

class Jenkins_Tool():

def __init__(self):
self.jenkins_url = `http://jenkins host url`
self.sever = jenkins.Jenkins(self.jenkins_url, username=username, password=password)

# 獲取最新構建號
def get_build_number(self, job_name):
lastest_completed_build_number = self.sever.get_job_info(job_name)[`lastCompletedBuild`][`number`]
# print(lastest_completed_build_number)
return lastest_completed_build_number

# 獲取Jenkins下的所有job
def get_all_jobs(self):
all_josbs = self.sever.get_all_jobs()
print(self.sever.jobs_count())
for job in all_josbs:
print(job[`name`])

# 獲取構建日誌
def get_build_console_output(self, job_name):
number = self.get_build_number(job_name)
resps = (self.sever.get_build_console_output(job_name, number))
print(resps)

# 生成ios下載的絕對URL
def getIosAbsoluteURL(self, job_name):
ios_download_url = base_ios_debug_download_url + str(self.get_build_number(job_name)) + `/` + self.getPkgName(job_name)
return ios_download_url

# 生成android下載的絕對URL
def getAndroidAbsoluteURL(self, job_name):
android_download_url = base_android_debug_download_url + str(self.get_build_number(job_name)) + `/` + self.getPkgName(job_name)
return android_download_url

# 拼接包名
def getPkgName(self, job_name):
number = self.get_build_number(job_name)
if job_name == `carpool_iosbuild`:
pkg_name = `iOS包名字首` + str(number) + `_.ipa`
# print(pkg_name)
return pkg_name
else:
pkg_name = `Android包名.apk`
return pkg_name

# 從Jenkins下載最新ios App
def download_ios_app(self, job_name):
local = os.path.join(`/Users/didi/Downloads/`, self.getPkgName(job_name))
ios_download_url = self.getIosAbsoluteURL(job_name)
print(`開始下載iOS App...`)
urllib.urlretrieve(ios_download_url, local)
print(`iOS App 下載完成`)

# 從Jenkins下載最新android App
def download_android_app(self, job_name):
local = os.path.join(`/Users/didi/Downloads/`, self.getPkgName(job_name))
android_download_url = self.getAndroidAbsoluteURL(job_name)
print(`開始下載Android App...`)
urllib.urlretrieve(android_download_url, local)
print(`Android App 下載完成`)

# 下載進度
def callbackfunc(blocknum, blocksize, totalsize):
```回撥函式
@blocknum: 已經下載的資料塊
@blocksize: 資料塊的大小
@totalsize: 遠端檔案的大小
```
percent = 100.0 * blocknum * blocksize / totalsize
if percent > 100:
percent = 100
print "%.2f%%" % percent

# 重新命名ios app
def rename(self, file_dir, file_name):
os.chdir(file_dir) #切換目錄
# print "當前目錄為: %s" % os.listdir(os.getcwd())
for root, dirs, files in os.walk(file_dir):
os.listdir(os.getcwd()) # 獲得當前目錄中的內容
for file in files:
if os.path.splitext(file)[1] == `.ipa`:
os.rename(file_name, `xxx`+ios_app_suffix)
print(`ios app已重新命名為 xxx.ipa`)
return

#從Jenkins下載最新的構建包
def download_app_from_jenkins(self):
jt = Jenkins_Tool()
start_download_time = datetime.now() # 開始下載時間
jt.download_ios_app(`ios-job`)
jt.rename(`/Users/didi/Downloads/`,jt.getPkgName(`ios-job`))
jt.download_android_app(`android-job`)
end_download_time = datetime.now() # 下載完成時間
print(`Android & iOS下載耗時:` + str((end_download_time - start_download_time).seconds))
return True



相關文章