make retry function as decorator
authorHasan Wan <hasan.wan@intel.com>
Mon, 4 Nov 2013 07:17:53 +0000 (09:17 +0200)
committerGerrit Code Review <gerrit2@otctools.jf.intel.com>
Mon, 4 Nov 2013 07:54:32 +0000 (23:54 -0800)
Change-Id: Ibbe7114af871b3cafc2dde6e4085b52e6737f20e
Signed-off-by: Hasan Wan <hasan.wan@intel.com>
common/git.py
common/utils.py

index 5347c57..7146919 100644 (file)
@@ -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)
index 363f60d..c0a4cc7 100644 (file)
@@ -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"""