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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 """A temp file that automatically gets pushed and deleted from a device."""
6
7 # pylint: disable=W0622
8
9 import random
10 import time
11
12 class DeviceTempFile(object):
13 def __init__(self, device, prefix='temp_file', suffix=''):
14 """Find an unused temporary file path in the devices external directory.
15
16 When this object is closed, the file will be deleted on the device.
17
18 Args:
19 device: An instance of DeviceUtils
20 prefix: The prefix of the name of the temp file.
21 suffix: The suffix of the name of the temp file.
22 """
23 self._device = device
24 while True:
25 i = random.randint(0, 1000000)
26 self.name = '%s/%s-%d-%010d%s' % (
27 self._device.GetExternalStoragePath(),
28 prefix, int(time.time()), i, suffix)
29 if not self._device.FileExists(self.name):
30 break
31 # Immediately create an empty file so that other temp files can't
32 # be given the same name.
33 # |as_root| must be set to False due to the implementation of |WriteFile|.
34 # Having |as_root| be True may cause infinite recursion.
35 self._device.WriteFile(self.name, '', as_root=False)
36
37 def close(self):
38 """Deletes the temporary file from the device."""
39 self._device.RunShellCommand(['rm', self.name])
40
41 def __enter__(self):
42 return self
43
44 def __exit__(self, type, value, traceback):
45 self.close()
OLDNEW
« 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