added -s|--summary, will print a short summary and then exit
authorCiro Scognamiglio <c.scognamiglio@cslash.net>
Thu, 12 Jul 2012 15:00:53 +0000 (17:00 +0200)
committerCiro Scognamiglio <c.scognamiglio@cslash.net>
Thu, 12 Jul 2012 15:00:53 +0000 (17:00 +0200)
scripts/clean-backupdb.py

index 4fcbb06..ab15ddf 100755 (executable)
@@ -151,19 +151,20 @@ class File:
     def cleanup (self, preserved):
         global counter
         counter+=1
+        src = os.path.abspath(os.path.basename(self.path()));
         if self.options.destination:
             dst = os.path.abspath(self.options.destination) + '/' + os.path.basename(self.path())
             if self.options.verbose:
                 print "moving %s\n\tto %s"%(self.path(), dst)
             if not self.options.dry_run:
-                os.rename (self.path(), dst)
+                os.rename (src, dst)
         else:
-            if self.options.dry_run:
-               print "Would cleanup %s"%(self.path())
+            if self.options.verbose:
+               print "Would cleanup %s"%(src)
                print "    (keeping %s)"%preserved.path()
-            else:
-                if self.options.verbose: print "unlink",self.path()
-                os.unlink (self.path())
+            if not self.options.dry_run:
+                if self.options.verbose: print "unlink",src
+                os.unlink (src)
 
 # all files in a given timeslot (either month or week)
 class Group:
@@ -282,7 +283,13 @@ class Index:
             self.insert (dir, filename, p, s, d)
 
     def cleanup (self):
-        for kind in self.index.values(): kind.cleanup()
+        for kind in self.index.values():
+            kind.cleanup()
+            
+    def summary (self):
+        print "%-30s%-10s%s"%("Prefix","Suffix","Num")
+        for kind in self.index.values():
+            print "%-30s%-10s%3s"%(kind.prefix, kind.suffix, len(kind.list))
 
 def handle_dir_pattern (index, dir, pattern):
     try:
@@ -306,6 +313,8 @@ def main ():
                        help="move to <destination> instead of removing the file")
     parser.add_option ("-o","--offset",dest='offset',action='store',type='int',default=0,
                        help="pretend we run <offset> days in the future")
+    parser.add_option ("-s","--summary",dest='summary',action='store_true',default=False,
+                       help="print a summary then exit")
     (options, args) = parser.parse_args()
     if options.extra_verbose: options.verbose=True
     try:
@@ -342,7 +351,10 @@ def main ():
     for (dir, pattern) in dir_patterns: handle_dir_pattern (index, dir, pattern)
     index.epilogue()
     index.show()
-    index.cleanup()
+    if (options.summary) :
+        index.summary()
+    else:
+        index.cleanup()
     print 'Found %d entries to unlink'%counter
             
 if __name__ == '__main__':