Permalink
Browse files

reset.py: Not every Unix is Linux.

Move the sysfs tests for Maple out of unix_get_maple_path() and into a
new linux_get_maple_path(). This prevents unnecessary probing for a
nonexistent /sys on e.g. OS X.

Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
  • Loading branch information...
1 parent 74266c8 commit c98111eb25eabceb11368ae30c28b081fe1daa24 Marti Bolivar committed Aug 22, 2012
Showing with 20 additions and 8 deletions.
  1. +20 −8 support/scripts/reset.py
View
@@ -9,16 +9,28 @@
import time
from struct import pack
-def unix_get_maple_path(file_prefix):
- """Try to find the device file for the Maple on *nix; assuming
- that it looks like /dev/<file_prefix>* and has the correct ID in
- the /sys tree. If there are multiple possibilities, ask the user
- what to do. If the user chooses not to say, returns None."""
+def unix_get_maple_path(file_prefix, dev_is_maple=lambda dev: True):
+ """Try to find the device file for the Maple on *nix.
+
+ This function works assuming that the device file globs like
+ '/dev/<file_prefix>*'. The caller may pass an additional
+ dev_is_maple predicate if the platform supports additional tests
+ to determine if a device is a Maple.
+
+ If there are multiple possibilities, ask the user what to do. If
+ the user chooses not to say, returns None."""
possible_paths = [os.path.join('/dev', x) for x in os.listdir('/dev') \
- if (x.startswith(file_prefix) and tty_is_maple(x))]
+ if x.startswith(file_prefix) and dev_is_maple(x)]
return choose_path(possible_paths)
-def tty_is_maple(device):
+def linux_get_maple_path(file_prefix='ttyACM'):
+ """Specialized unix_get_maple_path() for Linux.
+
+ Attempts to check that a candidate device has the correct ID in
+ the /sys tree when deciding if it's a Maple."""
+ return unix_get_maple_path(file_prefix, linux_tty_is_maple)
+
+def linux_tty_is_maple(device):
try:
sysfile = open("/sys/class/tty/%s/device/uevent" % device, "r")
text = "".join(sysfile.readlines())
@@ -72,7 +84,7 @@ def choose_among_options(options):
plat_sys = platform.system()
plat_bits = platform.architecture()[0]
if plat_sys == 'Linux':
- maple_path = unix_get_maple_path('ttyACM')
+ maple_path = linux_get_maple_path()
# fall back on /dev/maple if that doesn't work
if maple_path is None:
maple_path = '/dev/maple'

0 comments on commit c98111e

Please sign in to comment.