我开发了一个应用程序,它使用 udisks 版本 1 来查找和列出连接的 USB 驱动器的详细信息。详细信息包括设备(/dev/sdb1...等)、挂载点和可用空间。但是,我发现现代发行版默认安装了 udisks2。这是在另一个 SO 线程上找到的小代码:-

#!/usr/bin/python2.7
import dbus
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2')
om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
for k,v in om.GetManagedObjects().iteritems():
  drive_info = v.get('org.freedesktop.UDisks2.Drive', {})
  if drive_info.get('ConnectionBus') == 'usb' and drive_info.get('Removable'):
    if drive_info['MediaRemovable']:
      print("Device Path: %s" % k)

它产生:-
[sundar@arch ~]$ ./udisk2.pyDevice Path: /org/freedesktop/UDisks2/drives/JetFlash_Transcend_8GB_GLFK4LYSFG3HZZ48
上面的结果很好,但是如何连接 org.freedesktop.UDisks2.Block 并获取设备的属性?

http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Block.html

最佳答案

经过大量的打击和试验,我可以得到我想要的。只是张贴它,以便将来有人可以受益。这是代码:-

#!/usr/bin/python2.7
# coding: utf-8
import dbus


def get_usb():
    devices = []
    bus = dbus.SystemBus()
    ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2')
    om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
    try:
        for k,v in om.GetManagedObjects().iteritems():
            drive_info = v.get('org.freedesktop.UDisks2.Block', {})
            if drive_info.get('IdUsage') == "filesystem" and not drive_info.get('HintSystem') and not drive_info.get('ReadOnly'):
                device = drive_info.get('Device')
                device = bytearray(device).replace(b'\x00', b'').decode('utf-8')
                devices.append(device)
    except:
        print "No device found..."
    return devices



def usb_details(device):
    bus = dbus.SystemBus()
    bd = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2/block_devices%s'%device[4:])
    try:
        device = bd.Get('org.freedesktop.UDisks2.Block', 'Device', dbus_interface='org.freedesktop.DBus.Properties')
        device = bytearray(device).replace(b'\x00', b'').decode('utf-8')
        print "printing " + device
        label = bd.Get('org.freedesktop.UDisks2.Block', 'IdLabel', dbus_interface='org.freedesktop.DBus.Properties')
        print 'Name od partition is %s'%label
        uuid = bd.Get('org.freedesktop.UDisks2.Block', 'IdUUID', dbus_interface='org.freedesktop.DBus.Properties')
        print 'UUID is %s'%uuid
        size = bd.Get('org.freedesktop.UDisks2.Block', 'Size', dbus_interface='org.freedesktop.DBus.Properties')
        print 'Size is %s'%uuid
        file_system =  bd.Get('org.freedesktop.UDisks2.Block', 'IdType', dbus_interface='org.freedesktop.DBus.Properties')
        print 'Filesystem is %s'%file_system
    except:
        print "Error detecting USB details..."

完整的块设备属性可以在这里找到 http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Block.html

关于python - 使用 python 和 udisks2 列出 USB 驱动器的详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23244245/

10-10 09:23