Created
October 31, 2017 14:53
-
-
Save barseghyanartur/94dbda2ad6f8937d6c307811ad51469a to your computer and use it in GitHub Desktop.
Detect if running on Raspberry PI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
def is_raspberry_pi(raise_on_errors=False): | |
"""Checks if Raspberry PI. | |
:return: | |
""" | |
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 | |
IS_RASPBERRY_PI = is_raspberry_pi() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment