OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """A temp file that automatically gets pushed and deleted from a device.""" | 5 """A temp file that automatically gets pushed and deleted from a device.""" |
6 | 6 |
7 # pylint: disable=W0622 | 7 # pylint: disable=W0622 |
8 | 8 |
9 import random | 9 import random |
10 import time | 10 import time |
11 | 11 |
12 from pylib import cmd_helper | |
13 from pylib.device import device_errors | |
14 | |
15 | |
16 class DeviceTempFile(object): | 12 class DeviceTempFile(object): |
17 def __init__(self, adb, prefix='temp_file', suffix=''): | 13 def __init__(self, device, prefix='temp_file', suffix=''): |
18 """Find an unused temporary file path in the devices external directory. | 14 """Find an unused temporary file path in the devices external directory. |
19 | 15 |
20 When this object is closed, the file will be deleted on the device. | 16 When this object is closed, the file will be deleted on the device. |
21 | 17 |
22 Args: | 18 Args: |
23 adb: An instance of AdbWrapper | 19 device: An instance of DeviceUtils |
24 prefix: The prefix of the name of the temp file. | 20 prefix: The prefix of the name of the temp file. |
25 suffix: The suffix of the name of the temp file. | 21 suffix: The suffix of the name of the temp file. |
26 """ | 22 """ |
27 self._adb = adb | 23 self._device = device |
28 external_storage = self._adb.Shell('echo $EXTERNAL_STORAGE').rstrip() | |
29 # make sure that external storage is readable | |
30 self._adb.Shell('test -d %s' % external_storage) | |
31 while True: | 24 while True: |
32 self.name = '{path}/{prefix}-{time:d}-{nonce:d}{suffix}'.format( | 25 i = random.randint(0, 1000000) |
33 path=external_storage, prefix=prefix, time=int(time.time()), | 26 self.name = '%s/%s-%d-%010d%s' % ( |
34 nonce=random.randint(0, 1000000), suffix=suffix) | 27 self._device.GetExternalStoragePath(), |
35 self.name_quoted = cmd_helper.SingleQuote(self.name) | 28 prefix, int(time.time()), i, suffix) |
36 try: | 29 if not self._device.FileExists(self.name): |
37 self._adb.Shell('test -e %s' % self.name_quoted) | 30 break |
38 except device_errors.AdbCommandFailedError: | 31 # Immediately create an empty file so that other temp files can't |
39 break # file does not exist | 32 # be given the same name. |
40 | 33 # |as_root| must be set to False due to the implementation of |WriteFile|. |
41 # Immediately touch the file, so other temp files can't get the same name. | 34 # Having |as_root| be True may cause infinite recursion. |
42 self._adb.Shell('touch %s' % self.name_quoted) | 35 self._device.WriteFile(self.name, '', as_root=False) |
43 | 36 |
44 def close(self): | 37 def close(self): |
45 """Deletes the temporary file from the device.""" | 38 """Deletes the temporary file from the device.""" |
46 # we use -f to ignore errors if the file is already gone | 39 self._device.RunShellCommand(['rm', self.name]) |
47 self._adb.Shell('rm -f %s' % self.name_quoted) | |
48 | 40 |
49 def __enter__(self): | 41 def __enter__(self): |
50 return self | 42 return self |
51 | 43 |
52 def __exit__(self, type, value, traceback): | 44 def __exit__(self, type, value, traceback): |
53 self.close() | 45 self.close() |
OLD | NEW |