From 27b5cd05006c13c3f1f5d014540f5f6bb69b6cac Mon Sep 17 00:00:00 2001 From: Alexander Grothe Date: Tue, 28 Feb 2017 16:07:33 +0100 Subject: [PATCH] add facts script for usb and pci ids --- roles/yavdr-common/files/hardware.facts.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 roles/yavdr-common/files/hardware.facts.py diff --git a/roles/yavdr-common/files/hardware.facts.py b/roles/yavdr-common/files/hardware.facts.py new file mode 100755 index 0000000..854b50d --- /dev/null +++ b/roles/yavdr-common/files/hardware.facts.py @@ -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}))