def move_path_to_trash(path, preclean=True): """ Move a path to the trash """ # Try deleting the trash every time we use it. if preclean: delete_trash() from ..base.context import context for pkg_dir in context.pkgs_dirs: trash_dir = join(pkg_dir, '.trash') try: makedirs(trash_dir) except OSError as e1: if e1.errno != EEXIST: continue trash_file = join(trash_dir, text_type(uuid4())) try: rename(path, trash_file) except OSError as e: log.debug("Could not move %s to %s (%s)", path, trash_file, e) else: log.debug("Moved to trash: %s", path) from conda.install import delete_linked_data_any delete_linked_data_any(path) if not preclean: rm_rf(trash_file, max_retries=1, trash=False) return True return False
def move_path_to_trash(path, preclean=True): """ Move a path to the trash """ from ..base.context import context for pkg_dir in context.pkgs_dirs: trash_dir = join(pkg_dir, '.trash') try: makedirs(trash_dir) except (IOError, OSError) as e1: if e1.errno != EEXIST: continue trash_file = join(trash_dir, text_type(uuid4())) try: rename(path, trash_file) except (IOError, OSError) as e: log.debug("Could not move %s to %s.\n%r", path, trash_file, e) else: log.debug("Moved to trash: %s", path) from ..install import delete_linked_data_any delete_linked_data_any(path) return True return False
def rm_rf(path, max_retries=5, trash=True): """ Completely delete path max_retries is the number of times to retry on failure. The default is 5. This only applies to deleting a directory. If removing path fails and trash is True, files will be moved to the trash directory. """ if islink(path) or isfile(path): # Note that we have to check if the destination is a link because # exists('/path/to/dead-link') will return False, although # islink('/path/to/dead-link') is True. try: backoff_unlink(path) return except (OSError, IOError) as e: log.debug("%r errno %d\nCannot unlink %s.", e, e.errno, path) if trash and move_path_to_trash(path): return else: log.warn("Failed to remove %s.", path) elif isdir(path): try: # On Windows, always move to trash first. if trash and on_win and move_path_to_trash(path, preclean=False): return else: backoff_rmdir(path) finally: # If path was removed, ensure it's not in linked_data_ if not isdir(path): from conda.install import delete_linked_data_any delete_linked_data_any(path) else: log.debug("rm_rf failed. Not a link, file, or directory: %s", path)
def rm_rf(path, max_retries=5, trash=True): """ Completely delete path max_retries is the number of times to retry on failure. The default is 5. This only applies to deleting a directory. If removing path fails and trash is True, files will be moved to the trash directory. """ try: path = abspath(path) log.debug("rm_rf %s", path) if isdir(path): try: # On Windows, always move to trash first. if trash and on_win: move_result = move_path_to_trash(path, preclean=False) if move_result: return True backoff_rmdir(path) finally: # If path was removed, ensure it's not in linked_data_ if islink(path) or isfile(path): from conda.install import delete_linked_data_any delete_linked_data_any(path) if lexists(path): try: backoff_unlink(path) return True except (OSError, IOError) as e: log.debug("%r errno %d\nCannot unlink %s.", e, e.errno, path) if trash: move_result = move_path_to_trash(path) if move_result: return True log.info("Failed to remove %s.", path) else: log.debug("rm_rf failed. Not a link, file, or directory: %s", path) return True finally: if lexists(path): log.info("rm_rf failed for %s", path) return False
def rm_rf(path, max_retries=5, trash=True): """ Completely delete path max_retries is the number of times to retry on failure. The default is 5. This only applies to deleting a directory. If removing path fails and trash is True, files will be moved to the trash directory. """ try: path = abspath(path) log.debug("rm_rf %s", path) if isdir(path): try: # On Windows, always move to trash first. if trash and on_win: move_result = move_path_to_trash(path, preclean=False) if move_result: return True backoff_rmdir(path) finally: # If path was removed, ensure it's not in linked_data_ if not isdir(path): from conda.install import delete_linked_data_any delete_linked_data_any(path) elif lexists(path): try: backoff_unlink(path) return True except (OSError, IOError) as e: log.debug("%r errno %d\nCannot unlink %s.", e, e.errno, path) if trash: move_result = move_path_to_trash(path) if move_result: return True log.info("Failed to remove %s.", path) else: log.debug("rm_rf failed. Not a link, file, or directory: %s", path) return True finally: if lexists(path): log.info("rm_rf failed for %s", path) return False