From da10cd9aa89aa6d0470259db6826e49140976f85 Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sun, 21 May 2023 21:39:40 +0200 Subject: [PATCH 1/8] Increase max-module-lines to permit successful build Please refer to Issue 296 for more information. --- .pylintrc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.pylintrc b/.pylintrc index 0f254c75..ef843a9f 100644 --- a/.pylintrc +++ b/.pylintrc @@ -223,7 +223,9 @@ indent-string=' ' max-line-length=100 # Maximum number of lines in a module -max-module-lines=1000 +# +# XXX: Reduce this number once Issue 296 is resolved. +max-module-lines=1050 # List of optional constructs for which whitespace checking is disabled. `dict- # separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. From 822638059f11da72e84e5bc921e4e4898c61ee20 Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sun, 21 May 2023 21:44:40 +0200 Subject: [PATCH 2/8] Revert "Increase max-module-lines to permit successful build" This reverts commit da10cd9aa89aa6d0470259db6826e49140976f85. --- .pylintrc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.pylintrc b/.pylintrc index ef843a9f..0f254c75 100644 --- a/.pylintrc +++ b/.pylintrc @@ -223,9 +223,7 @@ indent-string=' ' max-line-length=100 # Maximum number of lines in a module -# -# XXX: Reduce this number once Issue 296 is resolved. -max-module-lines=1050 +max-module-lines=1000 # List of optional constructs for which whitespace checking is disabled. `dict- # separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. From 0e35bd663f02710383555a71cf55a0aa2c597fca Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 13:56:27 +0200 Subject: [PATCH 3/8] Extract Raspberry PI detection into own module The motivation for this change was to reduce the size of board.py. --- adafruit_platformdetect/board.py | 81 ++++--------------- .../boards/raspberrypi/__init__.py | 81 +++++++++++++++++++ 2 files changed, 95 insertions(+), 67 deletions(-) create mode 100644 adafruit_platformdetect/boards/raspberrypi/__init__.py diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index 83ef02bd..1efde88c 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -29,7 +29,7 @@ pass from adafruit_platformdetect.constants import boards, chips - +import adafruit_platformdetect.boards.raspberrypi as raspberrypi __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git" @@ -64,7 +64,9 @@ def id(self) -> Optional[str]: if chip_id == chips.H3: board_id = self._armbian_id() or self._allwinner_variants_id() elif chip_id == chips.BCM2XXX: - board_id = self._pi_id() + from adafruit_platformdetect.boards.raspberrypi import determine_board_id + + board_id = determine_board_id(self.detector) elif chip_id == chips.AM625X: board_id = self._beaglebone_id() elif chip_id == chips.AM33XX: @@ -198,67 +200,9 @@ def id(self) -> Optional[str]: # pylint: enable=invalid-name - def _pi_id(self) -> Optional[str]: - """Try to detect id of a Raspberry Pi.""" - # Check for Pi boards: - pi_rev_code = self._pi_rev_code() - if pi_rev_code: - from adafruit_platformdetect.revcodes import PiDecoder - - try: - decoder = PiDecoder(pi_rev_code) - model = boards._PI_MODELS[decoder.type_raw] - if isinstance(model, dict): - model = model[decoder.revision] - return model - except ValueError: - pass - # We may be on a non-Raspbian OS, so try to lazily determine - # the version based on `get_device_model` - else: - pi_model = self.detector.get_device_model() - if pi_model: - pi_model = pi_model.upper().replace(" ", "_") - if "PLUS" in pi_model: - re_model = re.search(r"(RASPBERRY_PI_\d).*([AB]_*)(PLUS)", pi_model) - elif "CM" in pi_model: # untested for Compute Module - re_model = re.search(r"(RASPBERRY_PI_CM)(\d)", pi_model) - else: # untested for non-plus models - re_model = re.search(r"(RASPBERRY_PI_\d).*([AB])", pi_model) - - if re_model: - pi_model = "".join(re_model.groups()) - available_models = boards._PI_REV_CODES.keys() - for model in available_models: - if model == pi_model: - return model - - return None - def _pi_rev_code(self) -> Optional[str]: """Attempt to find a Raspberry Pi revision code for this board.""" - # 2708 is Pi 1 - # 2709 is Pi 2 - # 2835 is Pi 3 (or greater) on 4.9.x kernel - # Anything else is not a Pi. - if self.detector.chip.id != chips.BCM2XXX: - # Something else, not a Pi. - return None - rev = self.detector.get_cpuinfo_field("Revision") - - if rev is not None: - return rev - - try: - with open("/proc/device-tree/system/linux,revision", "rb") as revision: - rev_bytes = revision.read() - - if rev_bytes[:1] == b"\x00": - rev_bytes = rev_bytes[1:] - - return rev_bytes.hex() - except FileNotFoundError: - return None + return raspberrypi.determine_rev_code(self.detector) # pylint: disable=no-self-use def _beaglebone_id(self) -> Optional[str]: @@ -588,12 +532,12 @@ def _udoo_id(self) -> Optional[str]: return None - def _intel_n_series_id(self) -> Optional[str]: - """Try to detect the id of an Intel N-Series board.""" - if self.detector.check_board_name_value() == "ODROID-H3": - return boards.ODROID_H3 + # def _intel_n_series_id(self) -> Optional[str]: + # """Try to detect the id of an Intel N-Series board.""" + # if self.detector.check_board_name_value() == "ODROID-H3": + # return boards.ODROID_H3 - return None + # return None def _j4105_id(self) -> Optional[str]: """Try to detect the id of J4105 board.""" @@ -716,7 +660,10 @@ def any_96boards(self) -> bool: @property def any_raspberry_pi(self) -> bool: """Check whether the current board is any Raspberry Pi.""" - return self._pi_rev_code() is not None + return ( + self.id in boards._RASPBERRY_PI_40_PIN_IDS + or self.id in boards._RASPBERRY_PI_CM_IDS + ) @property def any_raspberry_pi_40_pin(self) -> bool: diff --git a/adafruit_platformdetect/boards/raspberrypi/__init__.py b/adafruit_platformdetect/boards/raspberrypi/__init__.py new file mode 100644 index 00000000..7a8c540a --- /dev/null +++ b/adafruit_platformdetect/boards/raspberrypi/__init__.py @@ -0,0 +1,81 @@ +# SPDX-FileCopyrightText: 2021-2023 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +"""Identify specific models and revisions of Raspberry Pi boards.""" +import re + +import adafruit_platformdetect.constants.boards as board_ids +import adafruit_platformdetect.constants.chips as chips_ids + +try: + from typing import Optional, TYPE_CHECKING + + if TYPE_CHECKING: + from adafruit_platformdetect import Detector +except ImportError: + pass + + +def determine_board_id(detector: "Detector") -> Optional[str]: + """Try to detect id of a Raspberry Pi.""" + # Check for Pi boards: + pi_rev_code = determine_rev_code(detector) + if pi_rev_code: + from adafruit_platformdetect.revcodes import PiDecoder + + try: + decoder = PiDecoder(pi_rev_code) + model = board_ids._PI_MODELS[decoder.type_raw] + if isinstance(model, dict): + model = model[decoder.revision] + return model + except ValueError: + pass + # We may be on a non-Raspbian OS, so try to lazily determine + # the version based on `get_device_model` + else: + pi_model = detector.get_device_model() + if pi_model: + pi_model = pi_model.upper().replace(" ", "_") + if "PLUS" in pi_model: + re_model = re.search(r"(RASPBERRY_PI_\d).*([AB]_*)(PLUS)", pi_model) + elif "CM" in pi_model: # untested for Compute Module + re_model = re.search(r"(RASPBERRY_PI_CM)(\d)", pi_model) + else: # untested for non-plus models + re_model = re.search(r"(RASPBERRY_PI_\d).*([AB])", pi_model) + + if re_model: + pi_model = "".join(re_model.groups()) + available_models = board_ids._PI_REV_CODES.keys() + for model in available_models: + if model == pi_model: + return model + + return None + + +def determine_rev_code(detector: "Detector") -> Optional[str]: + """Attempt to find a Raspberry Pi revision code for this board.""" + # 2708 is Pi 1 + # 2709 is Pi 2 + # 2835 is Pi 3 (or greater) on 4.9.x kernel + # Anything else is not a Pi. + if detector.chip.id != chips_ids.BCM2XXX: + # Something else, not a Pi. + return None + rev = detector.get_cpuinfo_field("Revision") + + if rev is not None: + return rev + + try: + with open("/proc/device-tree/system/linux,revision", "rb") as revision: + rev_bytes = revision.read() + + if rev_bytes[:1] == b"\x00": + rev_bytes = rev_bytes[1:] + + return rev_bytes.hex() + except FileNotFoundError: + return None From a2aff462334e1c956ea730f5632a9a13e0751f4c Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 13:57:55 +0200 Subject: [PATCH 4/8] Reduce max-module-lines used by Pylint --- .pylintrc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.pylintrc b/.pylintrc index ef843a9f..0f254c75 100644 --- a/.pylintrc +++ b/.pylintrc @@ -223,9 +223,7 @@ indent-string=' ' max-line-length=100 # Maximum number of lines in a module -# -# XXX: Reduce this number once Issue 296 is resolved. -max-module-lines=1050 +max-module-lines=1000 # List of optional constructs for which whitespace checking is disabled. `dict- # separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. From 10459a4ddf4670ac5c50e750c9beb66cf7a85e3d Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 14:00:56 +0200 Subject: [PATCH 5/8] Revert unintentional change --- adafruit_platformdetect/board.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index 1efde88c..f5e4ebc2 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -532,12 +532,12 @@ def _udoo_id(self) -> Optional[str]: return None - # def _intel_n_series_id(self) -> Optional[str]: - # """Try to detect the id of an Intel N-Series board.""" - # if self.detector.check_board_name_value() == "ODROID-H3": - # return boards.ODROID_H3 + def _intel_n_series_id(self) -> Optional[str]: + """Try to detect the id of an Intel N-Series board.""" + if self.detector.check_board_name_value() == "ODROID-H3": + return boards.ODROID_H3 - # return None + return None def _j4105_id(self) -> Optional[str]: """Try to detect the id of J4105 board.""" From 9719b47179e3cff509408e7279f8364b03ead96d Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 14:03:53 +0200 Subject: [PATCH 6/8] Revert change to any_raspberry_pi --- adafruit_platformdetect/board.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index f5e4ebc2..10bd9519 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -660,10 +660,7 @@ def any_96boards(self) -> bool: @property def any_raspberry_pi(self) -> bool: """Check whether the current board is any Raspberry Pi.""" - return ( - self.id in boards._RASPBERRY_PI_40_PIN_IDS - or self.id in boards._RASPBERRY_PI_CM_IDS - ) + return self._pi_rev_code() is not None @property def any_raspberry_pi_40_pin(self) -> bool: From 3c0e2e9160ce0200bed926957058f39464339970 Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 14:09:36 +0200 Subject: [PATCH 7/8] Fix import statement --- adafruit_platformdetect/board.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_platformdetect/board.py b/adafruit_platformdetect/board.py index 10bd9519..d65b9d92 100644 --- a/adafruit_platformdetect/board.py +++ b/adafruit_platformdetect/board.py @@ -21,7 +21,6 @@ """ import os -import re try: from typing import Optional @@ -29,7 +28,7 @@ pass from adafruit_platformdetect.constants import boards, chips -import adafruit_platformdetect.boards.raspberrypi as raspberrypi +from adafruit_platformdetect.boards import raspberrypi __version__ = "0.0.0+auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PlatformDetect.git" From d2bf663e86637063ef509b203524f1cef3c8021b Mon Sep 17 00:00:00 2001 From: Nathan Young <77929198+NathanY3G@users.noreply.github.com> Date: Sat, 27 May 2023 14:14:20 +0200 Subject: [PATCH 8/8] Disable an Pylint observation for now More extensive refactoring is required to resolve this. --- adafruit_platformdetect/boards/raspberrypi/__init__.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/adafruit_platformdetect/boards/raspberrypi/__init__.py b/adafruit_platformdetect/boards/raspberrypi/__init__.py index 7a8c540a..e6510dab 100644 --- a/adafruit_platformdetect/boards/raspberrypi/__init__.py +++ b/adafruit_platformdetect/boards/raspberrypi/__init__.py @@ -17,6 +17,7 @@ pass +# pylint: disable=protected-access def determine_board_id(detector: "Detector") -> Optional[str]: """Try to detect id of a Raspberry Pi.""" # Check for Pi boards: @@ -55,6 +56,9 @@ def determine_board_id(detector: "Detector") -> Optional[str]: return None +# pylint: enable=protected-access + + def determine_rev_code(detector: "Detector") -> Optional[str]: """Attempt to find a Raspberry Pi revision code for this board.""" # 2708 is Pi 1