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

Side by Side Diff: lib/cros_build_lib.py

Issue 5339006: Add RunCommandCaptureOutput as a helper method. It's like RunCommand, but you (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Tweaked return value. Created 10 years 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Common python commands used by various build scripts.""" 5 """Common python commands used by various build scripts."""
6 6
7 import inspect 7 import inspect
8 import os 8 import os
9 import subprocess 9 import subprocess
10 import sys 10 import sys
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 raise RunCommandException(e) 86 raise RunCommandException(e)
87 else: 87 else:
88 Warning(str(e)) 88 Warning(str(e))
89 if print_cmd: 89 if print_cmd:
90 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % 90 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' %
91 (GetCallerName(), cmd, cwd)) 91 (GetCallerName(), cmd, cwd))
92 92
93 return output 93 return output
94 94
95 95
96 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None,
97 enter_chroot=False,
98 combine_stdout_stderr=True):
99 """Runs a shell command. Differs from RunCommand, because it allows
100 you to run a command and capture the exit code, output, and stderr
101 all at the same time.
102
103 Arguments:
104 cmd: cmd to run. Should be input to subprocess.POpen. If a string,
105 converted to an array using split().
106 print_cmd: prints the command before running it.
107 cwd: the working directory to run this cmd.
108 input: input to pipe into this command through stdin.
109 enter_chroot: this command should be run from within the chroot. If set,
110 cwd must point to the scripts directory.
111 combine_stdout_stderr -- combine outputs together.
112
113 Returns:
114 Returns a tuple: (exit_code, stdout, stderr) (integer, string, string)
115 stderr is None if combine_stdout_stderr is True
116 """
117 # Set default for variables.
118 stdout = subprocess.PIPE
119 stderr = subprocess.PIPE
120 stdin = None
121
122 # Modify defaults based on parameters.
123 if input: stdin = subprocess.PIPE
124 if combine_stdout_stderr: stderr = subprocess.STDOUT
125
126 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd
127
128 # Print out the command before running.
129 if print_cmd:
130 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' %
131 (GetCallerName(), cmd, cwd))
132
133 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin,
134 stdout=stdout, stderr=stderr)
135 (output, error) = proc.communicate(input)
136
137 # Error is None if stdout, stderr are combined.
138 return proc.returncode, output, error
139
140
96 class Color(object): 141 class Color(object):
97 """Conditionally wraps text in ANSI color escape sequences.""" 142 """Conditionally wraps text in ANSI color escape sequences."""
98 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) 143 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
99 BOLD = -1 144 BOLD = -1
100 COLOR_START = '\033[1;%dm' 145 COLOR_START = '\033[1;%dm'
101 BOLD_START = '\033[1m' 146 BOLD_START = '\033[1m'
102 RESET = '\033[0m' 147 RESET = '\033[0m'
103 148
104 def __init__(self, enabled=True): 149 def __init__(self, enabled=True):
105 self._enabled = enabled 150 self._enabled = enabled
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 root_abs_path = os.path.abspath(root_path) 229 root_abs_path = os.path.abspath(root_path)
185 230
186 # Strip the repository root from the path and strip first /. 231 # Strip the repository root from the path and strip first /.
187 relative_path = path_abs_path.replace(root_abs_path, '')[1:] 232 relative_path = path_abs_path.replace(root_abs_path, '')[1:]
188 233
189 if relative_path == path_abs_path: 234 if relative_path == path_abs_path:
190 raise Exception('Error: path is outside your src tree, cannot reinterpret.') 235 raise Exception('Error: path is outside your src tree, cannot reinterpret.')
191 236
192 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) 237 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path)
193 return new_path 238 return new_path
194
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698