fix resolv.conf issue on plc
[myplc.git] / clean-empty-dirs.py
1 #!/usr/bin/python
2 ###
3 ### $Id$
4 ###
5 ### utility script for cleaning empty directories
6 ### useful to clean up /var/tmp/bootmedium
7 ### 
8
9 """
10 Usage: $0 dir [ .. dir]
11 scans all provided directories and prunes any empty directory under
12 the arg directories are always preserved 
13 """
14
15 import os,sys
16
17 ### cleans up a everything under a given root
18 def clean_root (path, cleanRoot = False):
19
20     if not os.path.isdir(path):
21         return
22     
23     # scan dir contents
24     files=os.listdir(path)
25
26     for x in files:
27         fullpath=os.path.join(path, x)
28         if os.path.isfile(fullpath):
29             # we do not remove files
30             return
31         elif os.path.isdir(fullpath):
32             clean_root(fullpath,True)
33
34     if (cleanRoot):
35         # rescan, and clean if empty
36         files=os.listdir(path)
37         if not files:
38             os.rmdir(path)
39
40 ERROR_STR= """Error removing %(path)s, %(error)s """
41
42 def main (dirs):
43
44     for dir in dirs:
45         try:
46             if dir.index("/") != 0:
47                 print "%s: args must be absolute paths"%(sys.argv[0])
48                 print "%s: %s ignored"%(sys.argv[0],dir)
49             else:
50                 clean_root(dir)
51         except OSError, (errno, strerror):
52             print ERROR_STR % {'path' : path, 'error': strerror }
53
54 if __name__ == '__main__':
55     main (sys.argv[1:])