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.

133 lines
4.2 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 os
  8. import re
  9. import sys
  10. try:
  11. from typing import Optional
  12. except ImportError:
  13. pass
  14. from adafruit_platformdetect.board import Board
  15. from adafruit_platformdetect.chip import Chip
  16. # Needed to find libs (like libusb) installed by homebrew on Apple Silicon
  17. if sys.platform == "darwin":
  18. os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = "/opt/homebrew/lib/"
  19. # Various methods here may retain state in future, so tell pylint not to worry
  20. # that they don't use self right now:
  21. # pylint: disable=no-self-use
  22. class Detector:
  23. """Wrap various platform detection functions."""
  24. def __init__(self) -> None:
  25. self.board = Board(self)
  26. self.chip = Chip(self)
  27. def get_cpuinfo_field(self, field: str) -> Optional[str]:
  28. """
  29. Search /proc/cpuinfo for a field and return its value, if found,
  30. otherwise None.
  31. """
  32. # Match a line like 'Hardware : BCM2709':
  33. pattern = r"^" + field + r"\s+:\s+(.*)$"
  34. with open("/proc/cpuinfo", "r", encoding="utf-8") as infile:
  35. cpuinfo = infile.read().split("\n")
  36. for line in cpuinfo:
  37. match = re.search(pattern, line, flags=re.IGNORECASE)
  38. if match:
  39. return match.group(1)
  40. return None
  41. def check_dt_compatible_value(self, value: str) -> bool:
  42. """
  43. Search /proc/device-tree/compatible for a value and return True, if found,
  44. otherwise False.
  45. """
  46. # Match a value like 'qcom,apq8016-sbc':
  47. dt_compatible = self.get_device_compatible()
  48. if dt_compatible and value in dt_compatible:
  49. return True
  50. return False
  51. def get_armbian_release_field(self, field: str) -> Optional[str]:
  52. """
  53. Search /etc/armbian-release, if it exists, for a field and return its
  54. value, if found, otherwise None.
  55. """
  56. field_value = None
  57. pattern = r"^" + field + r"=(.*)"
  58. try:
  59. with open("/etc/armbian-release", "r", encoding="utf-8") as release_file:
  60. armbian = release_file.read().split("\n")
  61. for line in armbian:
  62. match = re.search(pattern, line)
  63. if match:
  64. field_value = match.group(1)
  65. except FileNotFoundError:
  66. pass
  67. return field_value
  68. def get_device_model(self) -> Optional[str]:
  69. """
  70. Search /proc/device-tree/model for the device model and return its value, if found,
  71. otherwise None.
  72. """
  73. try:
  74. with open("/proc/device-tree/model", "r", encoding="utf-8") as model_file:
  75. return model_file.read()
  76. except FileNotFoundError:
  77. pass
  78. return None
  79. def get_device_compatible(self) -> Optional[str]:
  80. """
  81. Search /proc/device-tree/compatible for the compatible chip name.
  82. """
  83. try:
  84. with open(
  85. "/proc/device-tree/compatible", "r", encoding="utf-8"
  86. ) as model_file:
  87. return model_file.read()
  88. except FileNotFoundError:
  89. pass
  90. return None
  91. def check_board_asset_tag_value(self) -> Optional[str]:
  92. """
  93. Search /sys/devices/virtual/dmi/id for the device model and return its value, if found,
  94. otherwise None.
  95. """
  96. try:
  97. with open(
  98. "/sys/devices/virtual/dmi/id/board_asset_tag", "r", encoding="utf-8"
  99. ) as tag_file:
  100. return tag_file.read().strip()
  101. except FileNotFoundError:
  102. pass
  103. return None
  104. def check_board_name_value(self) -> Optional[str]:
  105. """
  106. Search /sys/devices/virtual/dmi/id for the board name and return its value, if found,
  107. otherwise None. Debian/ubuntu based
  108. """
  109. try:
  110. with open(
  111. "/sys/devices/virtual/dmi/id/board_name", "r", encoding="utf-8"
  112. ) as board_name_file:
  113. return board_name_file.read().strip()
  114. except FileNotFoundError:
  115. pass
  116. return None