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.

112 lines
3.9 KiB

5 years ago
  1. # Copyright (c) 2014-2018 Adafruit Industries
  2. # Author: Tony DiCola, Limor Fried, Brennen Bearnes
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in all
  10. # copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. """
  19. Attempt to detect the current platform.
  20. """
  21. import sys
  22. import platform
  23. import re
  24. from adafruit_platformdetect.board import Board
  25. from adafruit_platformdetect.chip import Chip
  26. # Various methods here may retain state in future, so tell pylint not to worry
  27. # that they don't use self right now:
  28. # pylint: disable=no-self-use
  29. class Detector:
  30. """Wrap various platform detection functions."""
  31. def __init__(self):
  32. self.board = Board(self)
  33. self.chip = Chip(self)
  34. def get_cpuinfo_field(self, field):
  35. """
  36. Search /proc/cpuinfo for a field and return its value, if found,
  37. otherwise None.
  38. """
  39. # Match a line like 'Hardware : BCM2709':
  40. pattern = r'^' + field + r'\s+:\s+(.*)$'
  41. with open('/proc/cpuinfo', 'r') as infile:
  42. cpuinfo = infile.read().split('\n')
  43. for line in cpuinfo:
  44. match = re.search(pattern, line, flags=re.IGNORECASE)
  45. if match:
  46. return match.group(1)
  47. return None
  48. def check_dt_compatible_value(self, value):
  49. """
  50. Search /proc/device-tree/compatible for a value and return True, if found,
  51. otherwise False.
  52. """
  53. # Match a value like 'qcom,apq8016-sbc':
  54. try:
  55. if value in open('/proc/device-tree/compatible').read():
  56. return True
  57. except FileNotFoundError:
  58. pass
  59. return False
  60. def get_armbian_release_field(self, field):
  61. """
  62. Search /etc/armbian-release, if it exists, for a field and return its
  63. value, if found, otherwise None.
  64. """
  65. field_value = None
  66. pattern = r'^' + field + r'=(.*)'
  67. try:
  68. with open("/etc/armbian-release", 'r') as release_file:
  69. armbian = release_file.read().split('\n')
  70. for line in armbian:
  71. match = re.search(pattern, line)
  72. if match:
  73. field_value = match.group(1)
  74. except FileNotFoundError:
  75. pass
  76. return field_value
  77. def get_device_model(self):
  78. """
  79. Search /proc/device-tree/model for the device model and return its value, if found,
  80. otherwise None.
  81. """
  82. try:
  83. with open('/proc/device-tree/model', 'r') as model_file:
  84. model = model_file.read()
  85. return model
  86. except FileNotFoundError:
  87. pass
  88. def get_device_compatible(self):
  89. """
  90. Search /proc/device-tree/compatible for the compatible chip name.
  91. """
  92. try:
  93. with open('/proc/device-tree/compatible', 'r') as model_file:
  94. model = model_file.read()
  95. return model
  96. except FileNotFoundError:
  97. pass