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

Side by Side Diff: scripts/slave/robust_tempdir.py

Issue 1879173003: annotated_run: extract robust_tempdir so it can be shared with kitchen_run (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: presubmit Created 4 years, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « scripts/slave/annotated_run.py ('k') | scripts/slave/unittests/annotated_run_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2016 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 import logging
6 import os
7 import sys
8 import tempfile
9
10 # Install Infra build environment.
11 BUILD_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
12 os.path.abspath(__file__))))
13 sys.path.insert(0, os.path.join(BUILD_ROOT, 'scripts'))
14
15 from common import chromium_utils
16
17
18 LOGGER = logging.getLogger('robust_tempdir')
19
20
21 def _ensure_directory(*path):
22 path = os.path.join(*path)
23 if not os.path.isdir(path):
24 os.makedirs(path)
25 return path
26
27
28 class RobustTempdir(object):
29 """RobustTempdir is a ContextManager that tracks generated files and cleans
30 them up at exit.
31 """
32
33 def __init__(self, prefix, leak=False):
kjellander_chromium 2016/04/13 09:57:44 Can you document the parameters? It's not obvious
34 self._tempdirs = []
35 self._prefix = prefix
36 self._leak = leak
37
38 def cleanup(self, path):
39 self._tempdirs.append(path)
40
41 def tempdir(self, base=None):
42 """Creates a temporary working directory and yields it.
43
44 This creates two levels of directory:
45 <base>/<prefix>
46 <base>/<prefix>/tmpFOO
47
48 On termination, the entire "<base>/<prefix>" directory is deleted,
49 removing the subdirectory created by this instance as well as cleaning up
50 any other temporary subdirectories leaked by previous executions.
51
52 Args:
53 base (str/None): The directory under which the tempdir should be created.
54 If None, the default temporary directory root will be used.
55 """
56 base = base or tempfile.gettempdir()
57 # TODO(phajdan.jr): Clean up leaked directories at the beginning.
58 # Doing so should automatically prevent more out-of-disk-space scenarios.
59 basedir = _ensure_directory(base, self._prefix)
60 self.cleanup(basedir)
61 tdir = tempfile.mkdtemp(dir=basedir)
62 return tdir
63
64 def __enter__(self):
65 return self
66
67 def __exit__(self, _et, _ev, _tb):
68 self.close()
69
70 def close(self):
71 if self._leak:
kjellander_chromium 2016/04/13 09:57:44 This refers to the --leak flag to annotated_run.py
72 LOGGER.warning('(--leak) Leaking temporary paths: %s', self._tempdirs)
73 else:
74 for path in reversed(self._tempdirs):
75 try:
76 if os.path.isdir(path):
77 LOGGER.debug('Cleaning up temporary directory [%s].', path)
78 chromium_utils.RemoveDirectory(path)
79 except BaseException:
80 LOGGER.exception('Failed to clean up temporary directory [%s].',
81 path)
82 del(self._tempdirs[:])
OLDNEW
« no previous file with comments | « scripts/slave/annotated_run.py ('k') | scripts/slave/unittests/annotated_run_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698