-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
124 lines (101 loc) · 3.97 KB
/
script.py
File metadata and controls
124 lines (101 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import sys
import subprocess
import re
# 黑名单列表,包含需要排除的文件夹名或文件名的正则表达式
BLACKLIST_PATTERNS = [
r".*tcp.*$", # 排除包含 "tcp" 的文件或文件夹
r".*mcu.*$", # 排除包含 "mcu" 的文件或文件夹
r".*kernel.*$", # 排除包含 "kernel" 的文件或文件夹
]
def is_blacklisted(path):
"""检查路径是否匹配黑名单规则"""
for pattern in BLACKLIST_PATTERNS:
if re.search(pattern, path, re.IGNORECASE):
return True
return False
def get_7z_path():
"""获取当前目录下的7z.exe路径"""
script_dir = os.path.dirname(os.path.abspath(__file__))
exe_path = os.path.join(script_dir, "7za.exe")
dll_path = os.path.join(script_dir, "7za.dll")
if not os.path.exists(exe_path):
raise FileNotFoundError("7za.exe 未在当前脚本目录中找到!")
if not os.path.exists(dll_path):
raise FileNotFoundError("7za.dll 未在当前脚本目录中找到!")
return exe_path
def decompress(archive_path, sevenz_exe):
"""解压文件到同名文件夹"""
output_dir = os.path.splitext(archive_path)[0]
# 特殊处理 .tar.gz 文件
if archive_path.endswith(".tar.gz"):
output_dir = os.path.splitext(output_dir)[0]
os.makedirs(output_dir, exist_ok=True)
cmd = [sevenz_exe, "x", archive_path, f"-o{output_dir}", "-y"] # 覆盖确认
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags=subprocess.CREATE_NO_WINDOW,
)
if result.returncode == 0:
print(
f"解压成功: {os.path.relpath(archive_path)} -> {os.path.relpath(output_dir)}"
)
return output_dir # 返回解压后的文件夹路径
else:
print(
f"解压失败: {os.path.relpath(archive_path)}\n错误信息: {result.stderr.decode('gbk')}"
)
return None
def process_path(path, sevenz_exe, processed_archives=None):
"""处理单个路径(文件或目录)"""
if processed_archives is None:
processed_archives = set()
# 检查路径是否在黑名单中
if is_blacklisted(path):
print(f"跳过黑名单路径: {os.path.relpath(path)}")
return
# 防止重复处理相同的文件
if path in processed_archives:
return
processed_archives.add(path)
if os.path.isfile(path):
ext = os.path.splitext(path)[1].lower()
# 特殊处理 .tar.gz 文件
if ext == ".gz" and path.endswith(".tar.gz"):
output_dir = decompress(path, sevenz_exe)
if output_dir:
# 递归处理解压后的文件夹
for root, _, files in os.walk(output_dir):
for file in files:
file_path = os.path.join(root, file)
process_path(file_path, sevenz_exe, processed_archives)
elif ext in (".zip", ".7z", ".rar", ".tar", ".gz", ".bz2"):
output_dir = decompress(path, sevenz_exe)
if output_dir:
# 递归处理解压后的文件夹
for root, _, files in os.walk(output_dir):
for file in files:
file_path = os.path.join(root, file)
process_path(file_path, sevenz_exe, processed_archives)
elif os.path.isdir(path):
for root, _, files in os.walk(path):
for file in files:
file_path = os.path.join(root, file)
process_path(file_path, sevenz_exe, processed_archives)
def main():
try:
sevenz_exe = get_7z_path()
except Exception as e:
print(f"初始化错误: {e}")
return
for path in sys.argv[1:]:
if os.path.exists(path):
process_path(path, sevenz_exe)
else:
print(f"路径不存在: {os.path.relpath(path)}")
# 保持窗口开启
input("按回车键退出...")
if __name__ == "__main__":
main()