1#!/usr/bin/env python3

2

3 import sys

4 from io import StringIO

5 from subprocess import Popen, PIPE, check_output, STDOUT, CalledProcessError

6 from checkbox_support.parsers.udevadm import UdevadmParser

7 from checkbox_support.parsers.modinfo import ModinfoParser

8

9

10 class TouchpadDriver:

11

12 def __init__(self, driver):

13 self._driver = driver

14 self.driver_version = self._find_driver_version()

15

16 def _find_driver_version(self):

17 cmd = ["/sbin/modinfo", self._driver]

18 try:

19 stream = check_output(cmd, stderr=STDOUT, universal_newlines=True)

20 except CalledProcessError as err:

21 print("Error communicating with modinfo.")

22 print(err.output)

23 return None

24

25 if not stream:

26 print("Error: modinfo returned nothing.")

27 else:

28 parser = ModinfoParser(stream)

29 version = parser.get_field("version")

30 if not version:

31 version = parser.get_field("vermagic").split()[0]

32

33 return version

34

35

36 def get_touch_attributes():

37 cmd = "udevadm info --export-db"

38 output, err = Popen(cmd, stdout=PIPE, shell=True).communicate()

39 if err:

40 print("Error running $s" % " ".join(cmd))

41 print(err)

42 return None

43

44 udev = UdevadmParser(StringIO(output.decode("unicode-escape")))

45 attributes = {}

46 for device in udev.run():

47 if getattr(device, "category") == "TOUCHPAD":

48 attributes["driver"] = getattr(device, "driver")

49 attributes["product"] = getattr(device, "product")

50 return attributes

51

52

53 def main():

54 attributes = get_touch_attributes()

55 if attributes:

56 modinfo = TouchpadDriver(attributes["driver"])

57 attributes["version"] = modinfo.driver_version

58 print(

59 "%s: %s\n%s: %s\n%s: %s\n"

60 % (

61 "Device",

62 attributes["product"],

63 "Driver",

64 attributes["driver"],

65 "Driver Version",

66 attributes["version"],

67 )

68 )

69 else:

70 print("No Touchpad Detected")

71 return 1

72

73 return 0

74

75

76 if __name__ == "__main__":

77 sys.exit(main())