| Index: devil/devil/android/device_temp_file.py
|
| diff --git a/devil/devil/android/device_temp_file.py b/devil/devil/android/device_temp_file.py
|
| index 4d0c7adb5ca0b2c8ec902293525e3979a22f991a..df148f0111dea2d26466ba99d8c90cf19aa5457d 100644
|
| --- a/devil/devil/android/device_temp_file.py
|
| +++ b/devil/devil/android/device_temp_file.py
|
| @@ -14,7 +14,16 @@ from devil.android import device_errors
|
| from devil.utils import cmd_helper
|
|
|
|
|
| +def _GenerateName(prefix, suffix, dir):
|
| + random_hex = hex(random.randint(0, 2 ** 52))[2:]
|
| + return posixpath.join(dir, '%s-%s%s' % (prefix, random_hex, suffix))
|
| +
|
| +
|
| class DeviceTempFile(object):
|
| + """A named temporary file on a device.
|
| +
|
| + Behaves like tempfile.NamedTemporaryFile.
|
| + """
|
|
|
| def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'):
|
| """Find an unused temporary file path on the device.
|
| @@ -23,9 +32,10 @@ class DeviceTempFile(object):
|
|
|
| Args:
|
| adb: An instance of AdbWrapper
|
| - suffix: The suffix of the name of the temp file.
|
| - prefix: The prefix of the name of the temp file.
|
| - dir: The directory on the device where to place the temp file.
|
| + suffix: The suffix of the name of the temporary file.
|
| + prefix: The prefix of the name of the temporary file.
|
| + dir: The directory on the device in which the temporary file should be
|
| + placed.
|
| Raises:
|
| ValueError if any of suffix, prefix, or dir are None.
|
| """
|
| @@ -36,8 +46,7 @@ class DeviceTempFile(object):
|
|
|
| self._adb = adb
|
| # Python's random module use 52-bit numbers according to its docs.
|
| - random_hex = hex(random.randint(0, 2 ** 52))[2:]
|
| - self.name = posixpath.join(dir, '%s-%s%s' % (prefix, random_hex, suffix))
|
| + self.name = _GenerateName(prefix, suffix, dir)
|
| self.name_quoted = cmd_helper.SingleQuote(self.name)
|
|
|
| def close(self):
|
| @@ -61,3 +70,46 @@ class DeviceTempFile(object):
|
|
|
| def __exit__(self, type, value, traceback):
|
| self.close()
|
| +
|
| +
|
| +class NamedDeviceTemporaryDirectory(object):
|
| + """A named temporary directory on a device.
|
| +
|
| + Behaves like tempfile_ext.NamdedTemporaryDirectory.
|
| + """
|
| +
|
| + def __init__(self, adb, suffix='', prefix='tmp', dir='/data/local/tmp'):
|
| + """Find an unused temporary directory path on the device and create it.
|
| +
|
| + When this object is closed, the directory will be deleted on the device.
|
| +
|
| + Args:
|
| + adb: An instance of AdbWrapper
|
| + suffix: The suffix of the name of the temporary directory.
|
| + prefix: The prefix of the name of the temporary directory.
|
| + dir: The directory on the device where to place the temporary directory.
|
| + Raises:
|
| + ValueError if any of suffix, prefix, or dir are None.
|
| + """
|
| + self._adb = adb
|
| + self.name = _GenerateName(prefix, suffix, dir)
|
| + self.name_quoted = cmd_helper.SingleQuote(self.name)
|
| + self._adb.Shell('mkdir -p %s' % self.name)
|
| +
|
| + def close(self):
|
| + """Deletes the temporary directory from the device."""
|
| + def delete_temporary_dir():
|
| + try:
|
| + self._adb.Shell('rm -rf %s' % self.name, expect_status=None)
|
| + except device_errors.AdbCommandFailedError:
|
| + pass
|
| +
|
| + threading.Thread(
|
| + target=delete_temporary_dir,
|
| + name='delete_temporary_dir(%s)' % self._adb.GetDeviceSerial()).start()
|
| +
|
| + def __enter__(self):
|
| + return self
|
| +
|
| + def __exit__(self, exc_type, exc_val, exc_tb):
|
| + self.close()
|
|
|