build.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import subprocess
  2. import sys
  3. import os
  4. import shutil
  5. import argparse
  6. # 解析命令行参数
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('--encrypt', default=False)
  9. # 执行shell ,移动文件
  10. shell = """cd /home/wangliming/project/zlwl-algos/PACKAGE/common &&
  11. python ./copy_file.py"""
  12. cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
  13. stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)
  14. cmd.communicate()
  15. if cmd.returncode != 0:
  16. raise Exception("打包过程出错")
  17. # 配置参数
  18. rel_path = os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))
  19. package_path = 'PACKAGE/common/'
  20. project_name_tmp = (os.path.abspath(__file__).split('/')[-4] + '_common').replace("_","-").split("-")
  21. project_name = "".join([x.capitalize() for x in project_name_tmp])
  22. with open(os.path.join(rel_path, package_path, 'ver'),'r') as f:
  23. ver = int(f.readline())
  24. version = f"1.0.{ver}"
  25. lib_path = os.path.join(rel_path, package_path, r'distribute', version.replace(".","_"))
  26. #加密
  27. if parser.parse_args().encrypt=='true':
  28. print("文件加密中.............................")
  29. shell = f"cd {lib_path} &&\
  30. python ./setup_encrypt.py build_ext"
  31. cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
  32. stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)
  33. cmd.communicate()
  34. if cmd.returncode != 0:
  35. raise Exception("打包过程出错")
  36. # 移动文件
  37. for p in os.listdir(os.path.join(lib_path,'build')):
  38. if 'lib.' in p:
  39. lib_sub_path = p
  40. shutil.copytree(os.path.join(lib_path, 'build',lib_sub_path), lib_path,dirs_exist_ok=True)
  41. for dirpath, dirnames, filenames in os.walk(os.path.join(lib_path,project_name)): # 遍历文件夹下的py文件
  42. if 'iotp_proto' not in dirpath:
  43. for f in filenames:
  44. if (f.endswith(".py") or f.endswith(".c")) and f != '__init__.py':
  45. os.remove(os.path.join(dirpath,f))
  46. # 执行打包
  47. print("文件打包中.............................")
  48. shell = f"cd {lib_path} &&\
  49. python ./setup.py bdist_wheel &&\
  50. python ./setup.py sdist "
  51. cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
  52. stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)
  53. cmd.communicate()
  54. if cmd.returncode != 0:
  55. raise Exception("打包过程出错")
  56. # 删除无用目录
  57. if os.path.exists(os.path.join(lib_path, 'build')):
  58. shutil.rmtree(os.path.join(lib_path, 'build'))
  59. if os.path.exists(os.path.join(lib_path, 'ZlwlAlgosCommon.egg-info')):
  60. shutil.rmtree(os.path.join(lib_path, 'ZlwlAlgosCommon.egg-info'))
  61. # 改名
  62. platform = sys.platform
  63. if 'win' in platform:
  64. platform = 'win_amd64'
  65. else:
  66. platform = 'linux_x86_64'
  67. if parser.parse_args().encrypt=='true':
  68. shutil.move(f"{lib_path}/dist/{project_name}-{version}-py3-none-any.whl",
  69. f"{lib_path}/dist/{project_name}-{project_name}-py3-none-{platform}.whl")
  70. # 上传
  71. print("上传包到私服仓库中")
  72. for f in os.listdir(os.path.join(lib_path, 'dist')):
  73. if f.endswith(".whl"):
  74. filename = f
  75. break
  76. username = "zlwl-algos-admin"
  77. password = "Qx123456"
  78. url = 'http://algo-dev-internal.li-ai.com.cn:8081/repository/zlwl-algos/'
  79. file = os.path.join(lib_path,"dist", filename)
  80. shell = f"cd {lib_path} &&\
  81. twine upload -u {username} -p {password} -r nexus\
  82. --repository-url {url} {file} "
  83. print(shell)
  84. cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
  85. stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)
  86. cmd.communicate()
  87. if cmd.returncode != 0:
  88. raise Exception("打包过程出错")
  89. print("上传成功")
  90. # 改名