From cf9936f338dc5b90f671cce4f1dba87aaef43bc5 Mon Sep 17 00:00:00 2001 From: William Lachance Date: Mon, 23 Feb 2015 10:56:50 -0500 Subject: [PATCH] Bug 1135255 - Fix mozdevice tempfile handling on Windows. r=gbrown --- .../mozdevice/mozdevice/devicemanagerADB.py | 45 +++++++------------ 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/testing/mozbase/mozdevice/mozdevice/devicemanagerADB.py b/testing/mozbase/mozdevice/mozdevice/devicemanagerADB.py index 611baaba11e1..5858abe33935 100644 --- a/testing/mozbase/mozdevice/mozdevice/devicemanagerADB.py +++ b/testing/mozbase/mozdevice/mozdevice/devicemanagerADB.py @@ -408,25 +408,20 @@ class DeviceManagerADB(DeviceManager): def pullFile(self, remoteFile, offset=None, length=None): # TODO: add debug flags and allow for printing stdout - localFile = tempfile.mkstemp()[1] - self._runPull(remoteFile, localFile) - - f = open(localFile, 'r') - - # 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: - f.seek(offset) - ret = f.read(length) - elif offset is not None: - f.seek(offset) - ret = f.read() - else: - ret = f.read() - - f.close() - mozfile.remove(localFile) - return ret + with mozfile.NamedTemporaryFile() as tf: + self._runPull(remoteFile, tf.name) + # we need to reopen the file to get the written contents + 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 getFile(self, remoteFile, localFile): self._runPull(remoteFile, localFile) @@ -445,16 +440,10 @@ class DeviceManagerADB(DeviceManager): """ Return the md5 sum of a file on the device """ - localFile = tempfile.mkstemp()[1] - localFile = self._runPull(remoteFile, localFile) + with tempfile.NamedTemporaryFile() as f: + self._runPull(remoteFile, f.name) - if localFile is None: - return None - - md5 = self._getLocalHash(localFile) - mozfile.remove(localFile) - - return md5 + return self._getLocalHash(f.name) def _setupDeviceRoot(self, deviceRoot): # user-specified device root, create it and return it