/
clean.py
executable file
·65 lines (57 loc) · 1.71 KB
/
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
#!/usr/bin/python
import os, sys, shutil
TOBE_RM = ['.log', '.aux', '.out', '.snm', '.toc', '.vrb', '.nav', '.blg', '.bbl','.thm', '.auxlock', '.md5', '.idx', 'auto', '__pycache__']
def clean(path):
"""
clean latex generated temporary files
"""
removed_list = []
files = os.listdir(path)
for file_name in files:
f = os.path.abspath(os.path.join(path, file_name))
if os.path.isfile(f):
ext = os.path.splitext(f)[-1]
if (ext in TOBE_RM) or (ext.endswith("~")):
print '** Remove file ', f
os.remove(f)
removed_list.append(f)
elif os.path.isdir(f) and os.path.split(f)[-1] in TOBE_RM:
print '** Remove dir ', f
shutil.rmtree(f)
removed_list.append(f)
return removed_list
def get_sub_dirs(path):
dirs = []
files = os.listdir(path)
for file_name in files:
f = os.path.abspath(os.path.join(path, file_name))
if os.path.isdir(f) and not f.startswith('.') and not f.endswith('/.git'):
dirs.append(f)
return dirs
def process(path):
"""
scan path and its sub paths, remove latex generated temp files,
and store them in list.
"""
total_rm_list = []
print ' process dir ', path, '...'
files = os.listdir(path)
for file_name in files:
f = os.path.abspath(os.path.join(path, file_name))
if os.path.isfile(f):
ext = os.path.splitext(f)[-1]
if ext == '.tex':
total_rm_list.extend(clean(path))
break
sub_dirs = get_sub_dirs(path)
for dir in sub_dirs:
total_rm_list.extend(process(dir))
return total_rm_list
if __name__=='__main__':
rm_list = process('.')
print '-----------------------------'
print len(rm_list), ' temp files are removed.'
print '-----------------------------'
for item in rm_list:
print ' ==>', item
print len(rm_list), ' temp files are removed.'