Debian Codegraph @

dev/cleanup-mounts-under

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