You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

5 years ago
  1. # SPDX-FileCopyrightText: 2014-2018 Tony DiCola, Limor Fried, Brennen Bearnes
  2. #
  3. # SPDX-License-Identifier: MIT
  4. """
  5. Attempt to detect the current platform.
  6. """
  7. import re
  8. from adafruit_platformdetect.board import Board
  9. from adafruit_platformdetect.chip import Chip
  10. # Various methods here may retain state in future, so tell pylint not to worry
  11. # that they don't use self right now:
  12. # pylint: disable=no-self-use
  13. class Detector:
  14. """Wrap various platform detection functions."""
  15. def __init__(self):
  16. self.board = Board(self)
  17. self.chip = Chip(self)
  18. def get_cpuinfo_field(self, field):
  19. """
  20. Search /proc/cpuinfo for a field and return its value, if found,
  21. otherwise None.
  22. """
  23. # Match a line like 'Hardware : BCM2709':
  24. pattern = r"^" + field + r"\s+:\s+(.*)$"
  25. with open("/proc/cpuinfo", "r", encoding="utf-8") as infile:
  26. cpuinfo = infile.read().split("\n")
  27. for line in cpuinfo:
  28. match = re.search(pattern, line, flags=re.IGNORECASE)
  29. if match:
  30. return match.group(1)
  31. return None
  32. def check_dt_compatible_value(self, value):
  33. """
  34. Search /proc/device-tree/compatible for a value and return True, if found,
  35. otherwise False.
  36. """
  37. # Match a value like 'qcom,apq8016-sbc':
  38. dt_compatible = self.get_device_compatible()
  39. if dt_compatible and value in dt_compatible:
  40. return True
  41. return False
  42. def get_armbian_release_field(self, field):
  43. """
  44. Search /etc/armbian-release, if it exists, for a field and return its
  45. value, if found, otherwise None.
  46. """
  47. field_value = None
  48. pattern = r"^" + field + r"=(.*)"
  49. try:
  50. with open("/etc/armbian-release", "r", encoding="utf-8") as release_file:
  51. armbian = release_file.read().split("\n")
  52. for line in armbian:
  53. match = re.search(pattern, line)
  54. if match:
  55. field_value = match.group(1)
  56. except FileNotFoundError:
  57. pass
  58. return field_value
  59. def get_device_model(self):
  60. """
  61. Search /proc/device-tree/model for the device model and return its value, if found,
  62. otherwise None.
  63. """
  64. try:
  65. with open("/proc/device-tree/model", "r", encoding="utf-8") as model_file:
  66. return model_file.read()
  67. except FileNotFoundError:
  68. pass
  69. return None
  70. def get_device_compatible(self):
  71. """
  72. Search /proc/device-tree/compatible for the compatible chip name.
  73. """
  74. try:
  75. with open(
  76. "/proc/device-tree/compatible", "r", encoding="utf-8"
  77. ) as model_file:
  78. return model_file.read()
  79. except FileNotFoundError:
  80. pass
  81. return None
  82. def check_board_asset_tag_value(self):
  83. """
  84. Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
  85. otherwise None.
  86. """
  87. try:
  88. with open(
  89. "/sys/devices/virtual/dmi/id/board_asset_tag", "r", encoding="utf-8"
  90. ) as tag_file:
  91. return tag_file.read().strip()
  92. except FileNotFoundError:
  93. pass
  94. return None
  95. def check_board_name_value(self):
  96. """
  97. Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
  98. otherwise None. Debian/ubuntu based
  99. """
  100. try:
  101. with open(
  102. "/sys/devices/virtual/dmi/id/board_name", "r", encoding="utf-8"
  103. ) as board_name_file:
  104. return board_name_file.read().strip()
  105. except FileNotFoundError:
  106. pass
  107. return None