]> git.phdru.name Git - ppu.git/commitdiff
Add remove-old-files.py - the main script in the package
authorOleg Broytman <phd@phdru.name>
Sat, 15 Apr 2017 18:46:43 +0000 (21:46 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 15 Apr 2017 19:52:57 +0000 (22:52 +0300)
remove-old-files.py [new file with mode: 0755]
remove-old-files/__init__.py [deleted file]
remove-old-files/__version__.py [deleted file]
setup.py

diff --git a/remove-old-files.py b/remove-old-files.py
new file mode 100755 (executable)
index 0000000..79d5b69
--- /dev/null
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+import argparse
+from datetime import datetime, timedelta
+import os
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Remove old files')
+    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')
+    args = parser.parse_args()
+
+    count = errors = size = 0
+    now = datetime.now()
+
+    for dirpath, dirnames, filenames in os.walk(args.start_dir, topdown=False):
+        for fname in filenames:
+            file_path = os.path.join(dirpath, fname)
+            file_modified = datetime.fromtimestamp(os.path.getmtime(file_path))
+            if file_modified + timedelta(days=args.older) < now:
+                count += 1
+                size += os.path.getsize(file_path)
+                try:
+                    os.remove(file_path)
+                except OSError:
+                    errors += 1
+
+    print("Removed {0:d} files, freed {1:d} bytes, {2:d} errors".format(
+          count, size, errors))
diff --git a/remove-old-files/__init__.py b/remove-old-files/__init__.py
deleted file mode 100644 (file)
index 792d600..0000000
+++ /dev/null
@@ -1 +0,0 @@
-#
diff --git a/remove-old-files/__version__.py b/remove-old-files/__version__.py
deleted file mode 100644 (file)
index b8023d8..0000000
+++ /dev/null
@@ -1 +0,0 @@
-__version__ = '0.0.1'
index c789abeee184c7cca45cac5d353e5f618c543eea..20ab4a3cb10580fe8109e7badee99d649015f763 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -33,6 +33,6 @@ setup(name='remove-old-files',
           'Programming Language :: Python :: 3.5',
           'Programming Language :: Python :: 3.6',
       ],
-      scripts=[],
+      scripts=['remove-old-files.py'],
       requires=[],
       )