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