繁体   English   中英

使用python和udisk2列出USB驱动器的详细信息

[英]Listing details of USB drives using python and udisk2

我开发了一个应用程序,它使用udisks版本1查找并列出连接的USB驱动器的详细信息。 详细信息包括设备(/dev/sdb1...etc),安装点和可用空间。 但是,我发现现代发行版默认安装了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.py Device 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

编辑请注意, Block对象没有ConnectionBusRemovable属性。 您必须更改代码才能删除对Drive对象属性的引用,以使代码生效。 /编辑

如果你想连接到Block ,而不是Drive ,那么而不是

drive_info = v.get('org.freedesktop.UDisks2.Drive', {})

尝试

drive_info = v.get('org.freedesktop.UDisks2.Block', {})

然后你可以遍历drive_info并输出它的属性。 例如,要获取Id属性,您可以:

print("Id: %s" % drive_info['Id'])

我确信有一个很好的pythonic方法来遍历所有属性键/值对并显示值,但我会留给你。 键为'Id' ,值为存储在drive_info['Id']的字符串。 祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM