From e9d203ae7b06f02b475faa9779a346f4f4225238 Mon Sep 17 00:00:00 2001 From: jtc42 Date: Wed, 27 Nov 2019 19:28:55 +0000 Subject: [PATCH] Better is_raspberrypi --- .../api/v2/blueprints/actions/system.py | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/openflexure_microscope/api/v2/blueprints/actions/system.py b/openflexure_microscope/api/v2/blueprints/actions/system.py index f3c9de79..3b89e8d9 100644 --- a/openflexure_microscope/api/v2/blueprints/actions/system.py +++ b/openflexure_microscope/api/v2/blueprints/actions/system.py @@ -1,15 +1,49 @@ from openflexure_microscope.api.views import MicroscopeView import subprocess -import os +import io from sys import platform -def is_raspberrypi(): - if (platform != "linux"): - return False - else: - return (os.uname()[1] == 'raspberrypi') +def is_raspberrypi(raise_on_errors=False): + """ + Checks if Raspberry Pi. + """ + try: + with io.open('/proc/cpuinfo', 'r') as cpuinfo: + found = False + for line in cpuinfo: + if line.startswith('Hardware'): + found = True + label, value = line.strip().split(':', 1) + value = value.strip() + if value not in ( + 'BCM2708', + 'BCM2709', + 'BCM2835', + 'BCM2836' + ): + if raise_on_errors: + raise ValueError( + 'This system does not appear to be a ' + 'Raspberry Pi.' + ) + else: + return False + if not found: + if raise_on_errors: + raise ValueError( + 'Unable to determine if this system is a Raspberry Pi.' + ) + else: + return False + except IOError: + if raise_on_errors: + raise ValueError('Unable to open `/proc/cpuinfo`.') + else: + return False + + return True class ShutdownAPI(MicroscopeView): """