Search code examples
pythonudev

How do I use udev to find info about inserted video media (e.g. DVDs)


I'm trying to port an application from using HAL to using pure udev. It is written in python and will use the gudev library, though I would love to see examples in any language. I'm able to get all attached video devices (such as cameras) via:

import gudev

client = gudev.Client(["video4linux"])
for device in client.get_devices():
    print device.get_sysfs_attr("name"), device.get_device_name()

This prints out something like:

USB2.0 UVC WebCam /dev/video0

I am also able to get a list of block devices, but how can I:

  1. Tell if it is a CD/DVD drive?

  2. Tell if media is currently inserted if the drive supports removable media?

  3. Tell what the name/label of the media is (e.g. FUTURAMAS1 for a DVD)?

The original code I am trying to port over is located at http://github.com/danielgtaylor/arista/blob/045a4d48ebfda44bc5d0609618ff795604ee134f/arista/inputs.py

Any and all help would be greatly appreciated!


Update: adding answer below.

import gudev

client = gudev.Client(['block'])
for device in client.query_by_subsystem("block"):
    if device.has_property("ID_CDROM"):
        print "Found CD/DVD drive at %s" % device.get_device_file()
        if device.has_property("ID_FS_LABEL"):
            print "Found disc: %s" % device.get_property("ID_FS_LABEL")
        elif device.has_property("ID_FS_TYPE"):
            print "Found disc"
        else:
            print "No disc"

The code above will output data like:

Found CD/DVD drive at /dev/sr0
Found disc: Ubuntu_10.04_i386

Thanks for the help!


Solution

  • Have a look at the device properties:

    import gudev
    
    client = gudev.Client(['block'])
    for device in client.query_by_subsystem("block"):
        print device
        for device_key in device.get_property_keys():
            print "   property %s: %s" % (device_key, device.get_property(device_key))
        print