Example #1
0
    def rmdir(
        self,
        path: PATH_TYPES,
        recurse: bool = False,
        force: bool = False,
        ignore_errors: bool = False,
        dry_run: bool = False,
    ) -> int:
        """
        Remove the directory. If recurse is True, allow removing empty children.

        If force is true, remove contents destructively.
        """
        if not isinstance(path, self.PATH_BACKEND):
            path = self.PATH_BACKEND(path)
        log_prefix = "[DRY RUN] " if dry_run else ""
        logger.info(f"{log_prefix}Removing file: {path!s}")
        if not dry_run:
            path.rmdir()
        return 0
Example #2
0
 def rmdir(
     self,
     path: PATH_TYPES,
     recurse: bool = False,
     force: bool = False,
     ignore_errors: bool = False,
     dry_run: bool = False,
 ) -> int:
     """Remove the directory. If recurse is True, allow removing empty children.
     If force is true, remove contents destructively."""
     if not isinstance(path, pathlib.Path):
         path = pathlib.Path(path)
     log_prefix = "[DRY RUN] " if dry_run else ""
     if force:
         logger.info(f"{log_prefix}Forcing removal of files under {path!s}")
         if not dry_run:
             shutil.rmtree(path, ignore_errors=ignore_errors)
             return 0
     if recurse:
         for subdir in path.iterdir():
             if not subdir.is_dir():
                 continue
             logger.info(f"{log_prefix}Removing directory: {subdir!s}")
             if not dry_run:
                 rc = self.rmdir(
                     subdir,
                     recurse=recurse,
                     force=force,
                     ignore_errors=ignore_errors,
                 )
                 if rc != 0:
                     return rc
     logger.info(f"{log_prefix}Removing directory: {path!s}")
     if not dry_run:
         path.rmdir()
     return 0