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.

109 lines
3.8 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. if value in open('/proc/device-tree/compatible').read():
  55. return True
  56. return False
  57. def get_armbian_release_field(self, field):
  58. """
  59. Search /etc/armbian-release, if it exists, for a field and return its
  60. value, if found, otherwise None.
  61. """
  62. field_value = None
  63. pattern = r'^' + field + r'=(.*)'
  64. try:
  65. with open("/etc/armbian-release", 'r') as release_file:
  66. armbian = release_file.read().split('\n')
  67. for line in armbian:
  68. match = re.search(pattern, line)
  69. if match:
  70. field_value = match.group(1)
  71. except FileNotFoundError:
  72. pass
  73. return field_value
  74. def get_device_model(self):
  75. """
  76. Search /proc/device-tree/model for the device model and return its value, if found,
  77. otherwise None.
  78. """
  79. try:
  80. with open('/proc/device-tree/model', 'r') as model_file:
  81. model = model_file.read()
  82. return model
  83. except FileNotFoundError:
  84. pass
  85. def get_device_compatible(self):
  86. """
  87. Search /proc/device-tree/compatible for the compatible chip name.
  88. """
  89. try:
  90. with open('/proc/device-tree/compatible', 'r') as model_file:
  91. model = model_file.read()
  92. return model
  93. except FileNotFoundError:
  94. pass