| 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 threading |
| 10 |
| 9 from pylib import cmd_helper | 11 from pylib import cmd_helper |
| 10 from pylib.device import device_errors | 12 from pylib.device import device_errors |
| 11 | 13 |
| 12 _COMMAND_TEMPLATE = ( | 14 _COMMAND_TEMPLATE = ( |
| 13 # Make sure that the temp dir is writable | 15 # Make sure that the temp dir is writable |
| 14 'test -d {dir} && ' | 16 'test -d {dir} && ' |
| 15 # If 5 random attempts fail, something is up. | 17 # If 5 random attempts fail, something is up. |
| 16 'for i in 1 2 3 4 5; do ' | 18 'for i in 1 2 3 4 5; do ' |
| 17 'fn={dir}/{prefix}-$(date +%s)-"$RANDOM"{suffix};' | 19 'fn={dir}/{prefix}-$(date +%s)-"$RANDOM"{suffix};' |
| 18 'test -e "$fn" || break;' | 20 'test -e "$fn" || break;' |
| (...skipping 17 matching lines...) Expand all Loading... |
| 36 command = _COMMAND_TEMPLATE.format( | 38 command = _COMMAND_TEMPLATE.format( |
| 37 dir=cmd_helper.SingleQuote(dir), | 39 dir=cmd_helper.SingleQuote(dir), |
| 38 suffix=cmd_helper.SingleQuote(suffix), | 40 suffix=cmd_helper.SingleQuote(suffix), |
| 39 prefix=cmd_helper.SingleQuote(prefix)) | 41 prefix=cmd_helper.SingleQuote(prefix)) |
| 40 self.name = self._adb.Shell(command) | 42 self.name = self._adb.Shell(command) |
| 41 self.name_quoted = cmd_helper.SingleQuote(self.name) | 43 self.name_quoted = cmd_helper.SingleQuote(self.name) |
| 42 | 44 |
| 43 def close(self): | 45 def close(self): |
| 44 """Deletes the temporary file from the device.""" | 46 """Deletes the temporary file from the device.""" |
| 45 # ignore exception if the file is already gone. | 47 # ignore exception if the file is already gone. |
| 46 try: | 48 def helper(): |
| 47 self._adb.Shell('rm -f %s' % self.name_quoted) | 49 try: |
| 48 except device_errors.AdbCommandFailedError: | 50 self._adb.Shell('rm -f %s' % self.name_quoted, expect_status=None) |
| 49 # file does not exist on Android version without 'rm -f' support (ICS) | 51 except device_errors.AdbCommandFailedError: |
| 50 pass | 52 # file does not exist on Android version without 'rm -f' support (ICS) |
| 53 pass |
| 54 |
| 55 # It shouldn't matter when the temp file gets deleted, so do so |
| 56 # asynchronously. |
| 57 threading.Thread(target=helper).start() |
| 51 | 58 |
| 52 def __enter__(self): | 59 def __enter__(self): |
| 53 return self | 60 return self |
| 54 | 61 |
| 55 def __exit__(self, type, value, traceback): | 62 def __exit__(self, type, value, traceback): |
| 56 self.close() | 63 self.close() |
| OLD | NEW |