From bf8987e94707caa1263545d58f148cf8fd0fe5f4 Mon Sep 17 00:00:00 2001 From: Dave Hunt Date: Fri, 22 Feb 2019 12:33:14 +0000 Subject: [PATCH] Bug 1526752 - [mozdevice] Run modernize over adb.py; r=ahal Improve support for Python 3 by running modernize over adb.py and a few other related changes. Differential Revision: https://phabricator.services.mozilla.com/D19306 --HG-- extra : moz-landing-system : lando --- testing/mozbase/mozdevice/mozdevice/adb.py | 36 ++++++++++++---------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/testing/mozbase/mozdevice/mozdevice/adb.py b/testing/mozbase/mozdevice/mozdevice/adb.py index 8f3929cab47e..ac8b412663f7 100644 --- a/testing/mozbase/mozdevice/mozdevice/adb.py +++ b/testing/mozbase/mozdevice/mozdevice/adb.py @@ -4,6 +4,7 @@ from __future__ import absolute_import, print_function +import io import os import posixpath import re @@ -15,6 +16,9 @@ import time import traceback from distutils import dir_util +import six +from six.moves import range + from . import version_codes @@ -189,7 +193,7 @@ class ADBCommand(object): stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() re_version = re.compile(r'Android Debug Bridge version (.*)') - self._adb_version = re_version.match(output[0]).group(1) + self._adb_version = re_version.match(output[0].decode('utf-8')).group(1) except Exception as exc: raise ADBError('%s: %s is not executable.' % (exc, adb)) @@ -318,9 +322,9 @@ class ADBCommand(object): adb_process.exitcode, output)) - return output + return output.decode('utf-8') finally: - if adb_process and isinstance(adb_process.stdout_file, file): + if adb_process and isinstance(adb_process.stdout_file, io.IOBase): adb_process.stdout_file.close() @@ -787,7 +791,7 @@ class ADBDevice(ADBCommand): re_device_serial_tcpip.match(serial) is not None or \ ":" not in serial - if isinstance(device, (str, unicode)): + if isinstance(device, six.string_types): # Treat this as a device serial if not is_valid_serial(device): raise ValueError("Device serials containing ':' characters are " @@ -881,7 +885,7 @@ class ADBDevice(ADBCommand): offset = 1 while length - offset >= 0: file_obj.seek(-offset, os.SEEK_END) - char = file_obj.read(1) + char = file_obj.read(1).decode('utf-8') if not char: break if char != '\r' and char != '\n': @@ -1337,8 +1341,8 @@ class ADBDevice(ADBCommand): if cwd: cmd = "cd %s && %s" % (cwd, cmd) if env: - envstr = '&& '.join(map(lambda x: 'export %s=%s' % - (x[0], x[1]), env.iteritems())) + envstr = '&& '.join(['export %s=%s' % + (x[0], x[1]) for x in env.items()]) cmd = envstr + "&& " + cmd cmd += "; echo adb_returncode=$?" @@ -1362,7 +1366,7 @@ class ADBDevice(ADBCommand): time.sleep(self._polling_interval) exitcode = adb_process.proc.poll() else: - stdout2 = open(adb_process.stdout_file.name, 'rb') + stdout2 = io.open(adb_process.stdout_file.name, 'rb') while ((time.time() - start_time) <= float(timeout)) and exitcode is None: try: line = _timed_read_line(stdout2) @@ -1474,9 +1478,9 @@ class ADBDevice(ADBCommand): adb_process.exitcode, output)) - return output + return output.decode('utf-8') finally: - if adb_process and isinstance(adb_process.stdout_file, file): + if adb_process and isinstance(adb_process.stdout_file, io.IOBase): adb_process.stdout_file.close() # Informational methods @@ -2074,7 +2078,7 @@ class ADBDevice(ADBCommand): else: entry = line entries[entry] = 1 - entry_list = entries.keys() + entry_list = list(entries.keys()) entry_list.sort() return entry_list @@ -2247,7 +2251,7 @@ class ADBDevice(ADBCommand): """ with tempfile.NamedTemporaryFile() as tf: self.pull(remote, tf.name, timeout=timeout) - with open(tf.name) as tf2: + with io.open(tf.name) as tf2: # ADB pull does not support offset and length, but we can # instead read only the requested portion of the local file if offset is not None and length is not None: @@ -2481,7 +2485,7 @@ class ADBDevice(ADBCommand): :raises: * ADBTimeoutError * ADBError """ - if not isinstance(process_name, basestring): + if not isinstance(process_name, six.string_types): raise ADBError("Process name %s is not a string" % process_name) # Filter out extra spaces. @@ -3009,7 +3013,7 @@ class ADBDevice(ADBCommand): # against bool prior to testing it against int in order to # prevent falsely identifying a bool value as an int. if extras: - for (key, val) in extras.iteritems(): + for (key, val) in extras.items(): if isinstance(val, bool): extra_type_param = "--ez" elif isinstance(val, int): @@ -3060,7 +3064,7 @@ class ADBDevice(ADBCommand): if moz_env: # moz_env is expected to be a dictionary of environment variables: # Fennec itself will set them when launched - for (env_count, (env_key, env_val)) in enumerate(moz_env.iteritems()): + for (env_count, (env_key, env_val)) in moz_env.items(): extras["env" + str(env_count)] = env_key + "=" + env_val # Additional command line arguments that fennec will read and use (e.g. @@ -3110,7 +3114,7 @@ class ADBDevice(ADBCommand): if moz_env: # moz_env is expected to be a dictionary of environment variables: # geckoview_example itself will set them when launched - for (env_count, (env_key, env_val)) in enumerate(moz_env.iteritems()): + for (env_count, (env_key, env_val)) in enumerate(moz_env.items()): extras["env" + str(env_count)] = env_key + "=" + env_val # Additional command line arguments that the app will read and use (e.g.