use_umask: if False, the mode of the created directory will be explicitly
    set to `mode` using os.chmod.  By default, `mode` is altered by the current
    umask.

    For use in a with statement, this function returns a context manager that
    makes and a directory directly under root with the given mode that is
    guaranteed to be uniquely named by using the uuid module.  Then it yields
    the directory path.  When the context is exited the directory and
    everything under it will be removed.

    This function is useful if you need an isolated place to do some temporary
    work with files and dirs without worrying about naming conflicts and
    without having to write boilerplate and error handling to make sure the
    directory is cleaned up.

    Example:

        with temps.tmpdir() as workingdir:
            do_this(workingdir)
            do_that(workingdir)

def tmpdir(root=TEMPS_DIR, prefix=TEMPS_PREFIX, suffix=TEMPS_SUFFIX,
             mode=TEMPS_MODE, use_umask=True):
    '''
    use_umask: if False, the mode of the created directory will be explicitly
    set to `mode` using os.chmod.  By default, `mode` is altered by the current
    umask.

    For use in a with statement, this function returns a context manager that
    makes and a directory directly under root with the given mode that is
    guaranteed to be uniquely named by using the uuid module.  Then it yields
    the directory path.  When the context is exited the directory and
    everything under it will be removed.

    This function is useful if you need an isolated place to do some temporary
    work with files and dirs without worrying about naming conflicts and
    without having to write boilerplate and error handling to make sure the
    directory is cleaned up.

    Example:

        with temps.tmpdir() as workingdir:
            do_this(workingdir)
            do_that(workingdir)
    '''
    path = tmppath(root, prefix, suffix)
    os.makedirs(path, mode=mode)
    if not use_umask:
        os.chmod(path, mode)

    try:
        yield path
    finally:
        shutil.rmtree(path)