Volume id

Dieter Verfaillie dieterv at optionexplicit.be
Fri Nov 16 08:52:12 EST 2007


On Thu, 2007-11-15 at 17:05 +0100, Gabor Urban wrote:
> OK, you are right... Problem was not precise enough. I need to process
> CDs to create a list. Does it ring a bell for you?
> 
> Thanks

Hello,

The method below will work on linux systems (it uses dbus to communicate
with HAL). You'll maybe have to expand the filter on line 37, but I'm
not sure...

hth,
Dieter

#!/usr/bin/env python

import dbus

def discover():
    disks = []
    volumes = []

    # get a connection to the system bus
    bus = dbus.SystemBus ()

    # get a HAL object and an interface to HAL to make function calls
    hal_obj = bus.get_object ('org.freedesktop.Hal',
                              '/org/freedesktop/Hal/Manager')
    hal = dbus.Interface (hal_obj, 'org.freedesktop.Hal.Manager')

    # find all devices that have the capability 'volume'
    udis = hal.FindDeviceByCapability('volume')

    for udi in udis:
        # get volume info
        dev_obj = bus.get_object('org.freedesktop.Hal', udi)
        dev = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device')
        volume = str(dev.GetProperty('block.device'))
        volume_label = str(dev.GetProperty('volume.label'))
        volume_mount_point = str(dev.GetProperty('volume.mount_point'))
        volume_fstype = str(dev.GetProperty('volume.fstype'))

        # get storage info
        parent_udi = dev.GetProperty('info.parent')
        dev_obj = bus.get_object('org.freedesktop.Hal', parent_udi)
        dev = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device')
        storage = str(dev.GetProperty('block.device'))
        storage_product = str(dev.GetProperty('info.product'))

        # filter out hard disks
        if dev.GetProperty('storage.drive_type') == 'disk':
            continue

        # store disk
        if not storage in disks:
            disks.append(storage)

        # store volume
        volumes.append((storage,
                        volume,
                        volume_label,
                        volume_mount_point,
                        volume_fstype))

    return disks, volumes

if __name__ == '__main__':
    disks, volumes = discover()

    for disk in disks:
        print 'found disk', disk

        for volume in volumes:
            if volume[0] == disk:
                print '    with volume', volume[1]
                print '        label', volume[2]
                print '        mount point is', volume[3]
                print '        fstype is', volume[4]

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20071116/c4bdcf34/attachment.sig>


More information about the Python-list mailing list