diff --git a/examples/lib/package/ex_remove_files.py b/examples/lib/package/ex_remove_files.py new file mode 100644 index 0000000..7099ace --- /dev/null +++ b/examples/lib/package/ex_remove_files.py @@ -0,0 +1,18 @@ +import logging + +from hyo2.abc2.lib.logging import set_logging +from hyo2.abc2.lib.package.pkg_helper import PkgHelper + +logger = logging.getLogger(__name__) +set_logging(ns_list=["hyo2.abc2"]) + +folder_path = r"C:\code\hyo2\hyo2_grids" + +filter_files = ( + r"desktop.ini", +) +filter_folders = ( + r"__pycache__", +) + +PkgHelper.clean_folder(folder=folder_path, filter_files=filter_files, filter_folders=filter_folders) diff --git a/hyo2/abc2/__init__.py b/hyo2/abc2/__init__.py index d6fdf21..71f7072 100644 --- a/hyo2/abc2/__init__.py +++ b/hyo2/abc2/__init__.py @@ -12,7 +12,7 @@ logger.addHandler(logging.NullHandler()) name = "ABC" -__version__ = "2.3.10" +__version__ = "2.3.11" __license__ = "LGPLv3 license" __copyright__ = "Copyright 2024 University of New Hampshire, Center for Coastal and Ocean Mapping" diff --git a/hyo2/abc2/lib/package/pkg_helper.py b/hyo2/abc2/lib/package/pkg_helper.py index ea118be..20ac19e 100644 --- a/hyo2/abc2/lib/package/pkg_helper.py +++ b/hyo2/abc2/lib/package/pkg_helper.py @@ -3,10 +3,10 @@ import logging import os import platform +import shutil import subprocess import sys from datetime import datetime, timezone -from typing import Optional import psutil from appdirs import user_data_dir @@ -24,6 +24,38 @@ class PkgHelper: def __init__(self, pkg_info: PkgInfo): self._pi = pkg_info + @classmethod + def clean_folder(cls, folder: str, filter_files: tuple[str] | None, filter_folders: tuple[str] | None) -> None: + + if not os.path.exists(folder): + raise RuntimeError("Unable to locate the folder to clean: %s" % folder) + + logger.info("cleaning folder: %s ..." % folder) + if not filter_files: + filter_files = () + logger.debug("file filters: %s" % (filter_files,)) + if not filter_folders: + filter_folders = () + logger.debug("folder filters: %s" % (filter_folders,)) + + for dir_path, dir_names, files in os.walk(folder): + + for d in dir_names: + + if d in filter_folders: + full_path = os.path.join(dir_path, d) + shutil.rmtree(full_path) + logger.debug("deleted folder: %s" % full_path) + + for f in files: + + if f in filter_files: + full_path = os.path.join(dir_path, f) + os.remove(full_path) + logger.debug("deleted file: %s" % full_path) + + logger.info("cleaning folder: %s ... DONE" % folder) + @classmethod def explore_folder(cls, path: str) -> bool: """Open the passed path using OS-native commands""" @@ -383,7 +415,7 @@ def import_class(cl): return msg - def web_url(self, suffix: Optional[str] = None) -> str: + def web_url(self, suffix: str | None = None) -> str: url = '%s%s' % (self._pi.url, self._pi.version.replace('.', '_'),) if self.is_pydro(): diff --git a/scripts/cleaning/remove_pycache.py b/scripts/cleaning/remove_pycache.py deleted file mode 100644 index eb69fe9..0000000 --- a/scripts/cleaning/remove_pycache.py +++ /dev/null @@ -1,22 +0,0 @@ -import os -import shutil - - -def remove_pycache(folder: str): - - print("folder path: %s" % folder) - - for dir_path, dir_names, files in os.walk(folder): - - for d in dir_names: - - if d == "__pycache__": - - full_path = os.path.join(dir_path, d) - print(full_path) - shutil.rmtree(full_path) - - -folder_path = r"C:\code\hyo2\abc\hyo2_huddl" - -remove_pycache(folder=folder_path)