Chromium Code Reviews| OLD | NEW |
|---|---|
| (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[:]) | |
| OLD | NEW |