# Copyright 2015 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. """Miscellaneous utility functions.""" import contextlib import shutil import sys import tempfile # We're trying to be compatible with Python3 tempfile.TemporaryDirectory # context manager here. And they used 'dir' as a keyword argument. # pylint: disable=redefined-builtin @contextlib.contextmanager def temporary_directory(suffix="", prefix="tmp", dir=None, keep_directory=False): """Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example: with temporary_directory() as tmpdir: ... Upon exiting the context, the directory and everything contained in it are removed. Args: suffix, prefix, dir: same arguments as for tempfile.mkdtemp. keep_directory (bool): if True, do not delete the temporary directory when exiting. Useful for debugging. Returns: tempdir (str): full path to the temporary directory. """ tempdir = None # Handle mkdtemp raising an exception try: tempdir = tempfile.mkdtemp(suffix, prefix, dir) yield tempdir finally: if tempdir and not keep_directory: # pragma: no branch try: # TODO(pgervais,496347) Make this work reliably on Windows. shutil.rmtree(tempdir, ignore_errors=True) except OSError as ex: # pragma: no cover print >> sys.stderr, ( "ERROR: {!r} while cleaning up {!r}".format(ex, tempdir))