-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-empty-dirs
executable file
·67 lines (57 loc) · 1.71 KB
/
clean-empty-dirs
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import re
import subprocess
import shlex
import shutil
import tempfile
import logging
class Cleaner(object):
def __init__(self, root):
self.root = root
self.sub = {}
self.parent = {}
self.non_empty = set()
self.empty_leaves = set()
def Prepare(self):
for current, dirs, files in os.walk(self.root):
assert (current not in self.sub)
self.sub[current] = set()
if not dirs and not files:
self.empty_leaves.add(current)
continue
if files:
self.non_empty.add(current)
logging.info(f'{current} is not empty.')
for d in dirs:
subdir = os.path.join(current, d)
self.sub[current].add(subdir)
if subdir not in self.parent:
self.parent[subdir] = set()
self.parent[subdir].add(current)
def Clean(self):
worklist = list(self.empty_leaves)
while worklist:
d = worklist.pop()
os.rmdir(d)
logging.info(f'Removing {d}')
if d not in self.parent:
continue
for p in self.parent[d]:
self.sub[p].remove(d)
if p not in self.non_empty and not self.sub[p]:
worklist.append(p)
def main():
if len(sys.argv) != 2:
sys.stderr.write(f'Usage: {sys.argv[0]} <root>\n')
return 1
logging.basicConfig(level=logging.INFO)
root = os.path.abspath(sys.argv[1])
C = Cleaner(root)
C.Prepare()
C.Clean()
return 0
if __name__ == '__main__':
sys.exit(main())