Index: lib/cros_build_lib.py |
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py |
index 509871c6eedbe06ad9b9e160930e4eae4a3f2fa5..a07606c6447024b109668f3f34ee984e9febe03d 100644 |
--- a/lib/cros_build_lib.py |
+++ b/lib/cros_build_lib.py |
@@ -76,6 +76,37 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
return output |
+def RunCommandWithRetries(cmd, num_retries=5): |
+ """Runs a shell command with retries |
+ |
+ Runs a shell command. In case of failure, this function will retry the |
+ command up to num_retries times. |
+ |
+ Arguments: |
+ cmd: Array of commands/args to execute |
+ num_retries: The number of retries to perform before dying |
+ |
+ Returns: |
+ The stdout output of the command. |
+ |
+ Raises: |
+ Exception: Raises generic exception on error with optional error_message. |
+ """ |
+ output = '' |
+ |
+ for i in range(num_retries): |
+ try: |
+ output = RunCommand(cmd) |
+ break |
+ except Exception, e: |
+ if (i < num_retries - 1): |
+ Warning('Retrying command: %r' % cmd) |
+ else: |
+ raise |
+ |
+ return output |
+ |
+ |
class Color(object): |
"""Conditionally wraps text in ANSI color escape sequences.""" |
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |