[perf-web] Add 'python manage.py prunelogs'



commit 50285a9548969ef030b29402ea2994810fdf9e44
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Tue Sep 23 09:58:13 2014 -0400

    Add 'python manage.py prunelogs'
    
    The prunelogs command deletes all but the last 5 logs for each
    target.

 metrics/management/commands/prunelogs.py |   46 ++++++++++++++++++++++++++++++
 1 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/metrics/management/commands/prunelogs.py b/metrics/management/commands/prunelogs.py
new file mode 100644
index 0000000..b6ea39a
--- /dev/null
+++ b/metrics/management/commands/prunelogs.py
@@ -0,0 +1,46 @@
+from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
+
+import os
+import re
+import sys
+from datetime import datetime
+
+LOG_RE = re.compile(r'^(\d{4}-\d\d-\d\d-\d\d:\d\d:\d\d)-[a-f0-9]+.json$')
+
+# Number of logs to retain for each target
+NUM_RETAIN = 5
+
+class Command(BaseCommand):
+    help = 'Clean up old log files'
+
+    def handle(self, *args, **options):
+        first = True
+        for target in os.listdir(settings.LOG_ROOT):
+            if not os.path.isdir(target):
+                continue
+
+            target_dir = os.path.join(settings.LOG_ROOT, target)
+
+            to_sort = []
+            for f in os.listdir(target_dir):
+                m = LOG_RE.match(f)
+                if not m:
+                    print >>sys.stderr, "Log file " + f + " doesn't match pattern"
+                    continue
+                date = datetime.strptime(m.group(1), "%Y-%m-%d-%H:%M:%S")
+                to_sort.append((date, f))
+
+            to_sort.sort(lambda a, b: cmp(a[0], b[0]))
+            if len(to_sort) <= NUM_RETAIN:
+                continue
+
+            if first:
+                first = False
+            else:
+                print
+
+            print target + ":"
+            for (date, f) in to_sort[:len(to_sort)-NUM_RETAIN]:
+                print "Removing: " + f
+                os.remove(os.path.join(target_dir, f))


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]