summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@iki.fi>2008-05-18 19:10:59 +0300
committerLars Wirzenius <liw@iki.fi>2008-05-18 19:10:59 +0300
commit491c17244b0a8d9ec96a024f0be55a2fd42c4bb4 (patch)
treef6fe868091e3a19672f1a798b3390f3db6c13790
parent85f48ee98818840554f3e5f0b42bb38493a2153c (diff)
parente165403ab724c75904a515b45414b79c9e4869bb (diff)
downloadextrautils-491c17244b0a8d9ec96a024f0be55a2fd42c4bb4.tar.gz
Added some options to do-until.
-rwxr-xr-xdo-until78
-rw-r--r--do-until.129
2 files changed, 95 insertions, 12 deletions
diff --git a/do-until b/do-until
index d9dc863..be6efee 100755
--- a/do-until
+++ b/do-until
@@ -1,12 +1,66 @@
-#!/bin/sh
-#
-# do-until - run a command until it succeeds
-#
-# Lars Wirzenius <liw@liw.fi>
-
-cmdline="$@"
-while ! $cmdline
-do
- echo "Command failed, trying again in 1 second"
- sleep 1
-done
+#!/usr/bin/python
+
+
+"""Run a command until it succeeds.
+
+Lars Wirzenius <liw@liw.fi>
+"""
+
+
+import optparse
+import subprocess
+import sys
+import time
+
+
+def do_until(options, argv):
+ tries = 0
+ while options.max_tries == 0 or tries < options.max_tries:
+ tries += 1
+ if options.verbose:
+ sys.stderr.write("do-until: attempt #%d: %s\n" %
+ (tries, " ".join(argv)))
+ if options.no_act:
+ sys.stderr.write("do-until: not running command, pretending it "
+ "works anyway\n")
+ break
+ else:
+ p = subprocess.Popen(argv)
+ p.communicate()
+ if p.returncode == 0:
+ break
+ sys.stderr.write("do-until: command failed, "
+ "trying again in %d second(s)\n" % options.sleep)
+ time.sleep(options.sleep)
+
+
+def parse_args(args):
+ parser = optparse.OptionParser()
+ parser.add_option("-n", "--no-act", action="store_true",
+ help="Do not run command, just pretend to do it, and "
+ "pretend that it succeeds.")
+ parser.add_option("-v", "--verbose", action="store_true",
+ help="Print command before executing it.")
+ parser.add_option("--sleep", type="int", default=1, metavar="SECS",
+ help="Wait SECS seconds before re-trying a command. "
+ "(Default is %default.)")
+ parser.add_option("--max-tries", type="int", default=0, metavar="COUNT",
+ help="Try at most COUNT times, with 0 meaning forever. "
+ "(Default is %default.)")
+
+ options, argv = parser.parse_args(args)
+
+ return options, argv
+
+
+def main():
+ try:
+ options, argv = parse_args(sys.argv[1:])
+ if argv:
+ do_until(options, argv)
+ except KeyboardInterrupt:
+ sys.exit(1)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/do-until.1 b/do-until.1
index 16775e7..aa43aec 100644
--- a/do-until.1
+++ b/do-until.1
@@ -3,6 +3,12 @@
do-until \- repeat a command until it succeeds
.SH SYNOPSIS
.B do-until
+.RB [ -nv ]
+.RB [ --no-act ]
+.RB [ --verbose ]
+.RB [ --max-tries = \fICOUNT ]
+.RB [ --sleep = \fISECS ]
+.RB [ -- ]
.I command
.IR arg ...
.SH DESCRIPTION
@@ -30,3 +36,26 @@ If this matters, you may need to invoke the command you need via
"sh -c 'foo > bar'"
or some similar incantation.
See your shell manual for details.
+.SH OPTIONS
+.TP
+.BR -h ", " --help
+Write out a short usage summary.
+.TP
+.BR -n ", " --no-act
+Do not run the command, just pretend to run it.
+Also pretend it always succeeds.
+.TP
+.BR -v ", " --verbose
+Write the command out before executing it.
+.TP
+.BR --sleep =\fISECS
+Wait
+.I SECS
+seconds before re-trying a command.
+(Default is 1.)
+.TP
+.BR --max-tries =\fICOUNT
+Try at most
+COUNT
+times, with 0 meaning forever.
+(Default is 0.)