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

Unified Diff: build/android/pylib/utils/device_temp_file.py

Issue 1313123002: Reland: Optimize DeviceTempFile() by combining logic into a single "adb shell" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/pylib/utils/device_temp_file.py
diff --git a/build/android/pylib/utils/device_temp_file.py b/build/android/pylib/utils/device_temp_file.py
index 7d3b95b10464263c1abb9f79f4f5e90322b741b6..4623b9047bb37fc3b35fab4fc07dba834f509538 100644
--- a/build/android/pylib/utils/device_temp_file.py
+++ b/build/android/pylib/utils/device_temp_file.py
@@ -6,12 +6,19 @@
# pylint: disable=W0622
-import random
-import time
-
from pylib import cmd_helper
from pylib.device import device_errors
+_COMMAND_TEMPLATE = (
+ # Make sure that the temp dir is writable
+ 'test -d {dir} && '
+ # If 5 random attempts fail, something is up.
+ 'for i in 1 2 3 4 5; do '
+ 'fn={dir}/{prefix}-$(date +%s)-"$RANDOM"{suffix};'
+ 'test -e "$fn" || break;'
+ 'done && '
+ # Touch the file, so other temp files can't get the same name.
+ 'touch "$fn" && echo -n "$fn"')
class DeviceTempFile(object):
def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'):
@@ -26,20 +33,12 @@ class DeviceTempFile(object):
dir: The directory on the device where to place the temp file.
"""
self._adb = adb
- # make sure that the temp dir is writable
- self._adb.Shell('test -d %s' % cmd_helper.SingleQuote(dir))
- while True:
- self.name = '{dir}/{prefix}-{time:d}-{nonce:d}{suffix}'.format(
- dir=dir, prefix=prefix, time=int(time.time()),
- nonce=random.randint(0, 1000000), suffix=suffix)
- self.name_quoted = cmd_helper.SingleQuote(self.name)
- try:
- self._adb.Shell('test -e %s' % self.name_quoted)
- except device_errors.AdbCommandFailedError:
- break # file does not exist
-
- # Immediately touch the file, so other temp files can't get the same name.
- self._adb.Shell('touch %s' % self.name_quoted)
+ command = _COMMAND_TEMPLATE.format(
+ dir=cmd_helper.SingleQuote(dir),
+ suffix=cmd_helper.SingleQuote(suffix),
+ prefix=cmd_helper.SingleQuote(prefix))
+ self.name = self._adb.Shell(command)
+ self.name_quoted = cmd_helper.SingleQuote(self.name)
def close(self):
"""Deletes the temporary file from the device."""
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698