import subprocess import sys import os import shutil import argparse # 解析命令行参数 parser = argparse.ArgumentParser() parser.add_argument('--encrypt', default=False) # 执行shell ,移动文件 shell = """cd /home/wangliming/project/zlwl-algos/PACKAGE/common && python ./copy_file.py""" cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True, stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1) cmd.communicate() if cmd.returncode != 0: raise Exception("打包过程出错") # 配置参数 rel_path = os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__)))) package_path = 'PACKAGE/common/' project_name_tmp = (os.path.abspath(__file__).split('/')[-4] + '_common').replace("_","-").split("-") project_name = "".join([x.capitalize() for x in project_name_tmp]) with open(os.path.join(rel_path, package_path, 'ver'),'r') as f: ver = int(f.readline()) version = f"1.0.{ver}" lib_path = os.path.join(rel_path, package_path, r'distribute', version.replace(".","_")) #加密 if parser.parse_args().encrypt=='true': print("文件加密中.............................") shell = f"cd {lib_path} &&\ python ./setup_encrypt.py build_ext" cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True, stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1) cmd.communicate() if cmd.returncode != 0: raise Exception("打包过程出错") # 移动文件 for p in os.listdir(os.path.join(lib_path,'build')): if 'lib.' in p: lib_sub_path = p shutil.copytree(os.path.join(lib_path, 'build',lib_sub_path), lib_path,dirs_exist_ok=True) for dirpath, dirnames, filenames in os.walk(os.path.join(lib_path,project_name)): # 遍历文件夹下的py文件 if 'iotp_proto' not in dirpath: for f in filenames: if (f.endswith(".py") or f.endswith(".c")) and f != '__init__.py': os.remove(os.path.join(dirpath,f)) # 执行打包 print("文件打包中.............................") shell = f"cd {lib_path} &&\ python ./setup.py bdist_wheel &&\ python ./setup.py sdist " cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True, stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1) cmd.communicate() if cmd.returncode != 0: raise Exception("打包过程出错") # 删除无用目录 if os.path.exists(os.path.join(lib_path, 'build')): shutil.rmtree(os.path.join(lib_path, 'build')) if os.path.exists(os.path.join(lib_path, 'ZlwlAlgosCommon.egg-info')): shutil.rmtree(os.path.join(lib_path, 'ZlwlAlgosCommon.egg-info')) # 改名 platform = sys.platform if 'win' in platform: platform = 'win_amd64' else: platform = 'linux_x86_64' if parser.parse_args().encrypt=='true': shutil.move(f"{lib_path}/dist/{project_name}-{version}-py3-none-any.whl", f"{lib_path}/dist/{project_name}-{project_name}-py3-none-{platform}.whl") # 上传 print("上传包到私服仓库中") for f in os.listdir(os.path.join(lib_path, 'dist')): if f.endswith(".whl"): filename = f break username = "zlwl-algos-admin" password = "Qx123456" url = 'http://algo-dev-internal.li-ai.com.cn:8081/repository/zlwl-algos/' file = os.path.join(lib_path,"dist", filename) shell = f"cd {lib_path} &&\ twine upload -u {username} -p {password} -r nexus\ --repository-url {url} {file} " print(shell) cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True, stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1) cmd.communicate() if cmd.returncode != 0: raise Exception("打包过程出错") print("上传成功") # 改名