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 subprocess | 7 import subprocess |
8 import sys | 8 import sys |
9 | 9 |
10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() | 10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
(...skipping 16 matching lines...) Expand all Loading... | |
27 input -- input to pipe into this command through stdin. | 27 input -- input to pipe into this command through stdin. |
28 enter_chroot -- this command should be run from within the chroot. If set, | 28 enter_chroot -- this command should be run from within the chroot. If set, |
29 cwd must point to the scripts directory. | 29 cwd must point to the scripts directory. |
30 Raises: | 30 Raises: |
31 Exception: Raises generic exception on error with optional error_message. | 31 Exception: Raises generic exception on error with optional error_message. |
32 """ | 32 """ |
33 # Set default for variables. | 33 # Set default for variables. |
34 stdout = None | 34 stdout = None |
35 stderr = None | 35 stderr = None |
36 stdin = None | 36 stdin = None |
37 output = '' | |
37 | 38 |
38 # Modify defaults based on parameters. | 39 # Modify defaults based on parameters. |
39 if redirect_stdout: stdout = subprocess.PIPE | 40 if redirect_stdout: stdout = subprocess.PIPE |
40 if redirect_stderr: stderr = subprocess.PIPE | 41 if redirect_stderr: stderr = subprocess.PIPE |
41 if input: stdin = subprocess.PIPE | 42 if input: stdin = subprocess.PIPE |
42 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 43 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
43 | 44 |
44 # Print out the command before running. | 45 # Print out the command before running. |
45 if print_cmd: | 46 if print_cmd: |
46 Info('RunCommand: %s' % ' '.join(cmd)) | 47 Info('RunCommand: %s' % ' '.join(cmd)) |
47 | 48 |
48 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, | 49 try: |
49 stdout=stdout, stderr=stderr) | 50 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
50 (output, error) = proc.communicate(input) | 51 stdout=stdout, stderr=stderr) |
51 if exit_code: | 52 (output, error) = proc.communicate(input) |
52 return proc.returncode | 53 if exit_code: |
54 return proc.returncode | |
53 | 55 |
54 if not error_ok and proc.returncode: | 56 if not error_ok and proc.returncode: |
55 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + | 57 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + |
56 (error_message or error or output or '')) | 58 (error_message or error or output or '')) |
59 except Exception,e: | |
petkov
2010/08/31 16:27:19
space after ,
| |
60 if not error_ok: | |
61 raise | |
62 else: | |
63 Warning(str(e)) | |
57 | 64 |
58 return output | 65 return output |
59 | 66 |
60 | 67 |
61 class Color(object): | 68 class Color(object): |
62 """Conditionally wraps text in ANSI color escape sequences.""" | 69 """Conditionally wraps text in ANSI color escape sequences.""" |
63 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) | 70 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
64 BOLD = -1 | 71 BOLD = -1 |
65 COLOR_START = '\033[1;%dm' | 72 COLOR_START = '\033[1;%dm' |
66 BOLD_START = '\033[1m' | 73 BOLD_START = '\033[1m' |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 118 |
112 | 119 |
113 def Info(message): | 120 def Info(message): |
114 """Emits a blue informational message and continues execution. | 121 """Emits a blue informational message and continues execution. |
115 | 122 |
116 Keyword arguments: | 123 Keyword arguments: |
117 message: The message to be emitted. | 124 message: The message to be emitted. |
118 """ | 125 """ |
119 print >> sys.stderr, ( | 126 print >> sys.stderr, ( |
120 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) | 127 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) |
OLD | NEW |