-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_clean.py
More file actions
443 lines (372 loc) · 14.7 KB
/
Copy pathexport_clean.py
File metadata and controls
443 lines (372 loc) · 14.7 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env python3
"""
导出干净的项目代码,删除构建缓存和大文件
"""
import os
import shutil
import argparse
from pathlib import Path
# 要删除的目录
DIRS_TO_DELETE = [
"build",
".vs",
"__pycache__",
"CMakeFiles",
"x64",
"Debug",
"Release",
".cache",
"out",
# extern 子模块中的构建目录
"**/build",
"**/.vs",
"**/CMakeFiles",
]
# 要删除的文件扩展名(构建产物)
EXTENSIONS_TO_DELETE = [
# 编译产物
".o", ".obj", ".lo", ".slo",
".a", ".lib", ".la", ".lai",
".so", ".dll", ".dylib",
".exe", ".out", ".app",
".pch", ".gch",
".d", ".mod", ".smod",
# Visual Studio
".pdb", ".ilk", ".exp",
".idb", ".ipdb", ".iobj",
# CMake
".cmake",
# Python 缓存
".pyc", ".pyo",
# CUDA 编译产物
".ptx", ".cubin", ".fatbin",
# 临时文件
".tmp", ".temp", ".log",
]
# 要删除的特定文件名
FILES_TO_DELETE = [
"CMakeCache.txt",
"cmake_install.cmake",
"Makefile",
"compile_commands.json",
".DS_Store",
"Thumbs.db",
"desktop.ini",
]
# 要删除的大文件目录或文件(根据你的项目调整)
LARGE_ITEMS_TO_DELETE = [
# 渲染输出的 EXR 文件(很大)
"scenes/curly/render.exr",
"scenes/curly/render*.exr",
"scenes/envmaps", # 环境贴图很大
# assets 目录中的二进制文件(可选,如果需要保留模型就注释掉)
# "assets/*.bin",
]
# 要保留的代码文件扩展名
CODE_EXTENSIONS = {
".cpp", ".cc", ".cxx", ".c",
".h", ".hpp", ".hxx", ".hh",
".cu", ".cuh", # CUDA
".py",
".cmake", ".txt", # CMakeLists.txt
".json",
".md",
".sh", ".bat",
".gitignore", ".gitmodules",
}
def get_size_str(size_bytes):
"""将字节转换为可读的大小字符串"""
for unit in ['B', 'KB', 'MB', 'GB']:
if size_bytes < 1024:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024
return f"{size_bytes:.2f} TB"
def get_dir_size(path):
"""计算目录大小"""
total = 0
try:
for entry in os.scandir(path):
if entry.is_file(follow_symlinks=False):
total += entry.stat().st_size
elif entry.is_dir(follow_symlinks=False):
total += get_dir_size(entry.path)
except (PermissionError, OSError):
pass
return total
def find_items_to_delete(root_path, delete_exr=True, delete_envmaps=True, delete_assets_bin=False):
"""查找所有需要删除的项目"""
items_to_delete = []
root = Path(root_path)
# 1. 查找要删除的目录
dir_patterns = ["build", ".vs", "__pycache__", "CMakeFiles", "x64", "Debug", "Release", ".cache", "out"]
for dirpath, dirnames, filenames in os.walk(root):
dirpath = Path(dirpath)
# 跳过 .git 目录
if ".git" in dirpath.parts:
continue
for dirname in dirnames[:]: # 使用切片复制列表以便修改
if dirname in dir_patterns:
full_path = dirpath / dirname
size = get_dir_size(full_path)
items_to_delete.append(("dir", full_path, size))
dirnames.remove(dirname) # 不再遍历这个目录
# 2. 查找要删除的文件(按扩展名)
for dirpath, dirnames, filenames in os.walk(root):
dirpath = Path(dirpath)
# 跳过 .git 目录
if ".git" in dirpath.parts:
continue
# 跳过已标记删除的目录
skip = False
for item_type, item_path, _ in items_to_delete:
if item_type == "dir" and str(dirpath).startswith(str(item_path)):
skip = True
break
if skip:
continue
for filename in filenames:
filepath = dirpath / filename
# 检查扩展名
ext = filepath.suffix.lower()
if ext in EXTENSIONS_TO_DELETE:
try:
size = filepath.stat().st_size
items_to_delete.append(("file", filepath, size))
except OSError:
pass
continue
# 检查特定文件名
if filename in FILES_TO_DELETE:
try:
size = filepath.stat().st_size
items_to_delete.append(("file", filepath, size))
except OSError:
pass
continue
# 检查 EXR 文件
if delete_exr and ext == ".exr":
try:
size = filepath.stat().st_size
items_to_delete.append(("file", filepath, size))
except OSError:
pass
# 3. 检查环境贴图目录
if delete_envmaps:
envmaps_dir = root / "scenes" / "envmaps"
if envmaps_dir.exists():
size = get_dir_size(envmaps_dir)
items_to_delete.append(("dir", envmaps_dir, size))
# 4. 检查 assets 中的 bin 文件
if delete_assets_bin:
assets_dir = root / "assets"
if assets_dir.exists():
for f in assets_dir.glob("*.bin"):
try:
size = f.stat().st_size
items_to_delete.append(("file", f, size))
except OSError:
pass
return items_to_delete
def delete_items(items, dry_run=False):
"""删除指定的项目"""
total_freed = 0
deleted_count = 0
for item_type, item_path, size in items:
try:
if dry_run:
print(f"[DRY-RUN] 将删除 {item_type}: {item_path} ({get_size_str(size)})")
else:
if item_type == "dir":
shutil.rmtree(item_path)
else:
os.remove(item_path)
print(f"已删除 {item_type}: {item_path} ({get_size_str(size)})")
total_freed += size
deleted_count += 1
except Exception as e:
print(f"无法删除 {item_path}: {e}")
return deleted_count, total_freed
def copy_clean_project(src_root, dest_root, delete_exr=True, delete_envmaps=True, delete_assets_bin=False):
"""复制干净的项目到新目录"""
src_root = Path(src_root)
dest_root = Path(dest_root)
if dest_root.exists():
print(f"目标目录 {dest_root} 已存在,是否覆盖?(y/n)")
if input().lower() != 'y':
print("取消操作")
return
shutil.rmtree(dest_root)
# 获取要跳过的模式
skip_dirs = {"build", ".vs", "__pycache__", "CMakeFiles", "x64", "Debug", "Release", ".cache", "out", ".git"}
skip_extensions = set(EXTENSIONS_TO_DELETE)
if delete_exr:
skip_extensions.add(".exr")
copied_files = 0
total_size = 0
for dirpath, dirnames, filenames in os.walk(src_root):
dirpath = Path(dirpath)
rel_path = dirpath.relative_to(src_root)
# 跳过特定目录
dirnames[:] = [d for d in dirnames if d not in skip_dirs]
# 跳过环境贴图目录
if delete_envmaps and rel_path == Path("scenes"):
if "envmaps" in dirnames:
dirnames.remove("envmaps")
# 创建目标目录
dest_dir = dest_root / rel_path
dest_dir.mkdir(parents=True, exist_ok=True)
for filename in filenames:
src_file = dirpath / filename
ext = src_file.suffix.lower()
# 跳过构建产物
if ext in skip_extensions:
continue
# 跳过特定文件
if filename in FILES_TO_DELETE:
continue
# 跳过 assets 中的 bin 文件(可选)
if delete_assets_bin and rel_path == Path("assets") and ext == ".bin":
continue
# 复制文件
dest_file = dest_dir / filename
try:
shutil.copy2(src_file, dest_file)
copied_files += 1
total_size += src_file.stat().st_size
except Exception as e:
print(f"无法复制 {src_file}: {e}")
print(f"\n复制完成!")
print(f"复制了 {copied_files} 个文件")
print(f"总大小: {get_size_str(total_size)}")
print(f"目标目录: {dest_root}")
def calculate_remaining(root_path, items_to_delete):
"""计算删除后会保留的文件数量和大小"""
root = Path(root_path)
# 构建要删除的路径集合
delete_paths = set()
delete_dirs = set()
for item_type, item_path, _ in items_to_delete:
if item_type == "dir":
delete_dirs.add(Path(item_path))
else:
delete_paths.add(Path(item_path))
remaining_files = 0
remaining_size = 0
remaining_by_ext = {}
for dirpath, dirnames, filenames in os.walk(root):
dirpath = Path(dirpath)
# 跳过 .git 目录
if ".git" in dirpath.parts:
continue
# 检查是否在要删除的目录中
skip_dir = False
for del_dir in delete_dirs:
if dirpath == del_dir or del_dir in dirpath.parents or dirpath in del_dir.parents:
if str(dirpath).startswith(str(del_dir)):
skip_dir = True
break
if skip_dir:
continue
for filename in filenames:
filepath = dirpath / filename
# 跳过要删除的文件
if filepath in delete_paths:
continue
try:
size = filepath.stat().st_size
remaining_files += 1
remaining_size += size
# 按扩展名统计
ext = filepath.suffix.lower() or "(无扩展名)"
if ext not in remaining_by_ext:
remaining_by_ext[ext] = {"count": 0, "size": 0}
remaining_by_ext[ext]["count"] += 1
remaining_by_ext[ext]["size"] += size
except OSError:
pass
return remaining_files, remaining_size, remaining_by_ext
def main():
parser = argparse.ArgumentParser(description="清理项目构建缓存,导出干净代码")
parser.add_argument("--dry-run", "-n", action="store_true",
help="仅显示将要删除的内容,不实际删除")
parser.add_argument("--copy-to", "-c", type=str,
help="复制干净的项目到指定目录(而不是原地删除)")
parser.add_argument("--keep-exr", action="store_true",
help="保留 EXR 渲染结果文件")
parser.add_argument("--keep-envmaps", action="store_true",
help="保留环境贴图目录")
parser.add_argument("--delete-assets-bin", action="store_true",
help="删除 assets 目录中的 .bin 模型文件")
parser.add_argument("--root", "-r", type=str, default=".",
help="项目根目录(默认为当前目录)")
args = parser.parse_args()
root_path = Path(args.root).resolve()
print(f"项目根目录: {root_path}")
if args.copy_to:
# 复制模式:复制干净的项目到新目录
dest_path = Path(args.copy_to).resolve()
print(f"\n将复制干净的项目到: {dest_path}")
print(f" - 删除 EXR 文件: {'否' if args.keep_exr else '是'}")
print(f" - 删除环境贴图: {'否' if args.keep_envmaps else '是'}")
print(f" - 删除模型 bin 文件: {'是' if args.delete_assets_bin else '否'}")
if not args.dry_run:
copy_clean_project(
root_path, dest_path,
delete_exr=not args.keep_exr,
delete_envmaps=not args.keep_envmaps,
delete_assets_bin=args.delete_assets_bin
)
else:
print("\n[DRY-RUN] 不会实际复制文件")
else:
# 删除模式:原地删除构建缓存
print("\n扫描要删除的项目...")
items = find_items_to_delete(
root_path,
delete_exr=not args.keep_exr,
delete_envmaps=not args.keep_envmaps,
delete_assets_bin=args.delete_assets_bin
)
if not items:
print("没有找到需要删除的项目")
return
# 按大小排序
items.sort(key=lambda x: x[2], reverse=True)
total_delete_size = sum(item[2] for item in items)
print(f"\n找到 {len(items)} 个项目,共 {get_size_str(total_delete_size)}")
print("\n将要删除的项目:")
for item_type, item_path, size in items[:20]: # 显示前20个最大的
print(f" [{item_type:4}] {item_path} ({get_size_str(size)})")
if len(items) > 20:
print(f" ... 还有 {len(items) - 20} 个项目")
# 计算保留的内容
print("\n计算保留的文件...")
remaining_files, remaining_size, remaining_by_ext = calculate_remaining(root_path, items)
print(f"\n{'='*60}")
print(f"📊 统计摘要")
print(f"{'='*60}")
print(f" 🗑️ 将删除: {len(items)} 个项目, {get_size_str(total_delete_size)}")
print(f" ✅ 将保留: {remaining_files} 个文件, {get_size_str(remaining_size)}")
print(f"{'='*60}")
# 按大小排序显示保留的文件类型
print(f"\n📁 保留文件按类型统计 (按大小排序):")
sorted_ext = sorted(remaining_by_ext.items(), key=lambda x: x[1]["size"], reverse=True)
for ext, stats in sorted_ext[:15]: # 显示前15种类型
print(f" {ext:12} : {stats['count']:5} 个文件, {get_size_str(stats['size']):>10}")
if len(sorted_ext) > 15:
print(f" ... 还有 {len(sorted_ext) - 15} 种文件类型")
if args.dry_run:
print(f"\n[DRY-RUN] 如果删除,将释放 {get_size_str(total_delete_size)} 空间")
print(f"[DRY-RUN] 删除后项目大小约为 {get_size_str(remaining_size)}")
else:
print(f"\n确认删除这些项目?(y/n)")
if input().lower() == 'y':
deleted_count, freed_space = delete_items(items)
print(f"\n删除完成!")
print(f"删除了 {deleted_count} 个项目")
print(f"释放了 {get_size_str(freed_space)} 空间")
print(f"项目当前大小约为 {get_size_str(remaining_size)}")
else:
print("取消删除")
if __name__ == "__main__":
main()