拷贝资源到包.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os
  2. import re
  3. import shutil
  4. src_paths = [
  5. "../ccbi",
  6. "../关卡",
  7. "../关卡/关卡模板",
  8. "../ccb/钉子",
  9. "../ccb/盘子",
  10. "../ccb/目标",
  11. "../config",
  12. "../plist",
  13. "../png_plist",
  14. "../png_plist/sg_gameui",
  15. "../png_plist/sg_items",
  16. "../img_single",
  17. "../轮廓/sg_宝石轮廓",
  18. "../轮廓/sg_盘子轮廓"
  19. ]
  20. dst_dir = "./ScrewGame1-desktop.app/Contents/Resources"
  21. #tools_dir = "/Users/red/ScrewGame/Resources/res_ScrewGame/tools"
  22. # 需要拷贝的文件的后缀
  23. required_suffixes = {
  24. ".redream", ".red", ".json", ".png", ".webp", ".tps"
  25. }
  26. def get_suffix(path: str) -> str:
  27. suffixes = re.findall(
  28. r"(\.[a-zA-Z^/]+)$", path
  29. )
  30. if not len(suffixes):
  31. # print(f"{path}中没有找到suffix")
  32. return ""
  33. else:
  34. return suffixes[0]
  35. # 对比看看哪些文件需要删除
  36. def compare_and_delete_files(file_names: list) -> None:
  37. dst_dir_full_path = os.path.join(os.getcwd(), dst_dir)
  38. dst_file_names = os.listdir(dst_dir_full_path)
  39. file_deleted = []
  40. for file in dst_file_names:
  41. if get_suffix(file) in required_suffixes:
  42. if not file in file_names:
  43. file_deleted.append(file)
  44. # print(file_deleted)
  45. cnt = 0
  46. for file in file_deleted:
  47. full_path = os.path.join(dst_dir_full_path, file)
  48. os.remove(full_path)
  49. cnt += 1
  50. print(f"{cnt} file(s) removed")
  51. def find_files(path: str) -> list:
  52. files = []
  53. if os.path.exists(path) and os.path.isdir(path):
  54. for file in os.listdir(path):
  55. if file != ".DS_Store":
  56. if not os.path.isdir(file):
  57. files.append(os.path.join(path, file))
  58. else:
  59. print(f"{path} not exits or is not a directory.")
  60. return files
  61. def get_file_name(path: str):
  62. pattern = r"([^/]+\.[^/]+)$"
  63. matches = re.finditer(pattern, path)
  64. ret = []
  65. for match in matches:
  66. ret.append(match.group())
  67. return ret
  68. def copy_files(src: str, dst: str) -> None:
  69. shutil.copy2(src, dst)
  70. if __name__ == "__main__":
  71. this_file_path = os.path.dirname(os.path.abspath(__file__))
  72. os.chdir(this_file_path)
  73. # os.chdir(tools_dir)
  74. # print(get_test_path())
  75. files = []
  76. cwd_full_path = os.getcwd()
  77. for dir in src_paths:
  78. # print(dir)
  79. files_in_dir = find_files(dir)
  80. # print(files_in_dir)
  81. files.append(files_in_dir)
  82. files = [
  83. file for file_list in files for file in file_list
  84. ]
  85. # print(files)
  86. file_names = []
  87. file_name_to_remove_from_files = []
  88. for file in files:
  89. if not os.path.isdir(file):
  90. file_name_list = get_file_name(file)
  91. if len(file_name_list) == 1:
  92. file_names.append(file_name_list[0])
  93. else:
  94. print(f"err: {file}匹配的文件名结果不止一个")
  95. else:
  96. file_name_to_remove_from_files.append(file)
  97. # print(file_names)
  98. for file in file_name_to_remove_from_files:
  99. files.remove(file)
  100. compare_and_delete_files(file_names)
  101. if len(files) != len(file_names):
  102. print(f"{len(files)}个file: {files}")
  103. print(f"{len(file_names)}个file_names: {file_names}")
  104. print("长度不统一, 检查一下")
  105. else:
  106. dst_dir_full_path = os.path.join(os.getcwd(), dst_dir)
  107. cnt = 0
  108. for i in range(len(files)):
  109. src_full_path = os.path.join(os.getcwd(), files[i])
  110. dst_full_path = os.path.join(dst_dir_full_path, file_names[i])
  111. shutil.copy2(src_full_path, dst_full_path)
  112. cnt += 1
  113. print(f"{cnt} file(s) copied")