#coding:utf-8
OUTPUTS_PATH = "/var/lib/jenkins/workspace/"

GRADL_HOME = "/root/gradle-2.10/bin/"

SOURCE_CODE = "sourcecode"

BUILD_TYPE = "debug"

# 不同项目配置不同的地址
GIT_REPO = "your git path"


def copyFiles(sourceDir,  targetDir): #把某一目录下的所有文件复制到指定目录中 
     for file in os.listdir(sourceDir): 
         sourceFile = os.path.join(sourceDir,  file) 
         targetFile = os.path.join(targetDir,  file) 
         if os.path.isfile(sourceFile): 
             if not os.path.exists(targetDir): 
                 os.makedirs(targetDir) 
             if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))): 
                     open(targetFile, "wb").write(open(sourceFile, "rb").read()) 
         if os.path.isdir(sourceFile): 
             First_Directory = False 
             copyFiles(sourceFile, targetFile) 

def chdir(dir):
    os.chdir(dir)
    print ('当前目录:' + os.getcwd())

def getVersionName(path):
    import re
    versionName = ""
    f = open(path)
    for line in f:
        searchObj = re.search( r'versionName "(.*)"', line, re.M|re.I)
        if searchObj:
            versionName = searchObj.group(1) 
            break
    return versionName


# 主程序开始
if __name__ == "__main__":

    import shutil
    import sys
    import os

    print sys.path[0]
    chdir(sys.path[0])

    import time    
    start_time = time.time()
    print ('start time %f' %start_time)

    # 开始读取参数
    if len(sys.argv) < 3:
        print "构建脚本 参数不够3个,第一个是脚本路径,第二个是项目名称,第三个是构建分支"
        exit()

    if len(sys.argv) >= 4:
        BUILD_TYPE = sys.argv[3]
        pass

    OUTPUTS_PATH = OUTPUTS_PATH + sys.argv[1] + "/outputs"


    print "产物路径:" + OUTPUTS_PATH

    if (False == os.path.exists(OUTPUTS_PATH)):
        os.mkdir(OUTPUTS_PATH) 

    if (os.path.exists(OUTPUTS_PATH)):
        shutil.rmtree(OUTPUTS_PATH)


# git地址 分支
    branch = sys.argv[2]
    module = GIT_REPO
    print ('代码地址:' + module + ",分支:" + branch)

# 获取项目名称
    import re
    MODULE_NAME = re.search(r'/(.*)\.git', GIT_REPO).group(1)
    print "项目名称:" + MODULE_NAME

# clone代码

    if os.path.exists(SOURCE_CODE):
        shutil.rmtree(SOURCE_CODE)
    os.mkdir(SOURCE_CODE)
    chdir(SOURCE_CODE)
    print ('start to clone codes')

    os.system("/usr/bin/git clone -b " + branch + " " + module)

# 读取版本号
    versionName = getVersionName(MODULE_NAME + "/publish.gradle")
    print ("版本号为:" + versionName)

# 复制gradle配置文件
    chdir(os.path.dirname(os.getcwd()))
    print ('copy files from gradle_config to ' + SOURCE_CODE)
    copyFiles('../gradle_config', SOURCE_CODE)


    chdir(SOURCE_CODE)

# 写入setting.gradle

    setting_file = open('settings.gradle', "wb")
    setting_file.write("include ':" + MODULE_NAME + "'")
    setting_file.write('\n')
    setting_file.close()

# 创建outputs
    if os.path.exists("outputs"):
        shutil.rmtree("outputs")
    os.mkdir("outputs")

# 开始编译

    print ('start to build apk')
    os.system(GRADL_HOME + "gradle clean assemble" + BUILD_TYPE)

    chdir(os.path.dirname(os.getcwd()))

    copyFiles(SOURCE_CODE + "/" + MODULE_NAME + "/build/outputs", OUTPUTS_PATH)

    shutil.rmtree(SOURCE_CODE)

    if os.path.exists(OUTPUTS_PATH + "/apk/" + MODULE_NAME + "-"+ BUILD_TYPE + "-unaligned.apk"):
        os.remove(OUTPUTS_PATH + "/apk/" + MODULE_NAME + "-" + BUILD_TYPE + "-unaligned.apk")

    if os.path.exists(OUTPUTS_PATH + "/apk/" + MODULE_NAME + "-"+ BUILD_TYPE + ".apk"):
        os.rename(OUTPUTS_PATH + "/apk/" + MODULE_NAME + "-"+ BUILD_TYPE + ".apk",
                  OUTPUTS_PATH + "/apk/" + MODULE_NAME + "-" + versionName + "-"+ BUILD_TYPE + ".apk")



    end_time = time.time()

    cost_time  = (end_time - start_time)
    minutes = cost_time/60
    seconds = cost_time - 60 * minutes
    print ('cost time : %d mins %d secs' %(minutes, seconds))
    print ('提测包打包完成,结果在路径 ' + os.getcwd() + "/" + OUTPUTS_PATH)
    print ('提测之前请先完成自测')

标签: none

评论已关闭