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

Unified Diff: chromite/lib/cros_build_lib.py

Issue 3307022: Issue 6600: RunCommand should return an object with various attributes (Closed) Base URL: http://git.chromium.org/git/crosutils.git
Patch Set: add copyright notice Created 10 years, 3 months 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
« no previous file with comments | « no previous file | chromite/lib/cros_build_lib_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromite/lib/cros_build_lib.py
diff --git a/chromite/lib/cros_build_lib.py b/chromite/lib/cros_build_lib.py
index e9470685e28fa6574cf96e259900758c5cdf0bd0..e382445087132d3ad312d4db672e9611f369dc1b 100644
--- a/chromite/lib/cros_build_lib.py
+++ b/chromite/lib/cros_build_lib.py
@@ -10,6 +10,22 @@ import sys
_STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
+
+class CommandResult(object):
+ """An object to store various attributes of a child process."""
+
+ def __init__(self):
+ self.cmd = None
+ self.error = None
+ self.output = None
+ self.returncode = None
+
+
+class RunCommandError(Exception):
+ """Error caught in RunCommand() method."""
+ pass
+
+
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, shell=False):
@@ -36,6 +52,7 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
stderr = None
stdin = None
output = ''
+ cmd_result = CommandResult()
# Modify defaults based on parameters.
if redirect_stdout: stdout = subprocess.PIPE
@@ -51,25 +68,27 @@ def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None,
# Print out the command before running.
if print_cmd:
Info('RunCommand: %s' % cmd_str)
+ cmd_result.cmd = cmd_str
try:
- proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
+ proc = subprocess.Popen(cmd_str, cwd=cwd, stdin=stdin,
stdout=stdout, stderr=stderr,
shell=shell)
- (output, error) = proc.communicate(input)
+ (cmd_result.output, cmd_result.error) = proc.communicate(input)
if exit_code:
- return proc.returncode
+ cmd_result.returncode = proc.returncode
if not error_ok and proc.returncode:
- raise Exception('Command "%s" failed.\n' % cmd_str +
- (error_message or error or output or ''))
+ msg = ('Command "%s" failed.\n' % cmd_str +
+ (error_message or cmd_result.error or cmd_result.output or ''))
+ raise RunCommandError(msg)
except Exception,e:
if not error_ok:
raise
else:
Warning(str(e))
- return output
+ return cmd_result
class Color(object):
« no previous file with comments | « no previous file | chromite/lib/cros_build_lib_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698