-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp_cleaner.py
171 lines (115 loc) · 4.07 KB
/
tmp_cleaner.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
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
import glob
import os
import sys
def delete_files(files):
print("usuwam: ")
undeleted = []
for file in files:
print(" "+file)
try:
os.remove(file)
except Exception as exc:
print("Blad usuwania pliku: "+str(exc))
undeleted.append(file)
return undeleted
def list_files(extensions, path="."):
files = []
for ftype in extensions:
for filename in glob.glob(os.path.abspath(path) + '/' + '**/' + ftype, recursive=True):
if os.path.isfile(filename):
files.append(filename)
return files
def print_list(files):
max_len = len(max(files, key=len))
total_size = 0
print("Plik".ljust(max_len + 3 + 4, ' ') + "Rozmiar (b)")
for filename in files:
print(" "+filename.ljust(max_len + 3, ' ') + str(os.stat(filename).st_size))
total_size += os.stat(filename).st_size
print("\nLiczba plikow: ".ljust(max_len + 3 + 4 + 1, ' ') + str(len(files)))
print("Calkowity rozmiar (b): ".ljust(max_len + 3 + 4, ' ') + str(total_size))
def confirm():
while True:
print("Usunac? tak/nie")
decision = input()
if decision == "tak":
return True
elif decision == "nie":
return False
def delete(files):
confirming_answer = "usun"
print(str(len(files)) + " plikow zostanie usunietych wpisz \"" + confirming_answer + "\"")
if confirming_answer == input():
undeleted = delete_files(files)
if len(files) - len(undeleted) == 0:
print("zadne pliki nie zostaly usuniete.")
elif len(undeleted) == 0:
print("\npliki zostaly usuniete.")
else:
print("UWAGA: nie wszystkie pliki zostaly usuniete (" + str(len(undeleted)) + "):")
un_count = 1
for uf in undeleted:
print(" "+str(un_count)+". "+uf)
un_count += 1
else:
print("Pliki nie zostaly usuniete.")
def specify_extensions():
default_extensions = ['*.o', '*.obj', '*.class', '*.out'] # EXTENSIONS LIST
print("Domyslna lista rozszerzen (enter, aby uzyc): " + ' '.join(default_extensions) +
"\nlub wprowadz teraz wlasna liste, oddzielajac rozszerzenia spacjami (bez gwiazdek i kropek):")
custom_extensions = input()
if custom_extensions == "":
print("Zostanie uzyta domyslna lista.")
return default_extensions
else:
print("Zostanie uzyta lista uzytkownika.")
return ["*." + ext for ext in custom_extensions.split()]
def prepare_directories():
if len(sys.argv) > 1:
directories = sys.argv
directories.pop(0)
result = []
print("Wybrane katalogi: ")
for dirct in directories:
cut_size = 0
for i in range(len(dirct)-1, -1, -1):
if dirct[i] == "\\":
cut_size += 1
else:
break
if cut_size > 0:
dirct = dirct[:-cut_size]
if not os.path.isdir(dirct):
print("Blad: \""+dirct+"\" nie jest katalogiem")
exit(1)
else:
result.append(dirct)
result = list(set(result))
result.sort()
for dirct in result:
print(dirct)
return result # delete duplicates from list
else:
return ["."]
def create_filelist(directories, extensions):
files = []
for directory in directories:
files += list_files(extensions, directory)
files = list(set(files))
files.sort()
return files
def main():
directories = prepare_directories()
extensions = specify_extensions()
print("Pliki z rozszerzeniami " + ' '.join(extensions) + " zostana usuniete.")
files = create_filelist(directories, extensions)
if len(files) != 0:
print_list(files)
if confirm():
delete(files)
else:
print("Zadne pliki nie zostaly usuniete.")
else:
print("Nie znaleziono pasujacych plikow.")
if __name__ == "__main__":
main()