Index: lib/cros_build_lib.py |
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py |
index d7da384e09b48f605246b027fa6eaa4767dbdbd0..838d75162f593ed655a77860aea1a9ac0beaf600 100644 |
--- a/lib/cros_build_lib.py |
+++ b/lib/cros_build_lib.py |
@@ -50,7 +50,8 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
Else returns the output of the shell command. |
Raises: |
- Exception: Raises RunCommandException on error with optional error_message. |
+ Exception: Raises RunCommandException on error with optional error_message, |
+ but only if exit_code, and error_ok are both False. |
""" |
# Set default for variables. |
stdout = None |
@@ -70,27 +71,34 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
(GetCallerName(), cmd, cwd)) |
for retry_count in range(num_retries + 1): |
- try: |
- proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
- stdout=stdout, stderr=stderr) |
- (output, error) = proc.communicate(input) |
- if exit_code and retry_count == num_retries: |
- return proc.returncode |
- if proc.returncode == 0: |
- break |
+ # If it's not the first attempt, it's a retry |
+ if retry_count > 0 and print_cmd: |
+ Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % |
+ (GetCallerName(), cmd, cwd)) |
+ proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
+ stdout=stdout, stderr=stderr) |
+ (output, error) = proc.communicate(input) |
+ |
+ # if the command worked, don't retry any more. |
+ if proc.returncode == 0: |
+ break |
+ |
+ # If they asked for an exit_code, give it to them on success or failure |
+ if exit_code: |
+ return proc.returncode |
+ |
+ # If the command (and all retries) failed, handle error result |
+ if proc.returncode != 0: |
+ if error_ok: |
+ Warning('Command "%r" failed.\n' % (cmd) + |
+ (error_message or error or output or '')) |
+ else: |
raise RunCommandException('Command "%r" failed.\n' % (cmd) + |
(error_message or error or output or '')) |
- except RunCommandException as e: |
- if not error_ok and retry_count == num_retries: |
- raise e |
- else: |
- Warning(str(e)) |
- if print_cmd: |
- Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % |
- (GetCallerName(), cmd, cwd)) |
+ # return final result |
return output |