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 6import contextlib 7import shutil 8import tempfile 9 10 11@contextlib.contextmanager 12def NamedTemporaryDirectory(suffix='', prefix='tmp', dir=None): 13 """A context manager that manages a temporary directory. 14 15 This is a context manager version of tempfile.mkdtemp. The arguments to this 16 function are the same as the arguments for that one. 17 """ 18 # This uses |dir| as a parameter name for consistency with mkdtemp. 19 # pylint: disable=redefined-builtin 20 21 d = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir) 22 try: 23 yield d 24 finally: 25 shutil.rmtree(d) 26