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 |
| 8 import os |
7 import subprocess | 9 import subprocess |
8 import sys | 10 import sys |
9 | 11 |
10 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() | 12 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
11 | 13 |
| 14 # TODO(sosa): Move logging to logging module. |
| 15 |
| 16 def GetCallerName(): |
| 17 """Returns the name of the calling module with __main__.""" |
| 18 top_frame = inspect.stack()[-1][0] |
| 19 return os.path.basename(top_frame.f_code.co_filename) |
| 20 |
| 21 |
12 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, | 22 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
13 exit_code=False, redirect_stdout=False, redirect_stderr=False, | 23 exit_code=False, redirect_stdout=False, redirect_stderr=False, |
14 cwd=None, input=None, enter_chroot=False): | 24 cwd=None, input=None, enter_chroot=False): |
15 """Runs a shell command. | 25 """Runs a shell command. |
16 | 26 |
17 Keyword arguments: | 27 Keyword arguments: |
18 cmd - cmd to run. Should be input to subprocess.POpen. If a string, | 28 cmd - cmd to run. Should be input to subprocess.POpen. If a string, |
19 converted to an array using split(). | 29 converted to an array using split(). |
20 print_cmd -- prints the command before running it. | 30 print_cmd -- prints the command before running it. |
21 error_ok -- does not raise an exception on error. | 31 error_ok -- does not raise an exception on error. |
(...skipping 15 matching lines...) Expand all Loading... |
37 output = '' | 47 output = '' |
38 | 48 |
39 # Modify defaults based on parameters. | 49 # Modify defaults based on parameters. |
40 if redirect_stdout: stdout = subprocess.PIPE | 50 if redirect_stdout: stdout = subprocess.PIPE |
41 if redirect_stderr: stderr = subprocess.PIPE | 51 if redirect_stderr: stderr = subprocess.PIPE |
42 if input: stdin = subprocess.PIPE | 52 if input: stdin = subprocess.PIPE |
43 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 53 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
44 | 54 |
45 # Print out the command before running. | 55 # Print out the command before running. |
46 if print_cmd: | 56 if print_cmd: |
47 Info('RunCommand: %s' % ' '.join(cmd)) | 57 Info('PROGRAM(%s) -> RunCommand: %s in dir %s' % |
| 58 (GetCallerName(), ' '.join(cmd), cwd)) |
48 | 59 |
49 try: | 60 try: |
50 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, | 61 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
51 stdout=stdout, stderr=stderr) | 62 stdout=stdout, stderr=stderr) |
52 (output, error) = proc.communicate(input) | 63 (output, error) = proc.communicate(input) |
53 if exit_code: | 64 if exit_code: |
54 return proc.returncode | 65 return proc.returncode |
55 | 66 |
56 if not error_ok and proc.returncode: | 67 if not error_ok and proc.returncode: |
57 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + | 68 raise Exception('Command "%s" failed.\n' % (' '.join(cmd)) + |
58 (error_message or error or output or '')) | 69 (error_message or error or output or '')) |
59 except Exception,e: | 70 except Exception, e: |
60 if not error_ok: | 71 if not error_ok: |
61 raise | 72 raise |
62 else: | 73 else: |
63 Warning(str(e)) | 74 Warning(str(e)) |
64 | 75 |
65 return output | 76 return output |
66 | 77 |
67 | 78 |
68 class Color(object): | 79 class Color(object): |
69 """Conditionally wraps text in ANSI color escape sequences.""" | 80 """Conditionally wraps text in ANSI color escape sequences.""" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 129 |
119 | 130 |
120 def Info(message): | 131 def Info(message): |
121 """Emits a blue informational message and continues execution. | 132 """Emits a blue informational message and continues execution. |
122 | 133 |
123 Keyword arguments: | 134 Keyword arguments: |
124 message: The message to be emitted. | 135 message: The message to be emitted. |
125 """ | 136 """ |
126 print >> sys.stderr, ( | 137 print >> sys.stderr, ( |
127 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) | 138 Color(_STDOUT_IS_TTY).Color(Color.BLUE, '\nINFO: ' + message)) |
OLD | NEW |