Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: build/android/pylib/utils/device_temp_file.py

Issue 751063002: Allow RunShellCommand to work even with very large commands (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment implementation of WriteFile Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
12 class DeviceTempFile(object): 16 class DeviceTempFile(object):
13 def __init__(self, device, prefix='temp_file', suffix=''): 17 def __init__(self, adb, prefix='temp_file', suffix=''):
14 """Find an unused temporary file path in the devices external directory. 18 """Find an unused temporary file path in the devices external directory.
15 19
16 When this object is closed, the file will be deleted on the device. 20 When this object is closed, the file will be deleted on the device.
17 21
18 Args: 22 Args:
19 device: An instance of DeviceUtils 23 adb: An instance of AdbWrapper
20 prefix: The prefix of the name of the temp file. 24 prefix: The prefix of the name of the temp file.
21 suffix: The suffix of the name of the temp file. 25 suffix: The suffix of the name of the temp file.
22 """ 26 """
23 self._device = device 27 self._adb = adb
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)
24 while True: 31 while True:
25 i = random.randint(0, 1000000) 32 self.name = '{path}/{prefix}-{time:d}-{nonce:d}{suffix}'.format(
26 self.name = '%s/%s-%d-%010d%s' % ( 33 path=external_storage, prefix=prefix, time=int(time.time()),
27 self._device.GetExternalStoragePath(), 34 nonce=random.randint(0, 1000000), suffix=suffix)
28 prefix, int(time.time()), i, suffix) 35 self.name_quoted = cmd_helper.SingleQuote(self.name)
29 if not self._device.FileExists(self.name): 36 try:
30 break 37 self._adb.Shell('test -e %s' % self.name_quoted)
31 # Immediately create an empty file so that other temp files can't 38 except device_errors.AdbCommandFailedError:
32 # be given the same name. 39 break # file does not exist
33 # |as_root| must be set to False due to the implementation of |WriteFile|. 40
34 # Having |as_root| be True may cause infinite recursion. 41 # Immediately touch the file, so other temp files can't get the same name.
35 self._device.WriteFile(self.name, '', as_root=False) 42 self._adb.Shell('touch %s' % self.name_quoted)
36 43
37 def close(self): 44 def close(self):
38 """Deletes the temporary file from the device.""" 45 """Deletes the temporary file from the device."""
39 self._device.RunShellCommand(['rm', self.name]) 46 # we use -f to ignore errors if the file is already gone
47 self._adb.Shell('rm -f %s' % self.name_quoted)
40 48
41 def __enter__(self): 49 def __enter__(self):
42 return self 50 return self
43 51
44 def __exit__(self, type, value, traceback): 52 def __exit__(self, type, value, traceback):
45 self.close() 53 self.close()
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils_test.py ('k') | build/android/pylib/utils/device_temp_file_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698