Python 11 5/8/2017


SUBMITTED BY: canonical

DATE: May 8, 2017, 6:04 p.m.

FORMAT: Python

SIZE: 1.6 kB

HITS: 429

  1. If you want more of my pastes visit: https://randompaste.000webhostapp.com/index.html
  2. --------------------------------------------------------------------------------------
  3. view my last post at: https://bitbin.it/47LTjqoD/
  4. --------------------------------------------------------------------------------------
  5. #!/usr/bin/env python3
  6. import os, subprocess, sys
  7. from contextlib import suppress
  8. if "-h" in sys.argv:
  9. with suppress(subprocess.CalledProcessError):
  10. print(subprocess.check_output(["lspci", "-h"], universal_newlines=True))
  11. exit(0)
  12. if "-s" in sys.argv or "-d" in sys.argv:
  13. print("Do not use -s or -d selectors, the script does that for you.",
  14. file=sys.stderr)
  15. exit(1)
  16. groups = sorted(os.scandir("/sys/kernel/iommu_groups"),
  17. key=lambda de: int(de.name))
  18. for i, group in enumerate(groups):
  19. assert group.is_dir(follow_symlinks=False)
  20. if i > 0:
  21. print()
  22. print(f"Group {int(group.name):2d}:")
  23. devicespath = os.path.join(group, "devices")
  24. devices = sorted(os.scandir(devicespath),
  25. key=lambda de: int(de.name.translate(str.maketrans("", "", ":.")), 16))
  26. for device in devices:
  27. assert device.is_symlink()
  28. assert device.is_dir(follow_symlinks=True)
  29. lspci = subprocess.check_output(["lspci", "-s", device.name] + sys.argv[1:],
  30. stderr=subprocess.STDOUT,
  31. universal_newlines=True)
  32. print(lspci.strip())
  33. print()

comments powered by Disqus