Browse Source

regex matching: no longer imply matching at beginning of path

Fabian Peter Hammerle 1 year ago
parent
commit
2f74a7858e
2 changed files with 21 additions and 14 deletions
  1. 5 3
      free_disk/__init__.py
  2. 16 11
      tests/test_cleanup.py

+ 5 - 3
free_disk/__init__.py

@@ -47,8 +47,10 @@ def _main() -> None:
     argparser.add_argument(
         "--delete-re",
         action="store",
-        help="Only delete files matching regexp. examples: .*mp4$",
-        default=".*",
+        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)",
+        default="",
     )
     argparser.add_argument(
         "--free-bytes",
@@ -76,7 +78,7 @@ def _main() -> None:
     ]
     delete_re = re.compile(args.delete_re)
     file_mtime_paths = [
-        (os.stat(p).st_mtime, p) for p in file_paths if delete_re.match(p)
+        (os.stat(p).st_mtime, p) for p in file_paths if delete_re.search(p)
     ]
     file_mtime_paths.sort()
     removed_files_counter = 0

+ 16 - 11
tests/test_cleanup.py

@@ -127,22 +127,24 @@ def test__main_path_regex_absolute(
     caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
 ) -> None:
     tmp_path.joinpath("a").mkdir()
-    tmp_path.joinpath("a", "aa").write_bytes(b"a" * 4)
-    tmp_path.joinpath("b").write_bytes(b"b" * 3)
-    tmp_path.joinpath("c").write_bytes(b"c" * 5)
+    tmp_path.joinpath("a", "aa").write_bytes(b"aa")
+    tmp_path.joinpath("a", "a~").write_bytes(b"a~")
+    tmp_path.joinpath("b").write_bytes(b"b")
+    tmp_path.joinpath("c").write_bytes(b"c")
+    tmp_path.joinpath("d").write_bytes(b"d")
     with unittest.mock.patch(
         "shutil.disk_usage",
         lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
     ), unittest.mock.patch(
         "sys.argv",
-        ["", "--free-bytes", "35B", str(tmp_path), "--delete-re", r".*a/a|/b|b|.*c$"],
+        ["", "--free-bytes", "42B", str(tmp_path), "--delete-re", r"a/a|^b|c$|^.*/d$"],
     ), caplog.at_level(
         logging.INFO
     ):
         free_disk._main()
     assert {p.name for p in tmp_path.rglob("*")} == {"a", "b"}
     assert caplog.records[-1].message.startswith(
-        "Removed 2 file(s) with modification date <= 20"
+        "Removed 4 file(s) with modification date <= 20"
     )
 
 
@@ -150,15 +152,18 @@ def test__main_path_regex_relative(
     caplog: _pytest.logging.LogCaptureFixture, tmp_path: pathlib.Path
 ) -> None:
     tmp_path.joinpath("a").mkdir()
-    tmp_path.joinpath("a", "aa").write_bytes(b"a" * 4)
-    tmp_path.joinpath("b").write_bytes(b"b" * 3)
-    tmp_path.joinpath("c").write_bytes(b"c" * 5)
+    tmp_path.joinpath("a", "aa").write_bytes(b"aa")
+    tmp_path.joinpath("a", "aaa").write_bytes(b"aaa")
+    tmp_path.joinpath("a", "A").write_bytes(b"A")
+    tmp_path.joinpath("b").write_bytes(b"b")
+    tmp_path.joinpath("b2").write_bytes(b"b2")
+    tmp_path.joinpath("c").write_bytes(b"c")
     with unittest.mock.patch(
         "shutil.disk_usage",
         lambda p: _DiskUsage(free=42 - _folder_content_size_bytes(tmp_path)),
     ), unittest.mock.patch(
         "sys.argv",
-        ["", "--free-bytes", "35B", ".", "--delete-re", r"\./a/a|b|\./c$"],
+        ["", "--free-bytes", "123B", ".", "--delete-re", r"/aa|^b|\d$|^\./c$"],
     ), caplog.at_level(
         logging.INFO
     ):
@@ -168,7 +173,7 @@ def test__main_path_regex_relative(
             free_disk._main()
         finally:
             os.chdir(old_working_dir)
-    assert {p.name for p in tmp_path.rglob("*")} == {"a", "b"}
+    assert {p.name for p in tmp_path.rglob("*")} == {"a", "A", "b"}
     assert caplog.records[-1].message.startswith(
-        "Removed 2 file(s) with modification date <= 20"
+        "Removed 4 file(s) with modification date <= 20"
     )