From ece93bb17b122140f0ac0e30cdf0815df17116f3 Mon Sep 17 00:00:00 2001 From: Hasan Wan Date: Mon, 4 Nov 2013 09:17:53 +0200 Subject: [PATCH] make retry function as decorator Change-Id: Ibbe7114af871b3cafc2dde6e4085b52e6737f20e Signed-off-by: Hasan Wan --- common/git.py | 3 ++- common/utils.py | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/common/git.py b/common/git.py index 5347c57..7146919 100644 --- a/common/git.py +++ b/common/git.py @@ -101,6 +101,7 @@ def _update_gitproject(localdir, gitpath=None): return True +@retry() def _clone_gitproject(giturl, gerritprj, localdir, bare=False): """Clone gerrit project from remote to local dir""" @@ -143,4 +144,4 @@ def clone_gitproject(gerritprj, localdir, giturl=None, bare=False): os.getenv('GERRIT_SSHPORT')) - return retry(_clone_gitproject, giturl, gerritprj, localdir, bare) + return _clone_gitproject(giturl, gerritprj, localdir, bare) diff --git a/common/utils.py b/common/utils.py index 363f60d..c0a4cc7 100644 --- a/common/utils.py +++ b/common/utils.py @@ -45,19 +45,23 @@ class Workdir(object): def __exit__(self, _type, _value, _tb): os.chdir(self._cwd) -def retry(func, *opts): +# Retry decorator +def retry(retries=3): """This will try to execute one function 3 times(by default) until \ success.""" - - retry_count = 3 - while retry_count > 0: - if (lambda func, *args: func(*args))(func, *opts): - return True - else: - print '%s failed, retrying...' % func - retry_count -= 1 - if not retry_count: - return False + def deco_retry(func): + def f_retry(*args): + retry_count = retries + f = func(*args) + while retry_count > 0: + if f == True: + return True + else: + print '%s failed, retrying...' % func.func_name + retry_count -= 1 + return False + return f_retry + return deco_retry def find_spec(workdir): """Return specfile list in workdir""" -- 2.7.4