package.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import os
  2. import datetime
  3. import argparse
  4. import re
  5. # 要排除的目录列表
  6. EXCLUDE_DIRS = ['.git', '.idea', 'soft', 'pyTools', 'drop_code', 'jstest', 'local', 'logs', '对话1.txt','vod_cache','data/mv']
  7. # 要排除的文件列表
  8. EXCLUDE_FILES = ['config/env.json', '.env','js/UC分享.js','js/百忙无果[官].js','json/UC分享.json','jx/奇奇.js','jx/芒果关姐.js','data/settings/link_data.json','index.json','custom.json']
  9. def get_script_dir():
  10. """
  11. 获取当前脚本所在目录
  12. """
  13. return os.path.dirname(os.path.abspath(__file__))
  14. def filter_green_files(script_dir):
  15. """
  16. 筛选 js 目录下所有带 [密] 的文件
  17. """
  18. js_dir = os.path.join(script_dir, 'js')
  19. green_files = []
  20. if os.path.exists(js_dir):
  21. for root, _, files in os.walk(js_dir):
  22. for file in files:
  23. if re.search(r'\[密[^\]]*\]', file):
  24. green_files.append(os.path.relpath(os.path.join(root, file), script_dir))
  25. return green_files
  26. def compress_directory(script_dir, green=False):
  27. # 获取当前目录名
  28. current_dir = os.path.basename(script_dir)
  29. # 获取当前时间
  30. current_time = datetime.datetime.now().strftime("%Y%m%d")
  31. # 根据是否传入 green 参数生成压缩包文件名
  32. archive_suffix = "-green" if green else ""
  33. archive_name = f"{current_dir}-{current_time}{archive_suffix}.7z"
  34. # 压缩包输出路径 (脚本所在目录的外面)
  35. parent_dir = os.path.abspath(os.path.join(script_dir, ".."))
  36. archive_path = os.path.join(parent_dir, archive_name)
  37. # 构建 7z 压缩命令
  38. exclude_params = []
  39. # 排除目录
  40. for exclude_dir in EXCLUDE_DIRS:
  41. exclude_params.append(f"-xr!{exclude_dir}")
  42. # 排除文件
  43. for exclude_file in EXCLUDE_FILES:
  44. # 使用相对路径来确保文件的准确性
  45. exclude_file_path = os.path.join(script_dir, exclude_file)
  46. if os.path.exists(exclude_file_path):
  47. exclude_params.append(f"-xr!{exclude_file}")
  48. else:
  49. print(f"警告: {exclude_file} 不存在!")
  50. # 如果启用 green 筛选,排除不符合条件的文件
  51. if green:
  52. green_files = filter_green_files(script_dir)
  53. for file in green_files:
  54. exclude_params.append(f"-x!{file}")
  55. # 构建命令,打包目录内容而不包含目录本身
  56. command = f"7z a \"{archive_path}\" \"{script_dir}\\*\" " + " ".join(exclude_params)
  57. # 打印构建的命令进行调试
  58. print(f"构建的 7z 命令: {command}")
  59. try:
  60. # 执行压缩命令
  61. os.system(command)
  62. print(f"压缩完成: {archive_path}")
  63. except Exception as e:
  64. print(f"压缩失败: {e}")
  65. if __name__ == "__main__":
  66. # 获取脚本所在目录
  67. script_dir = get_script_dir()
  68. # 解析命令行参数
  69. parser = argparse.ArgumentParser(description="压缩当前目录为 7z 包,支持可选参数。")
  70. parser.add_argument('-g', '--green', action='store_true', help="启用 green 模式,筛选 js 目录下所有带 [密] 的文件。")
  71. args = parser.parse_args()
  72. # 调用压缩函数
  73. compress_directory(script_dir, green=args.green)