File size: 3,817 Bytes
c2d8817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
项目清理脚本

清理不需要的临时文件和缓存
"""

import os
import shutil
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent.parent


def clean_python_cache():
    """清理 Python 缓存"""
    print("🧹 清理 Python 缓存...")
    
    count = 0
    for pycache in PROJECT_ROOT.rglob("__pycache__"):
        if pycache.is_dir():
            shutil.rmtree(pycache)
            count += 1
    
    for pyc in PROJECT_ROOT.rglob("*.pyc"):
        pyc.unlink()
        count += 1
    
    print(f"   ✅ 删除 {count} 个缓存文件/目录")


def clean_temp_uploads():
    """清理临时上传文件"""
    print("🧹 清理临时上传...")
    
    cache_dir = PROJECT_ROOT / "data" / "cache"
    if cache_dir.exists():
        for f in cache_dir.iterdir():
            if f.is_file():
                f.unlink()
            elif f.is_dir():
                shutil.rmtree(f)
        print(f"   ✅ 清理 cache 目录")
    
    # 清理临时 Patient 目录
    raw_dir = PROJECT_ROOT / "data" / "raw"
    processed_dir = PROJECT_ROOT / "data" / "processed"
    
    for d in list(raw_dir.glob("Patient_*")):
        shutil.rmtree(d)
        print(f"   删除: {d.name}")
    
    for d in list(processed_dir.glob("Patient_*")):
        shutil.rmtree(d)
        print(f"   删除: {d.name}")


def clean_logs():
    """清理日志文件"""
    print("🧹 清理日志...")
    
    logs_dir = PROJECT_ROOT / "logs"
    if logs_dir.exists():
        for f in logs_dir.glob("*.log"):
            f.unlink()
            print(f"   删除: {f.name}")


def clean_output():
    """清理输出目录"""
    print("🧹 清理输出目录...")
    
    output_dir = PROJECT_ROOT / "output"
    if output_dir.exists():
        for item in output_dir.iterdir():
            if item.is_file():
                item.unlink()
            elif item.is_dir():
                shutil.rmtree(item)
        print("   ✅ 清理 output 目录")


def show_disk_usage():
    """显示磁盘使用"""
    print("\n📊 磁盘使用统计:")
    
    def get_size(path):
        total = 0
        for f in path.rglob("*"):
            if f.is_file():
                total += f.stat().st_size
        return total
    
    dirs = [
        ("data/raw", PROJECT_ROOT / "data" / "raw"),
        ("data/processed", PROJECT_ROOT / "data" / "processed"),
        ("models", PROJECT_ROOT / "models"),
        ("logs", PROJECT_ROOT / "logs"),
        ("output", PROJECT_ROOT / "output"),
    ]
    
    for name, path in dirs:
        if path.exists():
            size_mb = get_size(path) / (1024 * 1024)
            print(f"   {name}: {size_mb:.1f} MB")


def main():
    """主函数"""
    import argparse
    
    parser = argparse.ArgumentParser(description="项目清理脚本")
    parser.add_argument("--all", action="store_true", help="清理所有")
    parser.add_argument("--cache", action="store_true", help="清理 Python 缓存")
    parser.add_argument("--temp", action="store_true", help="清理临时文件")
    parser.add_argument("--logs", action="store_true", help="清理日志")
    parser.add_argument("--output", action="store_true", help="清理输出")
    parser.add_argument("--stats", action="store_true", help="显示磁盘统计")
    
    args = parser.parse_args()
    
    if args.all or args.cache:
        clean_python_cache()
    
    if args.all or args.temp:
        clean_temp_uploads()
    
    if args.all or args.logs:
        clean_logs()
    
    if args.all or args.output:
        clean_output()
    
    if args.stats or args.all:
        show_disk_usage()
    
    if not any([args.all, args.cache, args.temp, args.logs, args.output, args.stats]):
        parser.print_help()


if __name__ == "__main__":
    main()