-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmdsstore.py
57 lines (50 loc) · 1.93 KB
/
rmdsstore.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
import re
import shutil
if len(sys.argv) <= 1:
print("No argument provided.")
exit(1)
def make_width(p):
width = os.get_terminal_size()[0]
return p.ljust(width, ' ')
def smart_gen_progress(p, prompt='Scanning: {}...', newline=False) :
p2 = re.sub('[^ a-zA-Z0-9!@#$%^&*()_+={}[\\]|\\\\:;"\'<>,.?\\/~`-]', '?', p)
if p2.startswith(sys.argv[1]):
p2 = p2[(len(sys.argv[1]) + 1):]
empty = len(prompt) - 2
width = os.get_terminal_size()[0]
max_allowed = width - empty
sys.stdout.write(make_width(prompt.format(p2[:max_allowed])))
if newline:
sys.stdout.write('\n')
else:
sys.stdout.write('\r')
sys.stdout.flush()
def rm(top):
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
print("Running rmdsstore on \"{}\".".format(sys.argv[1]))
for root, dirs, files in os.walk(sys.argv[1], followlinks=False):
dirs[:] = [d for d in dirs if not d.endswith('.duck')]
smart_gen_progress(root)
for name in dirs:
if name == '$RECYCLE.BIN':
smart_gen_progress(os.path.join(root, name), prompt='Removing {}...', newline=True)
try:
shutil.rmtree(os.path.join(root, name), ignore_errors=True)
except:
smart_gen_progress(os.path.join(root, name), prompt='Could not remove {}.', newline=True)
for name in files:
if name == '.DS_Store' or name == 'Thumbs.db' or name == 'desktop.ini':
smart_gen_progress(os.path.join(root, name), prompt='Removing {}...', newline=True)
try:
os.remove(os.path.join(root, name))
except:
smart_gen_progress(os.path.join(root, name), prompt='Could not remove {}.', newline=True)
print("\nFinished.\n")