-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_clean.py
129 lines (95 loc) · 3.51 KB
/
cache_clean.py
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
import os
import time
import multiprocessing
import sys
import shutil
# Limit the number of threads
max_threads = 100
# Cache deletion threshold in bytes
threshold = 3000
def debug_print(return_data):
print(f'Dirs Deleted: {return_data["dircount"]}')
def single_thread(dirname, return_data, lock):
# shutil.rmtree(dirname)
counter = 0
# Get dirs in current directory
dirs = [os.path.join(dirname, d) for d in os.listdir(dirname) if os.path.isdir(os.path.join(dirname, d))]
print(f'Array list created for {dirname}')
for d in dirs:
# Get directory size in bytes
size = os.path.getsize(d)
# Not deleting the directory if size is greater than threshold
if size > threshold:
continue
# shutil.rmtree(d)
# os.rmdir(d)
# Use rm -rf
os.system(f'rm -rf {d}')
# print(f'Deleting {d}...')
counter += 1
if counter >= 800:
with lock:
return_data['dircount'] += counter
debug_print(return_data)
counter = 0
with lock:
return_data['dircount'] += counter
def multi_threads(dirname, return_data, lock):
# Get all the directories in the current directory
dirs = [os.path.join(dirname, d) for d in os.listdir(dirname) if os.path.isdir(os.path.join(dirname, d))]
# Create a thread for each directory and wait for them to finish
threads = []
# Check if number of directories is greater than max_threads
if len(dirs) > max_threads:
# print(f'Total Chunks: {len(dirs)/max_threads}')
threads_started = 0
print(f'Total Dirs: {len(dirs)}')
finished = 0
for d in dirs:
p = multiprocessing.Process(target=single_thread, args=(d, return_data, lock,))
p.start()
threads_started += 1
threads.append(p)
# Check count of threads
while len(threads) >= max_threads:
for p in threads:
if not p.is_alive():
p.join()
finished += 1
print(f'{finished}/{len(dirs)} threads finished...')
threads.remove(p)
time.sleep(0.1)
for p in threads:
p.join()
finished += 1
print(f'{finished}/{len(dirs)} threads finished...')
print(f'Threads Started: {threads_started}')
else:
for d in dirs:
p = multiprocessing.Process(target=single_thread, args=(d, return_data, lock,))
p.start()
threads.append(p)
print(f'Waiting for {len(threads)} threads to finish...')
finished = 0
for p in threads:
p.join()
finished += 1
print(f'{finished}/{len(threads)} threads finished...')
if __name__ == '__main__':
# Get directory from args
if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]) and sys.argv[1] != "/":
dirname = sys.argv[1]
else:
print(f'Usage: {sys.argv[0]} <dirname>')
sys.exit(1)
manager = multiprocessing.Manager()
return_data = manager.dict()
return_data['dircount'] = 0
# Create a lock
lock = multiprocessing.Lock()
start = time.time()
multi_threads(dirname, return_data, lock)
end = time.time()
# Print number of directories deleted
print(f'Dirs Deleted: {return_data["dircount"]}')
print('Time: ' + str(round(end - start, 2)) + 's')