#-*- 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