You need to sign in or sign up before continuing.
tempdir.py 994 Bytes
Newer Older
1 2 3 4 5 6 7
"""Make temporary directories nicely."""

import atexit
import os.path
import shutil
import tempfile

8

9
def mkdtemp_clean(suffix="", prefix="tmp", dir=None):   # pylint: disable=redefined-builtin
10 11 12 13 14
    """Just like mkdtemp, but the directory will be deleted when the process ends."""
    the_dir = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
    atexit.register(cleanup_tempdir, the_dir)
    return the_dir

15

16 17 18 19
def cleanup_tempdir(the_dir):
    """Called on process exit to remove a temp directory."""
    if os.path.exists(the_dir):
        shutil.rmtree(the_dir)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38


def create_symlink(src, dest):
    """
    Creates a symbolic link which will be deleted when the process ends.
    :param src: path to source
    :param dest: path to destination
    """
    os.symlink(src, dest)
    atexit.register(delete_symlink, dest)


def delete_symlink(link_path):
    """
    Removes symbolic link for
    :param link_path:
    """
    if os.path.exists(link_path):
        os.remove(link_path)