Match serial numbers in python
Replace shell commands that match serial numbers in output of `adb
devices` with python regular expressions.
Change-Id: Ie47e572f1a88602102cf79a9902eb77a3e16d79c
diff --git a/deploy.py b/deploy.py
index f08920c..559fe8c 100755
--- a/deploy.py
+++ b/deploy.py
@@ -3,6 +3,7 @@
import os
import time
from uiautomator import Device
+import re
import sys
import subprocess
import pathlib
@@ -28,6 +29,8 @@
'security': 'WPA/WPA2 PSK', 'password': 'fdev@adm'},
}
+ADB_DEVICES_PATTERN = re.compile(r'^([a-z0-9-]+)\s+device$', flags=re.M)
+
class HostCommandError(BaseException):
"""An error happened while issuing a command on the host."""
@@ -100,6 +103,16 @@
return ret
+def list_devices():
+ """List serial numbers of devices attached to adb.
+
+ Raises:
+ DeviceCommandError: If the underlying adb command failed.
+ """
+ process = adb('devices')
+ return ADB_DEVICES_PATTERN.findall(process.stdout)
+
+
def aapt(*args, raise_on_error = True):
"""Run an AAPT command.
@@ -403,20 +416,7 @@
if len(sys.argv) > 1:
serials.append(sys.argv[1])
else:
- # List devices attached to adb
- ret = subprocess.run(
- "adb devices | grep -E '^[a-z0-9-]+\s+device$' | awk '{{print $1}}'",
- shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- universal_newlines=True)
-
- if 0 < ret.returncode:
- # TODO handle the error
- raise Exception('Failed to list adb devices')
- elif ret.stdout:
- for line in ret.stdout.splitlines():
- serial = line.strip()
- if serial:
- serials.append(serial)
+ serials = list_devices()
for serial in serials:
print('Configuring device {}…'.format(serial))