add facts script for usb and pci ids

This commit is contained in:
Alexander Grothe 2017-02-28 16:07:33 +01:00
parent a731768b7c
commit 27b5cd0500
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# This script returns a list of Vendor- and Product-IDs for all connected usb
# and pci(e) devices in json format
import glob
import json
import os
import sys
import usb.core
from collections import namedtuple
Device = namedtuple("Device", ['idVendor', 'idProduct'])
def get_pci_devices():
for device in glob.glob('/sys/devices/pci*/*:*:*/'):
with open(os.path.join(device, 'device')) as d:
product_id = int(d.read().strip(), 16)
with open(os.path.join(device, 'vendor')) as d:
vendor_id = int(d.read().strip(), 16)
yield Device(idVendor=vendor_id, idProduct=product_id)
def format_device_list(iterator):
return ["{:04x}:{:04x}".format(d.idVendor, d.idProduct) for d in iterator]
if __name__ == '__main__':
usb_devices = format_device_list(usb.core.find(find_all=True))
pci_devices = format_device_list(get_pci_devices())
print(json.dumps({'usb': usb_devices, 'pci': pci_devices}))