Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / devil / devil / android / device_temp_file.py
blob4d0c7adb5ca0b2c8ec902293525e3979a22f991a
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """A temp file that automatically gets pushed and deleted from a device."""
7 # pylint: disable=W0622
9 import posixpath
10 import random
11 import threading
13 from devil.android import device_errors
14 from devil.utils import cmd_helper
17 class DeviceTempFile(object):
19 def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'):
20 """Find an unused temporary file path on the device.
22 When this object is closed, the file will be deleted on the device.
24 Args:
25 adb: An instance of AdbWrapper
26 suffix: The suffix of the name of the temp file.
27 prefix: The prefix of the name of the temp file.
28 dir: The directory on the device where to place the temp file.
29 Raises:
30 ValueError if any of suffix, prefix, or dir are None.
31 """
32 if None in (dir, prefix, suffix):
33 m = 'Provided None path component. (dir: %s, prefix: %s, suffix: %s)' % (
34 dir, prefix, suffix)
35 raise ValueError(m)
37 self._adb = adb
38 # Python's random module use 52-bit numbers according to its docs.
39 random_hex = hex(random.randint(0, 2 ** 52))[2:]
40 self.name = posixpath.join(dir, '%s-%s%s' % (prefix, random_hex, suffix))
41 self.name_quoted = cmd_helper.SingleQuote(self.name)
43 def close(self):
44 """Deletes the temporary file from the device."""
45 # ignore exception if the file is already gone.
46 def delete_temporary_file():
47 try:
48 self._adb.Shell('rm -f %s' % self.name_quoted, expect_status=None)
49 except device_errors.AdbCommandFailedError:
50 # file does not exist on Android version without 'rm -f' support (ICS)
51 pass
53 # It shouldn't matter when the temp file gets deleted, so do so
54 # asynchronously.
55 threading.Thread(
56 target=delete_temporary_file,
57 name='delete_temporary_file(%s)' % self._adb.GetDeviceSerial()).start()
59 def __enter__(self):
60 return self
62 def __exit__(self, type, value, traceback):
63 self.close()