From f6fbb341935a52b227fe462ff846d9d8382d9192 Mon Sep 17 00:00:00 2001 From: Ezio <37951898+ezio416@users.noreply.github.com> Date: Wed, 15 Mar 2023 00:09:50 -0600 Subject: [PATCH] 0.60 scripts --- pyproject.toml | 6 ++- ..._and_upload.cmd => clear-build-upload.cmd} | 2 +- scripts/clear-build.cmd | 2 + scripts/flake_scripts.cmd | 5 ++ src/py416/__init__.py | 6 +-- src/py416/files.py | 15 +++--- src/py416/scripts.py | 51 +++++++++++++++++++ 7 files changed, 76 insertions(+), 11 deletions(-) rename scripts/{build_and_upload.cmd => clear-build-upload.cmd} (73%) create mode 100644 scripts/clear-build.cmd create mode 100644 scripts/flake_scripts.cmd create mode 100644 src/py416/scripts.py diff --git a/pyproject.toml b/pyproject.toml index d20a56a..080c374 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "py416" -version = "0.59" +version = "0.60" authors = [ { name="Ezio416", email="ezezio416@gmail.com" }, ] @@ -26,3 +26,7 @@ dependencies = [ [project.urls] "Homepage" = "https://github.com/ezio416/py416" "Bug Tracker" = "https://github.com/ezio416/py416/issues" + +[project.scripts] +rmd = "py416.scripts:rmd" +uzd = "py416.scripts:uzd" \ No newline at end of file diff --git a/scripts/build_and_upload.cmd b/scripts/clear-build-upload.cmd similarity index 73% rename from scripts/build_and_upload.cmd rename to scripts/clear-build-upload.cmd index ecb4ef1..851cd85 100644 --- a/scripts/build_and_upload.cmd +++ b/scripts/clear-build-upload.cmd @@ -1,4 +1,4 @@ clear_dist.py -py -m build .. +build_pkg twine upload ../dist/* pause \ No newline at end of file diff --git a/scripts/clear-build.cmd b/scripts/clear-build.cmd new file mode 100644 index 0000000..5793c26 --- /dev/null +++ b/scripts/clear-build.cmd @@ -0,0 +1,2 @@ +clear_dist.py +py -m build .. \ No newline at end of file diff --git a/scripts/flake_scripts.cmd b/scripts/flake_scripts.cmd new file mode 100644 index 0000000..5f7ee97 --- /dev/null +++ b/scripts/flake_scripts.cmd @@ -0,0 +1,5 @@ +@echo off +REM run flake8 on py416.scripts +REM E501 is the "line too long" error +flake8 --extend-ignore=E501 ../src/py416/scripts.py +set /p tmp="Press enter to exit" \ No newline at end of file diff --git a/src/py416/__init__.py b/src/py416/__init__.py index 50cb95d..ad2af28 100644 --- a/src/py416/__init__.py +++ b/src/py416/__init__.py @@ -2,11 +2,11 @@ Name: py416 Author: Ezio416 Created: 2022-08-15 -Updated: 2023-02-22 -Version: 0.59 +Updated: 2023-03-14 +Version: 0.60 A collection of various functions ''' from .general import * -__version__ = 0, 59 +__version__ = 0, 60 v = __version__ diff --git a/src/py416/files.py b/src/py416/files.py index 49f0c63..a3fb566 100644 --- a/src/py416/files.py +++ b/src/py416/files.py @@ -1,7 +1,7 @@ ''' | Author: Ezio416 | Created: 2022-08-16 -| Updated: 2023-02-22 +| Updated: 2023-03-14 - Functions for filesystem and path string manipulation @@ -1017,9 +1017,10 @@ def unzip(path: str, remove: bool = False) -> None: def unzipdir(path: str = '.', ignore_errors: bool = True) -> int: ''' - - unzips all archives in a folder (only 1st level) until it is unable to continue - - deletes all archives as it unzips - - supports archives of type: (.7z, .gz, .rar, .tar, .zip) + - unzips all archive files in a folder until it is unable to continue + - recursive on archive files, but not folders + - deletes all archive files as it unzips + - supports archive files of type: (.7z, .gz, .rar, .tar, .zip) Parameters ---------- @@ -1043,13 +1044,15 @@ def unzipdir(path: str = '.', ignore_errors: bool = True) -> int: while True: unzipped_this_run = 0 for file in listdir(path, dirs=False): + if not file.endswith(('.7z', '.gz', '.rar', '.tar', '.zip')): + continue try: unzip(file, remove=True) unzipped += 1 unzipped_this_run += 1 - except Exception: + except Exception as e: if not bool(ignore_errors): - raise + raise Exception(f'unzipped {unzipped} archive file(s) before failing: {e}') if not unzipped_this_run: break return unzipped diff --git a/src/py416/scripts.py b/src/py416/scripts.py new file mode 100644 index 0000000..cbcd5c3 --- /dev/null +++ b/src/py416/scripts.py @@ -0,0 +1,51 @@ +''' +| Author: Ezio416 +| Created: 2023-03-14 +| Updated: 2023-03-14 + +- Functions behind console scripts +- Call these functions directly by name from the command line +''' +import sys + +from .files import rmdir, unzipdir + + +def rmd(): + ''' + - recursively deletes empty directories + - wraps `os.rmdir() `_ + + Arguments + --------- + - none: delete empty folders where we are, but leave our parent intact + - one argument: path to folder + - folder in which to delete empty folders + - two arguments: path to folder, anything + - if a second value is present, we'll also try to delete the given folder + - the second value itself is ignored + ''' + if len(sys.argv) > 1: + if len(sys.argv) > 2: + print(f'deleted {rmdir(sys.argv[1])} empty folders') + return + print(f'deleted {rmdir(sys.argv[1], delroot=False)} empty folders') + return + print(f'deleted {rmdir(".", delroot=False)} empty folders') + + +def uzd(): + ''' + - unzips all archive files in a folder until it is unable to continue + - recursive on archive files, but not folders + - deletes all archive files as it unzips + - supports archive files of type: (.7z, .gz, .rar, .tar, .zip) + + Arguments + --------- + - if any argument is passed, the script will stop upon error + ''' + if len(sys.argv) > 1: + print(f'unzipped {unzipdir(ignore_errors=False)} archive file(s)') + return + print(f'unzipped {unzipdir()} archive file(s)')