From 67cdba538dad96087eaaac5cf4d63ac38744bbce Mon Sep 17 00:00:00 2001 From: Austin Gibbons Date: Mon, 15 Jul 2024 11:58:02 -0400 Subject: [PATCH 1/2] Add option to remove empty mount directories on startup --- src/thumbtack/__init__.py | 16 +++++++++++++--- src/thumbtack/directory_monitoring.py | 7 ++++++- src/thumbtack/utils.py | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/thumbtack/__init__.py b/src/thumbtack/__init__.py index 782d1a5..c010374 100644 --- a/src/thumbtack/__init__.py +++ b/src/thumbtack/__init__.py @@ -20,7 +20,7 @@ __version__ = "Could not find version" -def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, path_contains=None, skip_subdirectory=None): +def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, path_contains=None, skip_subdirectory=None, remove_directories=None): if base_url: static_url_path = f"{base_url}/static" @@ -59,6 +59,9 @@ def create_app(mount_dir=None, image_dir=None, database=None, base_url=None, pat if skip_subdirectory: app.config.update(SKIP_SUBDIRECTORY=skip_subdirectory) + if remove_directories: + app.config.update(REMOVE_DIRECTORIES=remove_directories) + app.mnt_mutex = threading.Lock() # configure the rest @@ -170,9 +173,16 @@ def configure_logging(app): show_default=True, help="Subdirectory to ignore when monitoring files", ) -def start_app(debug, host, port, mount_dir, image_dir, database, base_url, path_contains, skip_subdirectory): +@click.option( + '-r', + "--remove-directories", + default=False, + is_flag=True, + help="Remove empty directories in the mount directory on startup.", +) +def start_app(debug, host, port, mount_dir, image_dir, database, base_url, path_contains, skip_subdirectory, remove_directories): app = create_app( - mount_dir=mount_dir, image_dir=image_dir, database=database, base_url=base_url, path_contains=path_contains, skip_subdirectory=skip_subdirectory + mount_dir=mount_dir, image_dir=image_dir, database=database, base_url=base_url, path_contains=path_contains, skip_subdirectory=skip_subdirectory, remove_directories=remove_directories, ) directory_monitoring_thread = DirectoryMonitoring(app) directory_monitoring_thread.start() diff --git a/src/thumbtack/directory_monitoring.py b/src/thumbtack/directory_monitoring.py index d34db18..a3e5f4e 100644 --- a/src/thumbtack/directory_monitoring.py +++ b/src/thumbtack/directory_monitoring.py @@ -1,7 +1,7 @@ import threading import time -from .utils import monitor_image_dir +from .utils import monitor_image_dir, startup_remove_dirs class DirectoryMonitoring(threading.Thread): @@ -11,6 +11,11 @@ def __init__(self, app): def run(self): with self.app.app_context(): + try: + if self.app.config["REMOVE_DIRECTORIES"]: + startup_remove_dirs() + except (KeyError, TypeError): + pass while True: time.sleep(3) monitor_image_dir() diff --git a/src/thumbtack/utils.py b/src/thumbtack/utils.py index a5e357f..e811d95 100644 --- a/src/thumbtack/utils.py +++ b/src/thumbtack/utils.py @@ -600,6 +600,24 @@ def monitor_image_dir(): if Path(image["full_path"]) not in full_path_filenames ] +def startup_remove_dirs(): + for item in os.listdir(current_app.config["MOUNT_DIR"]): + path = Path(current_app.config["MOUNT_DIR"], item) + current_app.logger.info(path) + try: + current_app.logger.info(f"Unmounting: {path}") + subprocess.run(["umount", path]) + except Exception as e: + current_app.logger.error(e) + + if os.path.isdir(path): + current_app.logger.info(path) + try: + current_app.logger.info(f"Removing: {path}") + os.rmdir(path) + except Exception as e: + current_app.logger.error(e) + def get_db(): db = getattr(g, "_database", None) From c9a1ec0729f9c81a62d92dfd192432fbbd5c0c7d Mon Sep 17 00:00:00 2001 From: Austin Gibbons Date: Wed, 17 Jul 2024 16:41:20 -0400 Subject: [PATCH 2/2] Update docs and readme with new command line option --- README.rst | 9 ++++----- docs/tutorial.rst | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index 4e032cb..0b05497 100644 --- a/README.rst +++ b/README.rst @@ -62,13 +62,12 @@ Quick Reference -h, --host TEXT Host to run Thumbtack server on [default: 127.0.0.1] -p, --port TEXT Port to run Thumbtack server on [default: 8208] -m, --mount-dir TEXT Directory to mount disk images [Default: /mnt/thumbtack] - -i, --image-dir TEXT Directory of disk images for Thumbtack server to - monitor [default: $CWD] - --db TEXT SQLite database to store mount state [default: - database.db] - -b, --base-url TEXT Base URL where Thumbtack is hosted on the server + -i, --image-dir TEXT Directory of disk images for Thumbtack server to monitor [Default: $CWD] + --db TEXT SQLite database to store mount state + -b, --base-url TEXT Base URL where Thumbtack is hosted on the server [default: /] --path-contains TEXT Only select files containing specified string in the path -s, --skip-subdirectory TEXT Subdirectory to ignore when monitoring files + -r, --remove-directories Remove empty directories in the mount directory on startup. --help Show this message and exit. LICENSE diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 19b07ef..116a2a4 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -112,13 +112,13 @@ See them below. -d, --debug Run the Thumbtack server in debug mode -h, --host TEXT Host to run Thumbtack server on [default: 127.0.0.1] -p, --port TEXT Port to run Thumbtack server on [default: 8208] - -i, --image-dir TEXT Directory of disk images for Thumbtack server to - monitor [default: $CWD] - --db TEXT SQLite database to store mount state [default: - database.db] - -b, --base-url TEXT Base URL where Thumbtack is hosted on the server + -m, --mount-dir TEXT Directory to mount disk images [Default: /mnt/thumbtack] + -i, --image-dir TEXT Directory of disk images for Thumbtack server to monitor [Default: $CWD] + --db TEXT SQLite database to store mount state + -b, --base-url TEXT Base URL where Thumbtack is hosted on the server [default: /] --path-contains TEXT Only select files containing specified string in the path -s, --skip-subdirectory TEXT Subdirectory to ignore when monitoring files + -r, --remove-directories Remove empty directories in the mount directory on startup. --help Show this message and exit. Development Environment