| 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 | |
| 10 import time | |
| 11 | |
| 12 from pylib import cmd_helper | 9 from pylib import cmd_helper |
| 13 from pylib.device import device_errors | 10 from pylib.device import device_errors |
| 14 | 11 |
| 12 _COMMAND_TEMPLATE = ( |
| 13 # Make sure that the temp dir is writable |
| 14 'test -d {dir} && ' |
| 15 # If 5 random attempts fail, something is up. |
| 16 'for i in 1 2 3 4 5; do ' |
| 17 'fn={dir}/{prefix}-$(date +%s)-"$RANDOM"{suffix};' |
| 18 'test -e "$fn" || break;' |
| 19 'done && ' |
| 20 # Touch the file, so other temp files can't get the same name. |
| 21 'touch "$fn" && echo -n "$fn"') |
| 15 | 22 |
| 16 class DeviceTempFile(object): | 23 class DeviceTempFile(object): |
| 17 def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'): | 24 def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'): |
| 18 """Find an unused temporary file path in the devices external directory. | 25 """Find an unused temporary file path in the devices external directory. |
| 19 | 26 |
| 20 When this object is closed, the file will be deleted on the device. | 27 When this object is closed, the file will be deleted on the device. |
| 21 | 28 |
| 22 Args: | 29 Args: |
| 23 adb: An instance of AdbWrapper | 30 adb: An instance of AdbWrapper |
| 24 suffix: The suffix of the name of the temp file. | 31 suffix: The suffix of the name of the temp file. |
| 25 prefix: The prefix of the name of the temp file. | 32 prefix: The prefix of the name of the temp file. |
| 26 dir: The directory on the device where to place the temp file. | 33 dir: The directory on the device where to place the temp file. |
| 27 """ | 34 """ |
| 28 self._adb = adb | 35 self._adb = adb |
| 29 # make sure that the temp dir is writable | 36 command = _COMMAND_TEMPLATE.format( |
| 30 self._adb.Shell('test -d %s' % cmd_helper.SingleQuote(dir)) | 37 dir=cmd_helper.SingleQuote(dir), |
| 31 while True: | 38 suffix=cmd_helper.SingleQuote(suffix), |
| 32 self.name = '{dir}/{prefix}-{time:d}-{nonce:d}{suffix}'.format( | 39 prefix=cmd_helper.SingleQuote(prefix)) |
| 33 dir=dir, prefix=prefix, time=int(time.time()), | 40 self.name = self._adb.Shell(command) |
| 34 nonce=random.randint(0, 1000000), suffix=suffix) | 41 self.name_quoted = cmd_helper.SingleQuote(self.name) |
| 35 self.name_quoted = cmd_helper.SingleQuote(self.name) | |
| 36 try: | |
| 37 self._adb.Shell('test -e %s' % self.name_quoted) | |
| 38 except device_errors.AdbCommandFailedError: | |
| 39 break # file does not exist | |
| 40 | |
| 41 # Immediately touch the file, so other temp files can't get the same name. | |
| 42 self._adb.Shell('touch %s' % self.name_quoted) | |
| 43 | 42 |
| 44 def close(self): | 43 def close(self): |
| 45 """Deletes the temporary file from the device.""" | 44 """Deletes the temporary file from the device.""" |
| 46 # ignore exception if the file is already gone. | 45 # ignore exception if the file is already gone. |
| 47 try: | 46 try: |
| 48 self._adb.Shell('rm -f %s' % self.name_quoted) | 47 self._adb.Shell('rm -f %s' % self.name_quoted) |
| 49 except device_errors.AdbCommandFailedError: | 48 except device_errors.AdbCommandFailedError: |
| 50 # file does not exist on Android version without 'rm -f' support (ICS) | 49 # file does not exist on Android version without 'rm -f' support (ICS) |
| 51 pass | 50 pass |
| 52 | 51 |
| 53 def __enter__(self): | 52 def __enter__(self): |
| 54 return self | 53 return self |
| 55 | 54 |
| 56 def __exit__(self, type, value, traceback): | 55 def __exit__(self, type, value, traceback): |
| 57 self.close() | 56 self.close() |
| OLD | NEW |