| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import dbus |
|---|
| 4 | import gobject |
|---|
| 5 | import time |
|---|
| 6 | import os |
|---|
| 7 | import logging |
|---|
| 8 | import subprocess |
|---|
| 9 | from dbus.mainloop.glib import DBusGMainLoop |
|---|
| 10 | |
|---|
| 11 | class DriveVolumeListener(object): |
|---|
| 12 | def __init__(self, uuid): |
|---|
| 13 | self._uuid = uuid |
|---|
| 14 | self._active_udis = set() |
|---|
| 15 | |
|---|
| 16 | dbus_loop = DBusGMainLoop() |
|---|
| 17 | self.bus = dbus.SystemBus(mainloop=dbus_loop) |
|---|
| 18 | |
|---|
| 19 | hal_manager_obj = self.bus.get_object('org.freedesktop.Hal','/org/freedesktop/Hal/Manager') |
|---|
| 20 | hal_manager = dbus.Interface(hal_manager_obj,'org.freedesktop.Hal.Manager') |
|---|
| 21 | |
|---|
| 22 | hal_manager.connect_to_signal('DeviceAdded', self.device_added_callback) |
|---|
| 23 | hal_manager.connect_to_signal('DeviceRemoved', self.device_removed_callback) |
|---|
| 24 | |
|---|
| 25 | logging.info("Listening for volume with uuid %s." % (self._uuid,)) |
|---|
| 26 | |
|---|
| 27 | def device_added_callback(self,udi): |
|---|
| 28 | """Callback for listening to devices being added.""" |
|---|
| 29 | logging.debug("Saw adding of udi %s." % (udi,)) |
|---|
| 30 | dev_obj = self.bus.get_object('org.freedesktop.Hal', udi) |
|---|
| 31 | dev_iface = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device') |
|---|
| 32 | |
|---|
| 33 | if dev_iface.QueryCapability('volume') and dev_iface.GetProperty('volume.uuid') == self._uuid: |
|---|
| 34 | logging.info("Volume %s mounted." % (self._uuid,)) |
|---|
| 35 | self._active_udis.add(udi) |
|---|
| 36 | self.volume_mounted(dev_obj) |
|---|
| 37 | |
|---|
| 38 | def device_removed_callback(self,udi): |
|---|
| 39 | """Callback for listening to devices being removed.""" |
|---|
| 40 | logging.debug("Saw removal of udi %s." % (udi,)) |
|---|
| 41 | |
|---|
| 42 | if udi in self._active_udis: |
|---|
| 43 | logging.info("Volume %s unmounted." % (self._uuid,)) |
|---|
| 44 | self._active_udis.remove(udi) |
|---|
| 45 | self.volume_unmounted(udi) |
|---|
| 46 | |
|---|
| 47 | def volume_mounted(self,dev_obj): |
|---|
| 48 | """Called when the volume is mounted (sub-classes should override this).""" |
|---|
| 49 | pass |
|---|
| 50 | |
|---|
| 51 | def volume_unmounted(self,udi): |
|---|
| 52 | """Called when the volume is unmounted (sub-classes should override this).""" |
|---|
| 53 | pass |
|---|
| 54 | |
|---|
| 55 | class ActionLauncher(DriveVolumeListener): |
|---|
| 56 | MOUNT_POINT_MARKER = object() |
|---|
| 57 | |
|---|
| 58 | def __init__(self, uuid, script, subfolder): |
|---|
| 59 | super(ActionLauncher, self).__init__(uuid) |
|---|
| 60 | self._script = script |
|---|
| 61 | self._subfolder = subfolder |
|---|
| 62 | self._mount_pos = script.index(self.MOUNT_POINT_MARKER) |
|---|
| 63 | |
|---|
| 64 | def volume_mounted(self,dev_obj): |
|---|
| 65 | try: |
|---|
| 66 | vol_iface = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device.Volume') |
|---|
| 67 | dev_iface = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device') |
|---|
| 68 | # vol_iface.Mount(self._mount_point, "", dbus.Array(signature="s")) |
|---|
| 69 | |
|---|
| 70 | tries_left = 10 |
|---|
| 71 | mount_point = dev_iface.GetProperty('volume.mount_point') |
|---|
| 72 | while tries_left and not mount_point: |
|---|
| 73 | time.sleep(1.0) |
|---|
| 74 | tries_left -= 1 |
|---|
| 75 | mount_point = dev_iface.GetProperty('volume.mount_point') |
|---|
| 76 | |
|---|
| 77 | logging.info("Volume mounted on %s." % (mount_point,)) |
|---|
| 78 | |
|---|
| 79 | enc_folder = os.path.join(mount_point, self._subfolder) |
|---|
| 80 | while tries_left and not os.path.exists(enc_folder): |
|---|
| 81 | time.sleep(1.0) |
|---|
| 82 | tries_left -= 1 |
|---|
| 83 | |
|---|
| 84 | logging.info("Found encryption folder at %s." % (enc_folder,)) |
|---|
| 85 | |
|---|
| 86 | if tries_left: |
|---|
| 87 | self._script[self._mount_pos] = enc_folder |
|---|
| 88 | retcode = subprocess.call(self._script) |
|---|
| 89 | except Exception, e: |
|---|
| 90 | logging.error(str(e)) |
|---|
| 91 | |
|---|
| 92 | def volume_unmounted(self,udi): |
|---|
| 93 | # vol_iface = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device.Volume') |
|---|
| 94 | # vol_iface.Unmount(dbus.Array(signature="s")) |
|---|
| 95 | pass |
|---|
| 96 | |
|---|
| 97 | UUID = "4298-61BB" |
|---|
| 98 | SCRIPT = ["/usr/bin/encfs", "--extpass=/usr/bin/ssh-askpass", "--no-default-flags", |
|---|
| 99 | ActionLauncher.MOUNT_POINT_MARKER, "/home/simon/keys"] |
|---|
| 100 | SUBFOLDER = "keys" |
|---|
| 101 | |
|---|
| 102 | logging.basicConfig(level=logging.DEBUG, |
|---|
| 103 | filemode='a') |
|---|
| 104 | |
|---|
| 105 | oL = ActionLauncher(UUID, SCRIPT, SUBFOLDER) |
|---|
| 106 | |
|---|
| 107 | mainloop = gobject.MainLoop() |
|---|
| 108 | mainloop.run() |
|---|