]> git.phdru.name Git - ppu.git/commitdiff
Remove empty directories
authorOleg Broytman <phd@phdru.name>
Sun, 16 Apr 2017 07:46:55 +0000 (10:46 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 16 Apr 2017 08:24:17 +0000 (11:24 +0300)
TODO
docs/index.rst
docs/news.rst
remove-old-files.py
tests/test_remove_old_files.py

diff --git a/TODO b/TODO
index 459559df34108da9073eda3f3486a3c7c400b321..0cd98e84b7f46f1e243716d3f9cd7205b0fb18d4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,6 +2,3 @@ Add option -v/--verbose to report every removed file.
 
 
 Add options to limit files or to exclude files by name, extension, path.
-
-
-Remove empty directories.
index 1fa41621e8280de94716b9a1d9a53fa169e2248d..a438400ca1dca0fee802c49ccefda09b4b2b1666 100644 (file)
@@ -29,6 +29,8 @@ Usage::
 
 Options::
 
+    -e, --empty-dirs
+                           remove empty directories
     -o days, --older days
                            remove files older than this number of days;
                            this is a required option
index 9dc7a82ca664a7358a80303261339b2b7f9d6848..3d3375d9752d9b84045c91f7b1f908727d683fb6 100644 (file)
@@ -1,6 +1,11 @@
 News
 ====
 
+0.1.0 (2017-04-16)
+------------------
+
+* Remove empty directories.
+
 0.0.1 (2017-04-16)
 ------------------
 
index 79d5b69ca1986ac2674d75bbc9987020a8c4adab..5ff7d80402730403944807876f8de624fcf45fc1 100755 (executable)
@@ -7,6 +7,8 @@ import os
 
 if __name__ == '__main__':
     parser = argparse.ArgumentParser(description='Remove old files')
+    parser.add_argument('-e', '--empty-dirs', action='store_true',
+                        help='remove empty directories')
     parser.add_argument('-o', '--older', required=True, type=int,
                         help='remove files older than this (in days)')
     parser.add_argument('start_dir', help='start from this directory')
@@ -16,6 +18,7 @@ if __name__ == '__main__':
     now = datetime.now()
 
     for dirpath, dirnames, filenames in os.walk(args.start_dir, topdown=False):
+        has_newer_files = False
         for fname in filenames:
             file_path = os.path.join(dirpath, fname)
             file_modified = datetime.fromtimestamp(os.path.getmtime(file_path))
@@ -26,6 +29,14 @@ if __name__ == '__main__':
                     os.remove(file_path)
                 except OSError:
                     errors += 1
+            else:
+                has_newer_files = True
+        if args.empty_dirs and not dirnames and not has_newer_files:
+            count += 1
+            try:
+                os.rmdir(dirpath)
+            except OSError:
+                errors += 1
 
-    print("Removed {0:d} files, freed {1:d} bytes, {2:d} errors".format(
+    print("Removed {0:d} files/dirs, freed {1:d} bytes, {2:d} errors".format(
           count, size, errors))
index e50b6fad711e5dbe2642b5b4c6f28c028da978d6..67da601952463f0c5379af8614b0b371aa7332f7 100755 (executable)
@@ -61,3 +61,17 @@ def test_recursive():
     assert os.system("remove-old-files.py --older 100 .") == 0
     assert_files_exist(test3)
     assert_files_not_exist(test4)
+
+
+def test_remove_empty_directory():
+    create_files(['test3', 'test4'], 'subdir')
+    test3 = os.path.join('subdir', 'test3')
+    test4 = os.path.join('subdir', 'test4')
+    assert_files_exist([test3, test4])
+    os.utime(test3, (0, 0))
+    os.utime(test4, (0, 0))
+    assert os.system("remove-old-files.py --older 100 .") == 0
+    assert_files_exist('subdir')
+    assert_files_not_exist([test3, test4])
+    assert os.system("remove-old-files.py -e --older 100 .") == 0
+    assert_files_not_exist('subdir')