You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
6.8 KiB

1 year ago
1 year ago
9 months ago
1 year ago
9 months ago
1 year ago
5 months ago
9 months ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
9 months ago
9 months ago
  1. import glob
  2. import json
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. import requests
  8. import platform
  9. import shutil
  10. import zipfile
  11. import urllib.request
  12. # import py7zr
  13. def get_processor_info():
  14. if os.uname().sysname == 'Darwin':
  15. processor_info = subprocess.check_output(['sysctl', '-n', 'machdep.cpu.brand_string']).strip()
  16. processor_info = str(processor_info)
  17. if 'Intel' in processor_info:
  18. return 'Intel'
  19. elif 'Apple' in processor_info:
  20. return 'Apple'
  21. else:
  22. return 'Unknown'
  23. else:
  24. return 'This method is only implemented for macOS.'
  25. def compress_folder_to_7z(folder_path, output_file):
  26. if os.path.exists(output_file):
  27. os.remove(output_file)
  28. # with py7zr.SevenZipFile(output_file, 'w') as archive:
  29. # archive.writeall(folder_path, output_file)
  30. # 压缩文件夹
  31. try:
  32. # "-mmt4"表示使用4个线程压缩
  33. subprocess.call(["7z", "a", "-mx=9", output_file, folder_path])
  34. except:
  35. subprocess.call(["7za", "a", "-mx=9", output_file, folder_path])
  36. def compress_folder_to_7z_split(folder_path, output_file):
  37. if os.path.exists(output_file):
  38. os.remove(output_file)
  39. file_name = os.path.basename(output_file)
  40. file_dir = os.path.dirname(output_file)
  41. # 获取文件名的前缀
  42. file_prefix = os.path.splitext(file_name)[0]
  43. # 构建分卷文件的路径模式
  44. split_file_pattern = os.path.join(file_dir, file_prefix + ".7z.*")
  45. # 获取匹配的分卷文件列表
  46. split_files = glob.glob(split_file_pattern)
  47. # 删除分卷文件
  48. for split_file in split_files:
  49. os.remove(split_file)
  50. # 压缩文件夹
  51. try:
  52. subprocess.call(["7z", "a", "-v95m", output_file, folder_path])
  53. except:
  54. try:
  55. subprocess.call(["7za", "a", "-v95m", output_file, folder_path])
  56. except:
  57. subprocess.call(["7zz", "a", "-v95m", output_file, folder_path])
  58. easyspider_version = "0.6.2"
  59. if __name__ == "__main__":
  60. if sys.platform == "win32" and platform.architecture()[0] == "64bit":
  61. file_name = f"EasySpider_{easyspider_version}_Windows_x64.7z"
  62. if os.path.exists("./EasySpider_Windows_x64/user_data"):
  63. shutil.rmtree("./EasySpider_Windows_x64/user_data")
  64. if os.path.exists("./EasySpider_Windows_x64/Data"):
  65. shutil.rmtree("./EasySpider_Windows_x64/Data")
  66. if os.path.exists("./EasySpider_Windows_x64/execution_instances"):
  67. shutil.rmtree("./EasySpider_Windows_x64/execution_instances")
  68. if os.path.exists("./EasySpider_Windows_x64/config.json"):
  69. os.remove("./EasySpider_Windows_x64/config.json")
  70. if os.path.exists("./EasySpider_Windows_x64/mysql_config.json"):
  71. os.remove("./EasySpider_Windows_x64/mysql_config.json")
  72. if os.path.exists("./EasySpider_Windows_x64/TempUserDataFolder"):
  73. shutil.rmtree("./EasySpider_Windows_x64/TempUserDataFolder")
  74. os.mkdir("./EasySpider_Windows_x64/Data")
  75. os.mkdir("./EasySpider_Windows_x64/execution_instances")
  76. # compress_folder_to_7z_split("./EasySpider_Windows_x64", file_name)
  77. # print(f"Compress {file_name} Split successfully!")
  78. compress_folder_to_7z("./EasySpider_Windows_x64", file_name)
  79. print(f"Compress {file_name} successfully!")
  80. elif sys.platform == "win32" and platform.architecture()[0] == "32bit":
  81. file_name = f"EasySpider_{easyspider_version}_Windows_x32.7z"
  82. if os.path.exists("./EasySpider_Windows_x32/user_data"):
  83. shutil.rmtree("./EasySpider_Windows_x32/user_data")
  84. if os.path.exists("./EasySpider_Windows_x32/Data"):
  85. shutil.rmtree("./EasySpider_Windows_x32/Data")
  86. if os.path.exists("./EasySpider_Windows_x32/execution_instances"):
  87. shutil.rmtree("./EasySpider_Windows_x32/execution_instances")
  88. if os.path.exists("./EasySpider_Windows_x32/config.json"):
  89. os.remove("./EasySpider_Windows_x32/config.json")
  90. if os.path.exists("./EasySpider_Windows_x32/mysql_config.json"):
  91. os.remove("./EasySpider_Windows_x32/mysql_config.json")
  92. if os.path.exists("./EasySpider_Windows_x32/TempUserDataFolder"):
  93. shutil.rmtree("./EasySpider_Windows_x32/TempUserDataFolder")
  94. os.mkdir("./EasySpider_Windows_x32/Data")
  95. os.mkdir("./EasySpider_Windows_x32/execution_instances")
  96. # compress_folder_to_7z_split("./EasySpider_Windows_x32", file_name)
  97. # print(f"Compress {file_name} Split successfully!")
  98. compress_folder_to_7z("./EasySpider_Windows_x32", file_name)
  99. print(f"Compress {file_name} successfully!")
  100. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  101. file_name = f"EasySpider_{easyspider_version}_Linux_x64.tar.xz"
  102. if os.path.exists("./EasySpider_Linux_x64/user_data"):
  103. shutil.rmtree("./EasySpider_Linux_x64/user_data")
  104. if os.path.exists("./EasySpider_Linux_x64/Data"):
  105. shutil.rmtree("./EasySpider_Linux_x64/Data")
  106. if os.path.exists("./EasySpider_Linux_x64/execution_instances"):
  107. shutil.rmtree("./EasySpider_Linux_x64/execution_instances")
  108. if os.path.exists("./EasySpider_Linux_x64/config.json"):
  109. os.remove("./EasySpider_Linux_x64/config.json")
  110. if os.path.exists("./EasySpider_Linux_x64/mysql_config.json"):
  111. os.remove("./EasySpider_Linux_x64/mysql_config.json")
  112. if os.path.exists("./EasySpider_Linux_x64/TempUserDataFolder"):
  113. shutil.rmtree("./EasySpider_Linux_x64/TempUserDataFolder")
  114. os.mkdir("./EasySpider_Linux_x64/Data")
  115. os.mkdir("./EasySpider_Linux_x64/execution_instances")
  116. subprocess.call(["tar", "-Jcvf", file_name, "./EasySpider_Linux_x64"])
  117. print(f"Compress {file_name} successfully!")
  118. elif sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  119. arch = get_processor_info()
  120. if arch == "Intel":
  121. file_name = f"EasySpider_{easyspider_version}_MacOS_Intel_Chip.7z"
  122. else:
  123. file_name = f"EasySpider_{easyspider_version}_MacOS_Apple_Arm_Chip.7z"
  124. if os.path.exists("./EasySpider_MacOS/Data"):
  125. shutil.rmtree("./EasySpider_MacOS/Data")
  126. os.mkdir("./EasySpider_MacOS/Data")
  127. if os.path.exists("./EasySpider_MacOS/TempUserDataFolder"):
  128. shutil.rmtree("./EasySpider_MacOS/TempUserDataFolder")
  129. # if os.path.exists(file_name):
  130. # os.remove(file_name)
  131. # print(f"Remove {file_name} successfully!")
  132. # subprocess.call(["tar", "-zcvf", file_name, "./EasySpider_MacOS"])
  133. # brew install p7zip
  134. # subprocess.call(["7z", "a", "-mx=9", file_name, "./EasySpider_MacOS"])
  135. compress_folder_to_7z("./EasySpider_MacOS", file_name)
  136. print(f"Compress {file_name} successfully!")