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

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: less mind-bendy version 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 adb_wrapper
14 from pylib.device import device_errors
15
16
12 class DeviceTempFile(object): 17 class DeviceTempFile(object):
13 def __init__(self, device, prefix='temp_file', suffix=''): 18 def __init__(self, device, prefix='temp_file', suffix=''):
14 """Find an unused temporary file path in the devices external directory. 19 """Find an unused temporary file path in the devices external directory.
15 20
16 When this object is closed, the file will be deleted on the device. 21 When this object is closed, the file will be deleted on the device.
17 22
18 Args: 23 Args:
19 device: An instance of DeviceUtils 24 device: An instance of AdbWrapper, or an object with an 'adb' property
20 prefix: The prefix of the name of the temp file. 25 prefix: The prefix of the name of the temp file.
21 suffix: The suffix of the name of the temp file. 26 suffix: The suffix of the name of the temp file.
22 """ 27 """
23 self._device = device 28 if isinstance(device, adb_wrapper.AdbWrapper):
29 self._adb = device
30 elif (hasattr(device, 'adb')
31 and isinstance(device.adb, adb_wrapper.AdbWrapper)):
perezju 2014/11/25 16:25:33 I think this should be quite safe, and allows us t
jbudorick 2014/11/25 17:05:26 Does it make sense to pass DeviceUtils at all? Dev
32 self._adb = device.adb
33 else:
34 raise ValueError('Unsupported type passed for argument "device"')
35
36 external_storage = self._adb.Shell('echo $EXTERNAL_STORAGE').rstrip()
37 # make sure that external storage is readable
38 self._adb.Shell('test -d %s' % external_storage)
24 while True: 39 while True:
25 i = random.randint(0, 1000000) 40 self.name = '{path}/{prefix}-{time:d}-{nonce:d}{suffix}'.format(
26 self.name = '%s/%s-%d-%010d%s' % ( 41 path=external_storage, prefix=prefix, time=int(time.time()),
27 self._device.GetExternalStoragePath(), 42 nonce=random.randint(0, 1000000), suffix=suffix)
28 prefix, int(time.time()), i, suffix) 43 self.name_quoted = cmd_helper.SingleQuote(self.name)
perezju 2014/11/25 16:25:33 we use this quite often, so thought it was a bette
29 if not self._device.FileExists(self.name): 44 try:
30 break 45 self._adb.Shell('test -e %s' % self.name_quoted)
31 # Immediately create an empty file so that other temp files can't 46 except device_errors.AdbCommandFailedError:
32 # be given the same name. 47 break # file does not exist
33 # |as_root| must be set to False due to the implementation of |WriteFile|. 48
34 # Having |as_root| be True may cause infinite recursion. 49 # Immediately touch the file, so other temp files can't get the same name.
35 self._device.WriteFile(self.name, '', as_root=False) 50 self._adb.Shell('touch %s' % self.name_quoted)
36 51
37 def close(self): 52 def close(self):
38 """Deletes the temporary file from the device.""" 53 """Deletes the temporary file from the device."""
39 self._device.RunShellCommand(['rm', self.name]) 54 # we use -f to ignore errors if the file is already gone
55 self._adb.Shell('rm -f %s' % self.name_quoted)
40 56
41 def __enter__(self): 57 def __enter__(self):
42 return self 58 return self
43 59
44 def __exit__(self, type, value, traceback): 60 def __exit__(self, type, value, traceback):
45 self.close() 61 self.close()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698