2 Utilities for working with unix-style mounts.
7 def list_mount_points():
9 Return list of current mount points
11 Note: unix-like OS specific
14 likely_locations = ['/sbin/mount', '/bin/mount']
15 for mount in likely_locations:
16 if os.path.exists(mount):
17 p = subprocess.Popen(mount, stdout=subprocess.PIPE)
19 for l in p.stdout.readlines():
24 # looking at the output of mount on linux, osx, and
25 # sunos, the first 3 elements are always the same
27 # everything after that displays the attributes
28 # of the mount points in wildly differing formats
29 mount_points.append(mount_point)
32 raise RuntimeError("Couldn't find a mount executable")
34 def is_mounted(point_to_check):
36 Return true if argument exactly matches a current mount point.
38 for mount_point in list_mount_points():
39 if point_to_check == mount_point:
44 def find_mount_point_for(pathname):
46 Find the deepest mount point pathname is located on
48 realpath = os.path.realpath(pathname)
49 mount_points = list_mount_points()
52 for current_mount in mount_points:
53 cp = os.path.commonprefix([current_mount, realpath])
54 prefixes.add((len(cp), cp))
56 prefixes = list(prefixes)
58 if len(prefixes) == 0:
62 # return longest common prefix
63 return prefixes[-1][1]