From 064b7b3dbf4277b9c57add479a478386504fc0d2 Mon Sep 17 00:00:00 2001 From: "Pawel Hajdan, Jr" Date: Mon, 10 Jul 2017 22:28:50 +0200 Subject: stabilization-candidates: retry network errors --- common.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'common.py') diff --git a/common.py b/common.py index 89e4996..8e0c14c 100644 --- a/common.py +++ b/common.py @@ -1,10 +1,12 @@ # Copyright 2012 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 +import functools import io import datetime import getpass import re +import time import portage @@ -17,6 +19,23 @@ def chunks(iterable, length): yield iterable[i:i + length] +def retry(ExceptionToCheck, tries=4, delay=3, backoff=2): + def deco_retry(f): + @functools.wraps(f) + def f_retry(*args, **kwargs): + mtries, mdelay = tries, delay + while mtries > 1: + try: + return f(*args, **kwargs) + except ExceptionToCheck: + time.sleep(mdelay) + mtries -= 1 + mdelay *= backoff + return f(*args, **kwargs) + return f_retry + return deco_retry + + # Snippet from http://bugs.python.org/issue9584 def expand_braces(orig): r = r'.*(\{.+?[^\\]\})' -- cgit v1.2.3-65-gdbad