Jenkins H 5コンパイル、パッケージング、リリース

17851 ワード

記録Jenkins H 5コンパイル、パッケージング、リリース
#!/usr/bin/env python3
# -*- coding:GBK -*-
# author by Michael Ho
# contact:[email protected]

import os, shutil, time
# npm  
def npm_compile(npm_path, npm_source, s_dir):
    """
    :param npm_path:
    :param path:
    :return:
    """
    os.chdir(s_dir)
    print("       :{}".format(s_dir))
    print("    {}      ... ...".format(npm_source))
    npm_install = npm_path + r" install --registry=" + npm_source
    os.system(npm_install)
    print("    ... ...")
    npm_build = npm_path + r" run build"
    os.system(npm_build)

#    .d  
def H5_encode(encode_tool, s_dir, d_dir):
    """
    :param encode_tool: H5Encode    
    :param s_path: workspace     
    :param d_path:         
    """
    if os.path.exists(d_dir):
        shutil.rmtree(d_dir)
    os.makedirs(d_dir)

    if not os.path.exists(encode_tool):
        print("       ,   ... ...")
        exit(0)
    else:
        H5_Encode_CMD = encode_tool + r" -D " + s_dir + r"\ " + d_dir + r"\ 0"
        print(H5_Encode_CMD)
        os.system(H5_Encode_CMD)

#     
def zip_file(zip_exe, path, file):
    """
    :param zip_exe:        
    :param path:        
    :param file:       
    :return:
    """
    if os.path.exists(file):
        os.remove(file)

    zip_CMD = zip_exe + r" a " + file + r" " + path + r"\*"
    print(zip_CMD)
    print("{}    ... ...".format(file))
    os.system(zip_CMD)

    if not os.path.exists(file):
        print("{}     ,     Jenkins    ... ...".format(file))
        exit(0)

#       
def copy_app_H5(x_root, s_dir, d_dir):
    if not os.path.exists(x_root):
        print("       ,     ... ...")
        exit(0)

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

    for root, dirs, files in os.walk(x_root):
        for d in dirs:
            d_name = os.path.join(root, d).rstrip()
            d_dir_name = d_name.replace(x_root, d_dir)
            if not os.path.exists(d_dir_name):
                os.makedirs(d_dir_name)

        for f_name in files:
            f_name = os.path.join(root, f_name).rstrip()
            if(os.path.splitext(f_name)[1] == ".d"):
                s_file = f_name.replace(x_root, s_dir)
                d_file = f_name.replace(x_root, d_dir)
                shutil.copyfile(s_file, d_file)
                #         
                if(s_file.replace(s_dir, "") == d_file.replace(d_dir, "")):
                    print("{} --->      ^.^".format(d_file))
                else:
                    print("        ,   ...")

#      
def copy_all_file(d_qdymanage, s_dir, d_dir):

    if os.path.exists(d_qdymanage):
        for file in os.listdir(d_dir):
            file = os.path.join(d_dir, file)
            if os.path.isfile(file):
                os.remove(file)
            if os.path.isdir(file) and file != d_qdymanage:
                shutil.rmtree(file) #    qdymanage            
    else:
        print("{}      ,    !     ... ...".format(d_qdymanage))
        exit(0)

    for root, dirs, files in os.walk(s_dir):
        for d in dirs:
            s_dir_name = os.path.join(root, d) #   workspace      
            d_dir_name = s_dir_name.replace(s_dir, d_dir)
            if not os.path.exists(d_dir_name):
                os.makedirs(d_dir_name) #   zzinfo      

        for f in files:
            s_file_name = os.path.join(root, f) #   workspace            
            d_file_name = s_file_name.replace(s_dir, d_dir) #            
            print("{0}  --->  {1}".format(s_file_name, d_file_name).rstrip())
            shutil.copyfile(s_file_name, d_file_name)


# main    
if __name__ == "__main__":

    os.environ['VERSION'] = r"7_06_001"

    # 0.  
    npm_path = r'"C:\Program Files
odejs
pm"
' npm_source = r"https://registry.npm.taobao.org" s_dir = os.getenv("WORKSPACE") npm_compile(npm_path, npm_source, s_dir) # 1. encode_tool = r"d:\Jenkins\H5Encode.exe" m_dir = s_dir + r"\dest" # %workspace%\dist H5 d_dir = r"d:\Jenkins\frontend_encrypt" H5_encode(encode_tool, m_dir, d_dir) # 2. ( ) # 2.1 x_root = r"d:\H5_app_src" xs_dir = d_dir xd_dir = r"d:\Jenkins\H5_app" copy_app_H5(x_root, xs_dir, xd_dir) # 2.2 zip_exe = r'"C:\"Program Files"\7-Zip\7z.exe"' src_file = r"H5_" + os.getenv("VERSION") + "_src_" + os.getenv("TEST_ENVIRONMENT") + "_" + time.strftime('%Y%m%d') + ".7z" src_dir = xd_dir zip_file(zip_exe, src_dir, src_file) # 3. ( ) file = r"H5_" + os.getenv("VERSION") + r"_" + os.getenv("TEST_ENVIRONMENT") + "_" + time.strftime('%Y%m%d') + ".7z" zip_file(zip_exe, d_dir, file) # 4.170 if os.getenv("TEST_ENVIRONMENT") == "js_test": d_qdymanage = r"d:\zzinfo\mszx\download\qdymanage" all_s_dir = d_dir all_d_dir = r"d:\zzinfo\mszx\download" copy_all_file(d_qdymanage, all_s_dir, all_d_dir)