Byron Clark avatar Byron Clark committed a961b53

Welcome udiskie-umount to the family.

Comments (0)

Files changed (6)

File contents unchanged.

bin/udiskie-umount

+#! /usr/bin/env python
+
+if __name__ == '__main__':
+    import sys
+    import udiskie.umount
+
+    udiskie.umount.cli(sys.argv)

udiskie/__init__.py

+import dbus
+
+__all__ = ['system_bus']
+
+system_bus = dbus.SystemBus()
+UDISKS_OBJECT = 'org.freedesktop.UDisks'
+UDISKS_OBJECT_PATH = '/org/freedesktop/UDisks'
+
+UDISKS_INTERFACE = 'org.freedesktop.UDisks'
+UDISKS_DEVICE_INTERFACE = 'org.freedesktop.UDisks.Device'
+
+DBUS_PROPS_INTERFACE = 'org.freedesktop.DBus.Properties'

udiskie/umount.py

+import logging
+
+from udiskie import system_bus
+from udiskie.names import UDISKS_OBJECT, UDISKS_DEVICE_INTERFACE
+import udiskie.util
+
+def unmount(path):
+    """Unmount a filesystem
+
+    The filesystem must match the criteria for a filesystem mountable by
+    udiskie.  path is either the physical device node (e.g. /dev/sdb1) or the
+    mount point (e.g. /media/Foo)."""
+    
+    logger = logging.getLogger('udiskie.umount.unmount')
+    device_path = udiskie.util.find_device(path)
+    if udiskie.util.handleable(device_path) \
+       and udiskie.util.mounted(device_path):
+        logger.info('Unmounting %s (udisks path: %s)' % (path, device_path))
+        device_object = system_bus.get_object(UDISKS_OBJECT, device_path)
+        device_object.FilesystemUnmount([],
+                                        dbus_interface=UDISKS_DEVICE_INTERFACE)
+        logger.info('Finished unmounting %s' % (path,))
+
+def cli(args):
+    for path in args[1:]:
+        unmount(path)
+import logging
+
+from udiskie import system_bus
+import udiskie.names as names
+
+def _get_all_devices():
+    obj = system_bus.get_object(names.UDISKS_OBJECT,
+                                names.UDISKS_OBJECT_PATH)
+    return obj.EnumerateDevices(dbus_interface=names.UDISKS_INTERFACE)
+
+def _get_property(device, prop):
+    device_obj = system_bus.get_object(names.UDISKS_OBJECT, device)
+    return device_obj.Get(names.UDISKS_DEVICE_INTERFACE, prop, dbus_interface=names.DBUS_PROPS_INTERFACE)
+
+def find_device(path):
+    logger = logging.getLogger('udiskie.util.find_device')
+
+    for device in _get_all_devices():
+        logger.debug('examining %s' % (device,))
+
+        mounted_paths = _get_property(device, 'DeviceMountPaths')
+        if path in mounted_paths:
+            return device
+
+        # device path
+        device_file = _get_property(device, 'DeviceFile')
+        if path == device_file:
+            return device
+
+def handleable(device_path):
+    """Check if the device should be handled by udiskie.
+    
+    Right now this just means that the device is removable."""
+
+    return _get_property(device_path, 'DeviceIsRemovable')
+
+def mounted(device_path):
+    """Check if the device is currently mounted."""
+
+    return _get_property(device_path, 'DeviceIsMounted')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.