From c69f66a658d739b9dbfa2a890afc48a1028b8d39 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 14 Feb 2011 22:29:18 +0000 Subject: Convert to using cliapp. --- do-until | 93 ++++++++++++++++++++++++++++------------------------------------ 1 file changed, 40 insertions(+), 53 deletions(-) (limited to 'do-until') 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 . -"""Run a command until it succeeds. +'''Run a command until it succeeds.''' -Lars Wirzenius -""" - -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() -- cgit v1.2.1