Skip to content

Commit

Permalink
added helper funtion to delete folders and files from folder
Browse files Browse the repository at this point in the history
  • Loading branch information
giumas committed Nov 2, 2024
1 parent 36f7636 commit 78305c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
18 changes: 18 additions & 0 deletions examples/lib/package/ex_remove_files.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion hyo2/abc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
36 changes: 34 additions & 2 deletions hyo2/abc2/lib/package/pkg_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"""
Expand Down Expand Up @@ -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():
Expand Down
22 changes: 0 additions & 22 deletions scripts/cleaning/remove_pycache.py

This file was deleted.

0 comments on commit 78305c5

Please sign in to comment.