Creates a directory, yields to the caller, and removes that directory
       if an exception is thrown.

def remove_on_exception(dirname, remove=True):
    """Creates a directory, yields to the caller, and removes that directory
       if an exception is thrown."""
    os.makedirs(dirname)
    try:
        yield
    except:
        if remove:
            shutil.rmtree(dirname, ignore_errors=True)
        raise