-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_venvs.py
88 lines (78 loc) · 3.01 KB
/
delete_venvs.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
import sys
import shutil
import os
tot_size = 0
def get_size(start_path = '.'):
"""
Returns total size of a path in bytes
"""
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
# skip if it is symbolic link
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return total_size
def delete_folder(folder_path):
shutil.rmtree(folder_path) # removes the complete folder given at folder path
print(f"[-] Deleted :{folder_path}")
def main(auto_delete:bool):
"""
Enter path to the text file generated by `detect_venv.py`
Then follow on screen instructions to delete the folder or skip them
(easy way to restore disk space without deleting project files!)
"""
search_results_path = input("Enter path to search results by detect_venv.py:")
global tot_size
tot_size = 0
skipped = []
if os.path.exists(search_results_path) and os.path.isfile(search_results_path):
with open(search_results_path, 'r') as results_file:
for line in results_file:
name, location = line.strip('\n').split(':',maxsplit=1)
size_mb = get_size(location) / 1000 / 1024
tot_size += size_mb
if not size_mb > 0:
print(f"-----skipped-----({name})\n")
continue
if not auto_delete:
print(f"confirm to delete: {name}:{location} [size: {size_mb:.03f}] ? (y/N): ", end="")
res = input()
else:
res = 'y'
if res == 'y' or res == 'Y':
print("\tDeleting...")
try:
delete_folder(location)
except FileNotFoundError:
pass
else:
print(f"-----skipped-----({name})\n")
tot_size -= size_mb
skipped.append(line.strip('\n'))
print(f"\nSaved [{tot_size:.02f} MB] of Disk-Space")
if skipped:
print("[*] updating results file")
with open('temp.txt', 'w') as file:
file.write('\n'.join(skipped))
os.remove(search_results_path)
os.rename('temp.txt', search_results_path)
print(f"[+] File updated")
else:
print("Invalid file location entered, expecting a *.txt file.")
print(f"{16*'---'}\nThat's all for now!")
if __name__ == "__main__":
argv = sys.argv
auto_delete = False
if len(argv) > 1:
command = argv[1]
if command == '-y': # all auto delete
print("You have chosen to automatically delete all listed virtualenvs in the directory...")
auto_delete = True
print("command:{argv[2]}")
try:
main(auto_delete)
except KeyboardInterrupt:
if tot_size > 0:
print(f"\nSaved [{tot_size:.02f} MB] of Disk-Space")