copy_file.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import shutil
  3. """
  4. 1、复制待打包的源码到distribute中;
  5. 2、生成新的版本号
  6. 3、添加__init__.py文件
  7. """
  8. # 参数
  9. rel_path = os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))
  10. package_path = 'PACKAGE/common/'
  11. print(rel_path)
  12. project_name_tmp = (os.path.abspath(__file__).split('/')[-4] + '_common').replace("_","-").split("-")
  13. project_name = "".join([x.capitalize() for x in project_name_tmp])
  14. with open(os.path.join(rel_path, package_path, 'ver'),'r+') as f:
  15. ver = int(f.readline())
  16. newver = ver + 1
  17. f.seek(0)
  18. f.truncate()
  19. f.write(str(newver))
  20. version = f"1.0.{newver}"
  21. # 删除旧的build
  22. print("\n清空旧的build目录中................")
  23. try:
  24. if os.path.exists(os.path.join(rel_path, package_path, r'build')):
  25. shutil.rmtree(os.path.join(rel_path, package_path, r'build'))
  26. except Exception as e:
  27. print(e)
  28. print(f'error: 清空旧的build目录失败,请手动删除build目录\n')
  29. # 生成对应项目的目录
  30. print("\n生成项目文件目录中................")
  31. lib_path = os.path.join(rel_path, package_path, r'distribute', version.replace(".","_"))
  32. if not os.path.exists(lib_path):
  33. os.makedirs(lib_path)
  34. else:
  35. shutil.rmtree(lib_path)
  36. # 移动文件
  37. print("\n移动文件中................")
  38. new_path = os.path.join(lib_path,project_name)
  39. shutil.copytree(os.path.join(rel_path, 'COMMON'), new_path)
  40. for dirpath, dirnames, filenames in os.walk(new_path):
  41. if '__init__.py' not in os.listdir(dirpath) and "__pycache__" not in dirpath:
  42. with open(f"{dirpath}/__init__.py", 'w') as f:
  43. pass
  44. shutil.copy(os.path.join(rel_path, package_path, 'template', 'setup.py'), lib_path)
  45. shutil.copy(os.path.join(rel_path, package_path, 'template','LICENSE'), lib_path)
  46. shutil.copy(os.path.join(rel_path, package_path, 'template','README.md'), lib_path)
  47. shutil.copy(os.path.join(rel_path, package_path, 'template','setup_encrypt.py'), lib_path)
  48. shutil.copy(os.path.join(rel_path, package_path, 'ver'), lib_path)
  49. # setup(
  50. # name=project_name,
  51. # version=version,
  52. # author='zlwl',
  53. # packages=find_packages(lib_path+"/src/common"),
  54. # python_requires='==3.8.16',
  55. # platforms='Linux'
  56. # )