Ejemplo n.º 1
0
def chdir(path):
    """
    Changes the directory temporarily.
    """

    path = os.path.abspath(path)
    saved_dir = os.getcwd()

    if os.path.abspath(saved_dir) != os.path.abspath(path):
        LOGGER.debug(
            "Temporarily changing current directory from %s to %s",
            hl(saved_dir),
            hl(path),
        )

        os.chdir(path)

    try:
        yield
    finally:
        if os.path.abspath(saved_dir) != os.path.abspath(path):
            LOGGER.debug(
                "Changing back current directory from %s to %s",
                hl(path),
                hl(saved_dir),
            )

            os.chdir(saved_dir)
Ejemplo n.º 2
0
        def onerror(func, path, excinfo):
            if os.path.exists(path):
                LOGGER.debug('Was unable to delete "%s": %s', hl(path), excinfo[1])
                LOGGER.debug('Trying again after changing permissions...')
                os.chmod(path, stat.S_IWUSR)

                try:
                    func(path)
                except Exception as ex:
                    LOGGER.error('Unable to delete "%s": %s', hl(path), excinfo[1])

                    raise
Ejemplo n.º 3
0
def chdir(path):
    """
    Change the current directory.
    """

    old_path = os.getcwd()

    LOGGER.debug('Moving to: %s', hl(path))
    os.chdir(path)

    try:
        yield path
    finally:
        LOGGER.debug('Moving back to: %s', hl(old_path))
        os.chdir(old_path)
Ejemplo n.º 4
0
def rmdir(path):
    """
    Delete the specified path if it exists.

    Does nothing if the path doesn't exist.
    """

    try:
        LOGGER.info('Removing directory at %s.', hl(path))

        def onerror(func, path, excinfo):
            if os.path.exists(path):
                LOGGER.debug('Was unable to delete "%s": %s', hl(path), excinfo[1])
                LOGGER.debug('Trying again after changing permissions...')
                os.chmod(path, stat.S_IWUSR)

                try:
                    func(path)
                except Exception as ex:
                    LOGGER.error('Unable to delete "%s": %s', hl(path), excinfo[1])

                    raise

        shutil.rmtree(path, ignore_errors=False, onerror=onerror)

    except Exception as ex:
        LOGGER.warning(ex)
Ejemplo n.º 5
0
def temporary_copy(source_path, target_path, persistent=False):
    """
    Copy a source path to a target path.

    The target will be deleted upon function exist, unless `persistent`
    is truthy.
    """

    try:
        if os.path.exists(target_path):
            rmdir(target_path)

        LOGGER.info('Copying %s to %s...', hl(source_path), hl(target_path))
        copytree(source_path, target_path, copy_function=getattr(os, 'link', shutil.copy2))

        yield target_path

    finally:
        if not persistent:
            rmdir(target_path)
        else:
            LOGGER.info('Not erasing temporary directory at %s.', hl(target_path))
Ejemplo n.º 6
0
def mkdir(path):
    """
    Create the specified path.

    Does nothing if the path exists.
    """

    try:
        if not os.path.isdir(path):
            LOGGER.debug('Creating directory at %s.', hl(path))

        os.makedirs(path)

    except OSError as ex:
        if ex.errno != errno.EEXIST or not os.path.isdir(path):
            raise