Bug 1440714 - Add ADBDevice.get_file() to pull and read a remote file; r=bc
authorGeoff Brown <gbrown@mozilla.com>
Fri, 09 Mar 2018 09:27:08 -0700
changeset 462387 bfe063307abd3fe4444b701af6c2e26b4e7ed02d
parent 462386 37bb096eb861b1b68220c3847102fc153d6675f1
child 462388 0441c697499ad35b67384ae28971cd4b26dff12a
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbc
bugs1440714
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1440714 - Add ADBDevice.get_file() to pull and read a remote file; r=bc
testing/mozbase/mozdevice/mozdevice/adb.py
--- a/testing/mozbase/mozdevice/mozdevice/adb.py
+++ b/testing/mozbase/mozdevice/mozdevice/adb.py
@@ -1803,16 +1803,46 @@ class ADBDevice(ADBCommand):
             self.command_output(["pull", remote, local], timeout=timeout)
         except BaseException:
             raise
         finally:
             if copy_required:
                 dir_util.copy_tree(local, original_local)
                 shutil.rmtree(temp_parent)
 
+    def get_file(self, remote, offset=None, length=None, timeout=None):
+        """Pull file from device and return the file's content
+
+        :param str remote: The path of the remote file.
+        :param offset: If specified, return only content beyond this offset.
+        :param length: If specified, limit content length accordingly.
+        :param timeout: The maximum time in
+            seconds for any spawned adb process to complete before
+            throwing an ADBTimeoutError.
+            This timeout is per adb call. The total time spent
+            may exceed this value. If it is not specified, the value
+            set in the ADBDevice constructor is used.
+        :type timeout: integer or None
+        :raises: * ADBTimeoutError
+                 * ADBError
+        """
+        with tempfile.NamedTemporaryFile() as tf:
+            self.pull(remote, tf.name, timeout=timeout)
+            with 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:
+                    tf2.seek(offset)
+                    return tf2.read(length)
+                elif offset is not None:
+                    tf2.seek(offset)
+                    return tf2.read()
+                else:
+                    return tf2.read()
+
     def rm(self, path, recursive=False, force=False, timeout=None, root=False):
         """Delete files or directories on the device.
 
         :param str path: The path of the remote file or directory.
         :param bool recursive: Flag specifying if the command is
             to be applied recursively to the target. Default is False.
         :param bool force: Flag which if True will not raise an
             error when attempting to delete a non-existent file. Default