diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/python/redo/redo/cmd.py | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/python/redo/redo/cmd.py')
-rw-r--r-- | third_party/python/redo/redo/cmd.py | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/third_party/python/redo/redo/cmd.py b/third_party/python/redo/redo/cmd.py new file mode 100644 index 0000000000..aeb65dbb3e --- /dev/null +++ b/third_party/python/redo/redo/cmd.py @@ -0,0 +1,70 @@ +# ***** BEGIN LICENSE BLOCK ***** +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at http://mozilla.org/MPL/2.0/. +# ***** END LICENSE BLOCK ***** +import logging +from subprocess import check_call, CalledProcessError +import sys + +from redo import retrying + +log = logging.getLogger(__name__) + + +def main(argv): + from argparse import ArgumentParser, REMAINDER + + parser = ArgumentParser() + parser.add_argument( + "-a", "--attempts", type=int, default=5, help="How many times to retry." + ) + parser.add_argument( + "-s", + "--sleeptime", + type=int, + default=60, + help="How long to sleep between attempts. Sleeptime doubles after each attempt.", + ) + parser.add_argument( + "-m", + "--max-sleeptime", + type=int, + default=5 * 60, + help="Maximum length of time to sleep between attempts (limits backoff length).", + ) + parser.add_argument("-v", "--verbose", action="store_true", default=False) + parser.add_argument( + "cmd", nargs=REMAINDER, help="Command to run. Eg: wget http://blah" + ) + + args = parser.parse_args(argv[1:]) + + if args.verbose: + logging.basicConfig(level=logging.INFO) + logging.getLogger("retry").setLevel(logging.INFO) + else: + logging.basicConfig(level=logging.ERROR) + logging.getLogger("retry").setLevel(logging.ERROR) + + try: + with retrying( + check_call, + attempts=args.attempts, + sleeptime=args.sleeptime, + max_sleeptime=args.max_sleeptime, + retry_exceptions=(CalledProcessError,), + ) as r_check_call: + r_check_call(args.cmd) + except KeyboardInterrupt: + sys.exit(-1) + except Exception as e: + log.error( + "Unable to run command after %d attempts" % args.attempts, exc_info=True + ) + rc = getattr(e, "returncode", -2) + sys.exit(rc) + + +if __name__ == "__main__": + main(sys.argv) |