summaryrefslogtreecommitdiff
path: root/do-until
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-02-14 22:29:18 +0000
committerLars Wirzenius <liw@liw.fi>2011-02-14 22:29:18 +0000
commitc69f66a658d739b9dbfa2a890afc48a1028b8d39 (patch)
tree43cb285fbdf9b1c571b29a2e1b734a34c544fa89 /do-until
parente9fb0ff9a711fa7f29691c4ca821785d603a892f (diff)
downloadextrautils-c69f66a658d739b9dbfa2a890afc48a1028b8d39.tar.gz
Convert to using cliapp.
Diffstat (limited to 'do-until')
-rwxr-xr-xdo-until93
1 files changed, 40 insertions, 53 deletions
diff --git a/do-until b/do-until
index 429772b..3d001e7 100755
--- a/do-until
+++ b/do-until
@@ -1,7 +1,7 @@
#!/usr/bin/python
#
# do-until - run a command until it succeeds
-# Copyright (C) 2008 Lars Wirzenius
+# Copyright (C) 2008, 2011 Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -17,66 +17,53 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-"""Run a command until it succeeds.
+'''Run a command until it succeeds.'''
-Lars Wirzenius <liw@liw.fi>
-"""
-
-import optparse
+import cliapp
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
+class DoUntil(cliapp.Application):
+ def add_settings(self):
+ self.add_boolean_setting(['no-act', 'n'],
+ 'Do not run command, just pretend to do it, '
+ 'and pretend that it succeeds.')
+ self.add_boolean_setting(['verbose', 'v'],
+ 'Print command before executing it.')
+ self.add_integer_setting(['sleep'],
+ 'Wait SECS seconds before re-trying a '
+ 'command. (Default is %default.)',
+ default=1)
+ self.add_integer_setting(['max-tries'],
+ 'Try at most COUNT times, with 0 meaning '
+ 'forever. (Default is %default.)',
+ default=0)
-def main():
- try:
- options, argv = parse_args(sys.argv[1:])
- if argv:
- do_until(options, argv)
- except KeyboardInterrupt:
- sys.exit(1)
+ def process_args(self, argv):
+ tries = 0
+ while self.options.max_tries == 0 or tries < self.options.max_tries:
+ tries += 1
+ if self.options.verbose:
+ sys.stderr.write('do-until: attempt #%d: %s\n' %
+ (tries, ' '.join(argv)))
+ if self.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' %
+ self.options.sleep)
+ time.sleep(self.options.sleep)
-if __name__ == "__main__":
- main()
+if __name__ == '__main__':
+ DoUntil().run()