Browse Source

rename param `--delete-re` to `--delete-path-regex` (to avoid ambiguity)

Fabian Peter Hammerle 1 year ago
parent
commit
e5821e5159
4 changed files with 17 additions and 6 deletions
  1. 3 0
      CHANGELOG.md
  2. 1 0
      README.md
  3. 4 4
      free_disk/__init__.py
  4. 9 2
      tests/test_cleanup.py

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- param `--delete-path-regex`
+
 ### Changed
 - made all constants and functions private
 

+ 1 - 0
README.md

@@ -19,6 +19,7 @@ pip3 install --user --upgrade free-disk
 ```sh
 free-disk --help
 free-disk --free-bytes 1GiB /dir/to/cleanup
+free-disk --free-bytes 1GiB --delete-path-regex '\.mp4$' /dir/to/cleanup
 free-disk --debug --free-bytes 2GB /dir/to/cleanup
 ```
 

+ 4 - 4
free_disk/__init__.py

@@ -45,8 +45,9 @@ def _main() -> None:
     argparser = argparse.ArgumentParser(description=__doc__)
     argparser.add_argument("-d", "--debug", action="store_true")
     argparser.add_argument(
-        "--delete-re",
-        action="store",
+        "--delete-path-regex",
+        metavar="REGULAR_EXPRESSION",
+        type=re.compile,  # type: ignore
         help="Only delete files with path matching regular expression (at any position)."
         " Paths will not be resolved or made absolute before check."
         r" Examples: \.mp4$ or ^/tmp/\d or ^rel/ative/ (default: no filter)",
@@ -76,9 +77,8 @@ def _main() -> None:
         for dirpath, _, filenames in os.walk(args.root_dir_path)
         for filename in filenames
     ]
-    delete_re = re.compile(args.delete_re)
     file_mtime_paths = [
-        (os.stat(p).st_mtime, p) for p in file_paths if delete_re.search(p)
+        (os.stat(p).st_mtime, p) for p in file_paths if args.delete_path_regex.search(p)
     ]
     file_mtime_paths.sort()
     removed_files_counter = 0

+ 9 - 2
tests/test_cleanup.py

@@ -137,7 +137,14 @@ def test__main_path_regex_absolute(
         lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
     ), unittest.mock.patch(
         "sys.argv",
-        ["", "--free-bytes", "42B", str(tmp_path), "--delete-re", r"a/a|^b|c$|^.*/d$"],
+        [
+            "",
+            "--free-bytes",
+            "42B",
+            str(tmp_path),
+            "--delete-path-regex",
+            r"a/a|^b|c$|^.*/d$",
+        ],
     ), caplog.at_level(
         logging.INFO
     ):
@@ -163,7 +170,7 @@ def test__main_path_regex_relative(
         lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
     ), unittest.mock.patch(
         "sys.argv",
-        ["", "--free-bytes", "123B", ".", "--delete-re", r"/aa|^b|\d$|^\./c$"],
+        ["", "--free-bytes", "123B", ".", "--delete-path-regex", r"/aa|^b|\d$|^\./c$"],
     ), caplog.at_level(
         logging.INFO
     ):