From 02344ed6f25bb3475ddc69aa42d2b3066a808f31 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Tue, 2 Jan 2024 09:40:30 +0100 Subject: [PATCH] clean: Longer but easier to maintain for me. --- .local/bin/clean | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/.local/bin/clean b/.local/bin/clean index 43b8b39..2857ff4 100755 --- a/.local/bin/clean +++ b/.local/bin/clean @@ -1,5 +1,31 @@ -#!/bin/sh +#!/usr/bin/env python3.12 +import sys +from pathlib import Path +import shutil -find -maxdepth 4 \ - \( -name '*~' -o -name '#*#' -o -name .tox -o -name .mypy_cache \) \ - -print0 | xargs -0 rm -vfr + +def find(path, maxdepth=3, prune=()): + for child in path.iterdir(): + if child.name in prune: + continue + yield child + if child.is_dir() and maxdepth > 0: + yield from find(child, prune=prune, maxdepth=maxdepth - 1) + + +def rm(path): + print(f"rm -fr {path}") + if "-n" in sys.argv: + return + if path.is_file(): + path.unlink() + else: + shutil.rmtree(path) + + +PRUNE = {"backup", "auto-save-list"} +TO_DROP = {Path(".tox"), Path(".mypy_cache"), Path(".envrc"), Path(".venv"), Path("*~"), Path("#*#")} + +for file in find(Path("."), prune=PRUNE): + if any(file.match(pat) for pat in TO_DROP): + rm(file)