tags:

views:

145

answers:

2

Hi,

I'm using python-dbus to interface with HAL, and I need to find a device's UDI based on it's path in the /dev hierarchy.

So given a path such as /dev/sdb, I want to get a value back like /org/freedesktop/Hal/devices/usb_device_10.

+1  A: 

I would spawn a hal-find-by-property call from Python:

import subprocess
def get_UDI(path):
    cmd = 'hal-find-by-property --key block.device --string %s' % path
    proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    output = proc.communicate()
    # stdout
    return output[0].strip()

print get_UDI('/dev/sdb') # /org/freedesktop/Hal/devices/xxxxxx
NicDumZ
+3  A: 

Pure python solution:

import dbus
bus = dbus.SystemBus()
obj = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
iface = dbus.Interface(obj, "org.freedesktop.Hal.Manager")
print iface.FindDeviceStringMatch("block.device", "/dev/sda")
abbot
+1 for teaching me how to use dbus :)
NicDumZ