Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Unified Diff: lib/cros_build_lib.py

Issue 4864001: Change _ArchiveTestResults to upload to Google Storage (Closed) Base URL: http://git.chromium.org/git/crosutils.git@master
Patch Set: Add num retries to RunCommand Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« bin/cbuildbot.py ('K') | « bin/cbuildbot.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/cros_build_lib.py
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py
index 509871c6eedbe06ad9b9e160930e4eae4a3f2fa5..e936ff68370eadbdbf3dae9e4a78a8a38291d633 100644
--- a/lib/cros_build_lib.py
+++ b/lib/cros_build_lib.py
@@ -21,22 +21,28 @@ def GetCallerName():
def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
exit_code=False, redirect_stdout=False, redirect_stderr=False,
- cwd=None, input=None, enter_chroot=False):
+ cwd=None, input=None, enter_chroot=False, num_retries=0):
"""Runs a shell command.
- Keyword arguments:
- cmd - cmd to run. Should be input to subprocess.POpen. If a string,
+ Arguments:
+ cmd: cmd to run. Should be input to subprocess.POpen. If a string,
converted to an array using split().
- print_cmd -- prints the command before running it.
- error_ok -- does not raise an exception on error.
- error_message -- prints out this message when an error occurrs.
- exit_code -- returns the return code of the shell command.
- redirect_stdout -- returns the stdout.
- redirect_stderr -- holds stderr output until input is communicated.
- cwd -- the working directory to run this cmd.
- input -- input to pipe into this command through stdin.
- enter_chroot -- this command should be run from within the chroot. If set,
+ print_cmd: prints the command before running it.
+ error_ok: does not raise an exception on error.
+ error_message: prints out this message when an error occurrs.
+ exit_code: returns the return code of the shell command.
+ redirect_stdout: returns the stdout.
+ redirect_stderr: holds stderr output until input is communicated.
+ cwd: the working directory to run this cmd.
+ input: input to pipe into this command through stdin.
+ enter_chroot: this command should be run from within the chroot. If set,
cwd must point to the scripts directory.
+ num_retries: the number of retries to perform before dying
+
+ Returns:
+ If exit_code is True, returns the return code of the shell command.
+ Else returns the output of the shell command.
+
Raises:
Exception: Raises generic exception on error with optional error_message.
"""
@@ -57,21 +63,28 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
Info('PROGRAM(%s) -> RunCommand: %r in dir %s' %
(GetCallerName(), cmd, cwd))
- try:
- proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
- stdout=stdout, stderr=stderr)
- (output, error) = proc.communicate(input)
- if exit_code:
- return proc.returncode
-
- if not error_ok and proc.returncode:
- raise Exception('Command "%r" failed.\n' % (cmd) +
- (error_message or error or output or ''))
- except Exception, e:
- if not error_ok:
- raise
- else:
- Warning(str(e))
+ 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
+ else:
scottz-goog 2010/11/12 23:37:13 No need for the else here.
thieule 2010/11/13 00:21:32 Done.
+ raise Exception('Command "%r" failed.\n' % (cmd) +
+ (error_message or error or output or ''))
+ except Exception, e:
scottz-goog 2010/11/12 23:37:13 Ugg do we really not raise a RunCommand Exception?
thieule 2010/11/13 00:21:32 Done.
+ if not error_ok and retry_count == num_retries:
+ raise
+ else:
+ Warning(str(e))
+
+ if print_cmd:
sosa 2010/11/12 23:33:09 Little easier to read if this is in the else: of y
thieule 2010/11/13 00:21:32 Done.
+ Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
+ (GetCallerName(), cmd, cwd))
return output
« bin/cbuildbot.py ('K') | « bin/cbuildbot.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698