OLD | NEW |
---|---|
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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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, | 96 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, |
97 enter_chroot=False, | 97 enter_chroot=False, |
98 combine_stdout_stderr=True): | 98 combine_stdout_stderr=True, |
99 verbose=False): | |
99 """Runs a shell command. Differs from RunCommand, because it allows | 100 """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 you to run a command and capture the exit code, output, and stderr |
101 all at the same time. | 102 all at the same time. |
102 | 103 |
103 Arguments: | 104 Arguments: |
104 cmd: cmd to run. Should be input to subprocess.POpen. If a string, | 105 cmd: cmd to run. Should be input to subprocess.POpen. If a string, |
105 converted to an array using split(). | 106 converted to an array using split(). |
106 print_cmd: prints the command before running it. | 107 print_cmd: prints the command before running it. |
107 cwd: the working directory to run this cmd. | 108 cwd: the working directory to run this cmd. |
108 input: input to pipe into this command through stdin. | 109 input: input to pipe into this command through stdin. |
109 enter_chroot: this command should be run from within the chroot. If set, | 110 enter_chroot: this command should be run from within the chroot. If set, |
110 cwd must point to the scripts directory. | 111 cwd must point to the scripts directory. |
111 combine_stdout_stderr -- combine outputs together. | 112 combine_stdout_stderr -- combine outputs together. |
113 verbose -- also echo cmd.stdout and cmd.stderr to stdout and stderr | |
112 | 114 |
113 Returns: | 115 Returns: |
114 Returns a tuple: (exit_code, stdout, stderr) (integer, string, string) | 116 Returns a tuple: (exit_code, stdout, stderr) (integer, string, string) |
115 stderr is None if combine_stdout_stderr is True | 117 stderr is None if combine_stdout_stderr is True |
116 """ | 118 """ |
117 # Set default for variables. | 119 # Set default for variables. |
118 stdout = subprocess.PIPE | 120 stdout = subprocess.PIPE |
119 stderr = subprocess.PIPE | 121 stderr = subprocess.PIPE |
120 stdin = None | 122 stdin = None |
121 | 123 |
122 # Modify defaults based on parameters. | 124 # Modify defaults based on parameters. |
123 if input: stdin = subprocess.PIPE | 125 if input: stdin = subprocess.PIPE |
124 if combine_stdout_stderr: stderr = subprocess.STDOUT | 126 if combine_stdout_stderr: stderr = subprocess.STDOUT |
125 | 127 |
126 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 128 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
127 | 129 |
128 # Print out the command before running. | 130 # Print out the command before running. |
129 if print_cmd: | 131 if print_cmd: |
130 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % | 132 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % |
131 (GetCallerName(), cmd, cwd)) | 133 (GetCallerName(), cmd, cwd)) |
132 | 134 |
133 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, | 135 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
134 stdout=stdout, stderr=stderr) | 136 stdout=stdout, stderr=stderr) |
135 (output, error) = proc.communicate(input) | 137 output, error = proc.communicate(input) |
sosa
2011/01/20 03:40:55
I'm not sure this completely helps us because if t
| |
138 | |
139 if verbose: | |
140 if output: sys.stdout.write(output) | |
141 if error: sys.stderr.write(error) | |
136 | 142 |
137 # Error is None if stdout, stderr are combined. | 143 # Error is None if stdout, stderr are combined. |
138 return proc.returncode, output, error | 144 return proc.returncode, output, error |
139 | 145 |
140 | 146 |
141 class Color(object): | 147 class Color(object): |
142 """Conditionally wraps text in ANSI color escape sequences.""" | 148 """Conditionally wraps text in ANSI color escape sequences.""" |
143 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) | 149 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
144 BOLD = -1 | 150 BOLD = -1 |
145 COLOR_START = '\033[1;%dm' | 151 COLOR_START = '\033[1;%dm' |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 root_abs_path = os.path.abspath(root_path) | 238 root_abs_path = os.path.abspath(root_path) |
233 | 239 |
234 # Strip the repository root from the path and strip first /. | 240 # Strip the repository root from the path and strip first /. |
235 relative_path = path_abs_path.replace(root_abs_path, '')[1:] | 241 relative_path = path_abs_path.replace(root_abs_path, '')[1:] |
236 | 242 |
237 if relative_path == path_abs_path: | 243 if relative_path == path_abs_path: |
238 raise Exception('Error: path is outside your src tree, cannot reinterpret.') | 244 raise Exception('Error: path is outside your src tree, cannot reinterpret.') |
239 | 245 |
240 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) | 246 new_path = os.path.join('/home', os.getenv('USER'), 'trunk', relative_path) |
241 return new_path | 247 return new_path |
OLD | NEW |