kjakeb
kjakeb

Reputation: 7590

problem printing mount point of usb in python

I have a program written in python that uses dbus to detect inserted usb drives and prints the dir they are mounted on when they are detected. Here is the code:

 
import dbus
import gobject
import shutil
import os
import subprocess
import time

class DeviceAddedListener:
    def __init__(self):
    self.bus = dbus.SystemBus()
        self.hal_manager_obj = self.bus.get_object(
                                              "org.freedesktop.Hal", 
                                              "/org/freedesktop/Hal/Manager")
        self.hal_manager = dbus.Interface(self.hal_manager_obj,
                                          "org.freedesktop.Hal.Manager")
    self.hal_manager.connect_to_signal("DeviceAdded", self._filter)

    def _filter(self, udi):
        device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
        device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")

        if device.QueryCapability("volume"):
            return self.do_something(device)

    def do_something(self, volume):
        device_file = volume.GetProperty("block.device")
        label = volume.GetProperty("volume.label")
        fstype = volume.GetProperty("volume.fstype")
        mounted = volume.GetProperty("volume.is_mounted")
        mount_point = volume.GetProperty("volume.mount_point")
        try:
            size = volume.GetProperty("volume.size")
        except:
            size = 0
    p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["grep", device_file], stdin=p1.stdout, stdout=subprocess.PIPE)
    p3 = subprocess.Popen(["awk", "{ print $6 }"], stdin=p2.stdout, stdout=subprocess.PIPE)
    path = p3.communicate()[0]
    print path



if __name__ == '__main__':
    from dbus.mainloop.glib import DBusGMainLoop
    DBusGMainLoop(set_as_default=True)
    loop = gobject.MainLoop()
    DeviceAddedListener()
    loop.run()

The problem is that when I print the path variable (the mount point of the usb), it prints an empty string. However, when I execute these same commands (Popen(), etc) in the python interactive interpreter, it prints the path just fine (/media/03CB-604C). Why does this occur? Any edits / suggestions to my code would be much appreciated. Thanks in advance!

Upvotes: 2

Views: 934

Answers (1)

ed.
ed.

Reputation: 1393

In your original question it seems likely that you're being beaten by a race condition.

The device is inserted and your code is executed before the mounting process has completed.

Try putting the Popen calls in a while loop (see below).

path = ""
count = 0
while count < 10 and path == "":
    p1 = subprocess.Popen(["df", "-h"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["grep", device_file], stdin=p1.stdout, stdout=subprocess.PIPE)
    p3 = subprocess.Popen(["awk", "{ print $6 }"], stdin=p2.stdout, stdout=subprocess.PIPE)
    path = p3.communicate()[0]
    count += 1
    if path == "":
        time.sleep(1)
print path

This is a bit of a resource-hungry solution, but it should do what you want.

Upvotes: 1

Related Questions