summaryrefslogtreecommitdiffstats
path: root/python-packages
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2016-02-24 12:28:45 -0800
committerDan Albert <danalbert@google.com>2016-02-24 12:28:45 -0800
commit9bee8c209c9bddda3e47f9ec0b8a31c34c4b5c9d (patch)
treead7dfec9edf59f57c3295503bd1b90b774120591 /python-packages
parentaaf5455b167d681bdf930bfd79e587a3b8068969 (diff)
downloadandroid_development-9bee8c209c9bddda3e47f9ec0b8a31c34c4b5c9d.tar.gz
android_development-9bee8c209c9bddda3e47f9ec0b8a31c34c4b5c9d.tar.bz2
android_development-9bee8c209c9bddda3e47f9ec0b8a31c34c4b5c9d.zip
Replace splitlines() with something more tolerant.
Old devices suck. Old devices on Windows seem to suck harder. Change-Id: I88df25d2c480439859c913e9d8f8fb7ef04b7d5b
Diffstat (limited to 'python-packages')
-rw-r--r--python-packages/adb/device.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/python-packages/adb/device.py b/python-packages/adb/device.py
index a10a85cdf..359230a7a 100644
--- a/python-packages/adb/device.py
+++ b/python-packages/adb/device.py
@@ -51,7 +51,7 @@ def get_devices(adb_path='adb'):
with open(os.devnull, 'wb') as devnull:
subprocess.check_call([adb_path, 'start-server'], stdout=devnull,
stderr=devnull)
- out = subprocess.check_output([adb_path, 'devices']).splitlines()
+ out = _split_lines(subprocess.check_output([adb_path, 'devices']))
# The first line of `adb devices` just says "List of attached devices", so
# skip that.
@@ -213,6 +213,21 @@ def _subprocess_Popen(*args, **kwargs):
return subprocess.Popen(*_get_subprocess_args(args), **kwargs)
+def _split_lines(s):
+ """Splits lines in a way that works even on Windows and old devices.
+
+ Windows will see \r\n instead of \n, old devices do the same, old devices
+ on Windows will see \r\r\n.
+ """
+ # rstrip is used here to workaround a difference between splineslines and
+ # re.split:
+ # >>> 'foo\n'.splitlines()
+ # ['foo']
+ # >>> re.split(r'\n', 'foo\n')
+ # ['foo', '']
+ return re.split(r'[\r\n]+', s.rstrip())
+
+
class AndroidDevice(object):
# Delimiter string to indicate the start of the exit code.
_RETURN_CODE_DELIMITER = 'x'
@@ -256,7 +271,7 @@ class AndroidDevice(object):
def features(self):
if self._features is None:
try:
- self._features = self._simple_call(['features']).splitlines()
+ self._features = _split_lines(self._simple_call(['features']))
except subprocess.CalledProcessError:
self._features = []
return self._features
@@ -460,7 +475,7 @@ class AndroidDevice(object):
def get_props(self):
result = {}
output, _ = self.shell(['getprop'])
- output = output.splitlines()
+ output = _split_lines(output)
pattern = re.compile(r'^\[([^]]+)\]: \[(.*)\]')
for line in output:
match = pattern.match(line)
@@ -474,7 +489,7 @@ class AndroidDevice(object):
return result
def get_prop(self, prop_name):
- output = self.shell(['getprop', prop_name])[0].splitlines()
+ output = _split_lines(self.shell(['getprop', prop_name])[0])
if len(output) != 1:
raise RuntimeError('Too many lines in getprop output:\n' +
'\n'.join(output))