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.

75 lines
2.8 KiB

  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 get_armbian_release_field(self, field):
  49. """
  50. Search /etc/armbian-release, if it exists, for a field and return its
  51. value, if found, otherwise None.
  52. """
  53. field_value = None
  54. pattern = r'^' + field + r'=(.*)'
  55. try:
  56. with open("/etc/armbian-release", 'r') as release_file:
  57. armbian = release_file.read().split('\n')
  58. for line in armbian:
  59. match = re.search(pattern, line)
  60. if match:
  61. field_value = match.group(1)
  62. except FileNotFoundError:
  63. pass
  64. return field_value