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

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

Issue 706203003: Update from https://crrev.com/303153 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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
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
new file mode 100644
index 0000000000000000000000000000000000000000..2a9b45101803e18f2ca8046a25e8bb126e5624b4
--- /dev/null
+++ b/build/android/pylib/utils/device_temp_file.py
@@ -0,0 +1,45 @@
+# Copyright 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""A temp file that automatically gets pushed and deleted from a device."""
+
+# pylint: disable=W0622
+
+import random
+import time
+
+class DeviceTempFile(object):
+ def __init__(self, device, prefix='temp_file', suffix=''):
+ """Find an unused temporary file path in the devices external directory.
+
+ When this object is closed, the file will be deleted on the device.
+
+ Args:
+ device: An instance of DeviceUtils
+ prefix: The prefix of the name of the temp file.
+ suffix: The suffix of the name of the temp file.
+ """
+ self._device = device
+ while True:
+ i = random.randint(0, 1000000)
+ self.name = '%s/%s-%d-%010d%s' % (
+ self._device.GetExternalStoragePath(),
+ prefix, int(time.time()), i, suffix)
+ if not self._device.FileExists(self.name):
+ break
+ # Immediately create an empty file so that other temp files can't
+ # be given the same name.
+ # |as_root| must be set to False due to the implementation of |WriteFile|.
+ # Having |as_root| be True may cause infinite recursion.
+ self._device.WriteFile(self.name, '', as_root=False)
+
+ def close(self):
+ """Deletes the temporary file from the device."""
+ self._device.RunShellCommand(['rm', self.name])
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self.close()
« no previous file with comments | « build/android/pylib/instrumentation/test_runner.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