]> arthur.barton.de Git - bup.git/blob - t/cleanup-mounts-under
Add "/t/sampledata/var" to .gitignore
[bup.git] / t / cleanup-mounts-under
1 #!/usr/bin/env python
2
3 import os.path, re, subprocess, sys
4
5 def mntent_unescape(x):
6     def replacement(m):
7         unescapes = {
8             "\\\\" : "\\",
9             "\\011" : "\t",
10             "\\012" : "\n",
11             "\\040" : " "
12         }
13         return unescapes.get(m.group(0))
14     return re.sub(r'(\\\\|\\011|\\012|\\040)', replacement, x)
15
16 targets = sys.argv[1:]
17 exit_status = 0
18 for target in targets:
19     if not os.path.isdir(target):
20         print >> sys.stderr, target, 'is not a directory'
21         exit_status = 1
22         continue
23     top = os.path.realpath(target)
24     proc_mounts = open('/proc/mounts', 'r')
25     for line in proc_mounts:
26         _, point, fstype, _ = line.split(' ', 3)
27         point = mntent_unescape(point)
28         if top == point or os.path.commonprefix((top + '/', point)) == top + '/':
29             if fstype.startswith('fuse'):
30                 if subprocess.call(['fusermount', '-uz', point]) != 0:
31                     exit_status = 1
32             else:
33                 if subprocess.call(['umount', '-l', point]) != 0:
34                     exit_status = 1
35
36 sys.exit(exit_status)