Adding a '-n, --dry-run' option
authorStefan Huber <shuber@sthu.org>
Sun, 6 Jan 2013 20:18:42 +0000 (21:18 +0100)
committerStefan Huber <shuber@sthu.org>
Sun, 6 Jan 2013 20:18:42 +0000 (21:18 +0100)
sitarba

diff --git a/sitarba b/sitarba
index b67925f..160f09b 100755 (executable)
--- a/sitarba
+++ b/sitarba
@@ -15,6 +15,11 @@ import logging
 
 Modes = ["full", "incr", "diff"]
 
+
+class Options:
+    dryrun = False
+
+
 class Epoch:
 
     units = {
@@ -498,6 +503,9 @@ class BackupManager:
         if since != None:
             logging.debug("Making backup relative to " + since.ctime())
 
+        if Options.dryrun:
+            return
+
         yesno = self.ask_user_yesno("Proceed? [Y, n] ")
         if yesno == "n":
             return
@@ -507,12 +515,12 @@ class BackupManager:
         dirname = Backup.getDirName(now, epoch, mode)
         tmpdirname = dirname + ("-%x" % (random.random()*2e16) )
         targetdir = os.path.join(basedir, tmpdirname)
-        os.mkdir( targetdir )
+        os.mkdir(targetdir)
 
 
         # Add file logger
         logfile = logging.getLogger("backuplog")
-        fil = logging.FileHandler( os.path.join(targetdir, "log") )
+        fil = logging.FileHandler(os.path.join(targetdir, "log"))
         fil.setLevel(logging.DEBUG)
         logfile.addHandler(fil)
 
@@ -579,6 +587,9 @@ class BackupManager:
             logging.info("No stale/outdated entries to remove.")
             return
 
+        if Options.dryrun:
+            return
+
         basedir = self.conf.backupdir
         yesno = self.ask_user_yesno("Remove entries marked by '*'? [y, N] ")
         if yesno == "y":
@@ -617,6 +628,7 @@ def printUsage():
     print("  -e, --epoch EPOCH          force to create backup for given epoch, which")
     print("                             can be 'sporadic' or one of the configured epochs")
     print("  -m, --mode MODE            override mode: full, diff, or incr")
+    print("  -n, --dry-run              don't do anything, just tell what would be done")
     print("  -v, --verbose              be more verbose and interact with user")
     print("  --verbosity LEVEL          set verbosity to LEVEL, which can be")
     print("                             error, warning, info, debug")
@@ -686,6 +698,9 @@ if __name__ == "__main__":
                 logging.error("Unknown mode '" + mode + "'.")
                 exit(1)
 
+        elif opt in ["-n", "--dry-run"]:
+            Options.dryrun = True
+
         elif opt in ["-e", "--epoch"]:
             i += 1
             epoch = sys.argv[i]