]> arthur.barton.de Git - bup.git/blob - dev/cleanup-mounts-under
configure/cleanup: support python 3.10
[bup.git] / dev / cleanup-mounts-under
1 #!/bin/sh
2 """": # -*-python-*-
3 # This command is used by "make clean", so don't rely on ./configure
4 set -e
5 for python in \
6     python3 \
7     python3.10 \
8     python3.9 \
9     python3.8 \
10     python3.7 \
11     python3.6 \
12     python \
13     python2.7; do \
14     if command -v "$python"; then
15         exec "$python" "$0" "$@"
16     fi
17 done
18 echo "error: unable to find suitable python executable; please report" 1>&2
19 exit 2
20 """
21
22 from __future__ import absolute_import, print_function
23 from sys import stderr
24 import os.path, re, subprocess, sys
25
26 def mntent_unescape(x):
27     def replacement(m):
28         unescapes = {
29             "\\\\" : "\\",
30             "\\011" : "\t",
31             "\\012" : "\n",
32             "\\040" : " "
33         }
34         return unescapes.get(m.group(0))
35     return re.sub(r'(\\\\|\\011|\\012|\\040)', replacement, x)
36
37 targets = sys.argv[1:]
38
39 if not os.path.exists('/proc/mounts'):
40     print('No /proc/mounts; skipping mount cleanup in', repr(targets),
41           file=stderr)
42     sys.exit(0)
43
44 exit_status = 0
45 for target in targets:
46     if not os.path.isdir(target):
47         print(repr(target), 'is not a directory', file=stderr)
48         exit_status = 1
49         continue
50     top = os.path.realpath(target)
51     proc_mounts = open('/proc/mounts', 'r')
52     for line in proc_mounts:
53         _, point, fstype, _ = line.split(' ', 3)
54         point = mntent_unescape(point)
55         if top == point or os.path.commonprefix((top + '/', point)) == top + '/':
56             if fstype.startswith('fuse'):
57                 if subprocess.call(['fusermount', '-uz', point]) != 0:
58                     exit_status = 1
59             else:
60                 if subprocess.call(['umount', '-l', point]) != 0:
61                     exit_status = 1
62
63 sys.exit(exit_status)