udiskie / udiskie / umount.py

import warnings
warnings.filterwarnings("ignore", ".*could not open display.*", Warning)
warnings.filterwarnings("ignore", ".*g_object_unref.*", Warning)

import logging
import optparse
import os

import dbus
import gio
import pynotify

import udiskie.device

def unmount_device(device):
    """Unmount a Device.

    Checks to make sure the device is unmountable and then unmounts."""

    logger = logging.getLogger('udiskie.umount.unmount_device')
    if device.is_handleable() and device.is_mounted():
        try:
            device.unmount()
            logger.info('unmounted device %s' % (device,))
        except dbus.exceptions.DBusException, dbus_err:
            logger.error('failed to unmount device %s: %s' % (device,
                                                              dbus_err))
            return

        try:
            pynotify.Notification('Device unmounted',
                                  '%s unmounted' % (device.device_file(),),
                                  'drive-removable-media').show()
        except gio.Error:
            pass
    else:
        logger.debug('skipping unhandled device %s' % (device,))

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')
    bus = dbus.SystemBus()
    for device in udiskie.device.get_all(bus):
        if path in device.mount_paths() or path == device.device_file():
            logger.debug('found device owning "%s": "%s"' % (path, device))
            unmount_device(device)

def unmount_all():
    """Unmount all filesystems handleable by udiskie."""

    bus = dbus.SystemBus()
    for device in udiskie.device.get_all(bus):
        unmount_device(device)

def cli(args):
    logger = logging.getLogger('udiskie.umount.cli')
    parser = optparse.OptionParser()
    parser.add_option('-a', '--all', action='store_true',
                      dest='all', default=False,
                      help='all devices')
    parser.add_option('-v', '--verbose', action='store_true',
                      dest='verbose', default=False,
                      help='verbose output')
    (options, args) = parser.parse_args(args)

    log_level = logging.INFO
    if options.verbose:
        log_level = logging.DEBUG
    logging.basicConfig(level=log_level, format='%(message)s')

    pynotify.init('udiskie.umount')

    if options.all:
        unmount_all()
    else:
        if len(args) == 0:
            logger.warn('No devices provided for unmount')
            return 1

        for path in args:
            unmount(os.path.normpath(path))
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.