D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] Detect USB drive insertion with Python and udev

 

Hi all,

I'm trying to plug together a few things that have been floating
around in my head lately using some lovely Python glue, but being the
less-than-seasoned coder that I am, I'm having a spot of difficulty
getting my head around something.

Essentially, what I'm after doing is this:
Software checks mtab to see if /media/something is mounted
If it is, check if /media/something/somefile exists, and if it does
then continue with the rest of the code, otherwise tell user that file
is missing and where to get it.
If /media/something is not mounted, inform user to insert the relevant
USB drive... and then go and wait for the USB drive to be inserted.
Once USB drive has been inserted, go back and check if
/media/something/somefile exists, etc, etc.

I have it all running nicely, except for the "wait for USB to be
inserted" bit... and that half works.

I have the following code:

import string, time, os, dbus, gobject, sys
from dbus.mainloop.glib import DBusGMainLoop

def device_added_callback(device):
  print ("... ... ... device inserted")
  usbdev = "".join(device.split("/")[5:6])
  print ("... ... ... does it contain a 1 at the end?")
  if usbdev.endswith("1") == 1:
    print ("... ... ... ... yes!")

def waitforusb():
  print ("... ... starting DBUS interface detection")
  DBusGMainLoop(set_as_default=True)
  bus = dbus.SystemBus()
  proxy = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  iface = dbus.Interface(proxy, "org.freedesktop.UDisks")
  devices = iface.get_dbus_method('EnumerateDevices')()
  iface.connect_to_signal('DeviceAdded', device_added_callback)
  mainloop = gobject.MainLoop()
  mainloop.run()

If USB is not present, waitforusb gets called. This connects to dbus
and will call device_added_callback if/when a USB disk is inserted. So
far, this works. However... I want to be able to drop out of
device_added_callback when the device has been added, and I want to
stop mainloop from running - I only need to wait for the USB to be
inserted once, and I don't need to know if any more are added later.

I have tried adding "mainloop.quit()" to the if in
device_added_callback, but it complains that mainloop doesn't exist. I
have tried adding mainloop.quit() after mainloop.run() in
waitforusb(), but it never gets called. I have tried duplicating most
of the code from DBusGMainLoop onwards inside device_added_callback
(so that mainloop DOES exist), but presumably it essentially becomes a
/new/ mainloop, as when I call mainloop.quit() nothing happens.

So, how to I get mainloop to quit once the USB device has been
successfully added, registered and enumerated?

I could achieve what I'm after by checking mtab repeatedly (after a
delay), but I thought using dbus would be a more elegant solution (and
an interesting learning project).

Cheers.
Grant. :)

-- 
The Mailing List for the Devon & Cornwall LUG
http://mailman.dclug.org.uk/listinfo/list
FAQ: http://www.dcglug.org.uk/listfaq