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 os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 | 10 |
11 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() | 11 _STDOUT_IS_TTY = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty() |
12 | 12 |
| 13 |
| 14 class CommandResult(object): |
| 15 """An object to store various attributes of a child process.""" |
| 16 |
| 17 def __init__(self): |
| 18 self.cmd = None |
| 19 self.error = None |
| 20 self.output = None |
| 21 self.returncode = None |
| 22 |
| 23 |
| 24 class RunCommandError(Exception): |
| 25 """Error caught in RunCommand() method.""" |
| 26 pass |
| 27 |
| 28 |
13 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, | 29 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
14 exit_code=False, redirect_stdout=False, redirect_stderr=False, | 30 exit_code=False, redirect_stdout=False, redirect_stderr=False, |
15 cwd=None, input=None, enter_chroot=False, shell=False): | 31 cwd=None, input=None, enter_chroot=False, shell=False): |
16 """Runs a command. | 32 """Runs a command. |
17 | 33 |
18 Keyword arguments: | 34 Keyword arguments: |
19 cmd - cmd to run. Should be input to subprocess.Popen. | 35 cmd - cmd to run. Should be input to subprocess.Popen. |
20 print_cmd -- prints the command before running it. | 36 print_cmd -- prints the command before running it. |
21 error_ok -- does not raise an exception on error. | 37 error_ok -- does not raise an exception on error. |
22 error_message -- prints out this message when an error occurrs. | 38 error_message -- prints out this message when an error occurrs. |
23 exit_code -- returns the return code of the shell command. | 39 exit_code -- returns the return code of the shell command. |
24 redirect_stdout -- returns the stdout. | 40 redirect_stdout -- returns the stdout. |
25 redirect_stderr -- holds stderr output until input is communicated. | 41 redirect_stderr -- holds stderr output until input is communicated. |
26 cwd -- the working directory to run this cmd. | 42 cwd -- the working directory to run this cmd. |
27 input -- input to pipe into this command through stdin. | 43 input -- input to pipe into this command through stdin. |
28 enter_chroot -- this command should be run from within the chroot. If set, | 44 enter_chroot -- this command should be run from within the chroot. If set, |
29 cwd must point to the scripts directory. | 45 cwd must point to the scripts directory. |
30 shell -- If shell is True, the specified command will be executed through th
e shell. | 46 shell -- If shell is True, the specified command will be executed through th
e shell. |
31 Raises: | 47 Raises: |
32 Exception: Raises generic exception on error with optional error_message. | 48 Exception: Raises generic exception on error with optional error_message. |
33 """ | 49 """ |
34 # Set default for variables. | 50 # Set default for variables. |
35 stdout = None | 51 stdout = None |
36 stderr = None | 52 stderr = None |
37 stdin = None | 53 stdin = None |
38 output = '' | 54 output = '' |
| 55 cmd_result = CommandResult() |
39 | 56 |
40 # Modify defaults based on parameters. | 57 # Modify defaults based on parameters. |
41 if redirect_stdout: stdout = subprocess.PIPE | 58 if redirect_stdout: stdout = subprocess.PIPE |
42 if redirect_stderr: stderr = subprocess.PIPE | 59 if redirect_stderr: stderr = subprocess.PIPE |
43 if input: stdin = subprocess.PIPE | 60 if input: stdin = subprocess.PIPE |
44 if isinstance(cmd, basestring): | 61 if isinstance(cmd, basestring): |
45 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd | 62 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd |
46 cmd_str = cmd | 63 cmd_str = cmd |
47 else: | 64 else: |
48 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
49 cmd_str = ' '.join(cmd) | 66 cmd_str = ' '.join(cmd) |
50 | 67 |
51 # Print out the command before running. | 68 # Print out the command before running. |
52 if print_cmd: | 69 if print_cmd: |
53 Info('RunCommand: %s' % cmd_str) | 70 Info('RunCommand: %s' % cmd_str) |
| 71 cmd_result.cmd = cmd_str |
54 | 72 |
55 try: | 73 try: |
56 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, | 74 proc = subprocess.Popen(cmd_str, cwd=cwd, stdin=stdin, |
57 stdout=stdout, stderr=stderr, | 75 stdout=stdout, stderr=stderr, |
58 shell=shell) | 76 shell=shell) |
59 (output, error) = proc.communicate(input) | 77 (cmd_result.output, cmd_result.error) = proc.communicate(input) |
60 if exit_code: | 78 if exit_code: |
61 return proc.returncode | 79 cmd_result.returncode = proc.returncode |
62 | 80 |
63 if not error_ok and proc.returncode: | 81 if not error_ok and proc.returncode: |
64 raise Exception('Command "%s" failed.\n' % cmd_str + | 82 msg = ('Command "%s" failed.\n' % cmd_str + |
65 (error_message or error or output or '')) | 83 (error_message or cmd_result.error or cmd_result.output or '')) |
| 84 raise RunCommandError(msg) |
66 except Exception,e: | 85 except Exception,e: |
67 if not error_ok: | 86 if not error_ok: |
68 raise | 87 raise |
69 else: | 88 else: |
70 Warning(str(e)) | 89 Warning(str(e)) |
71 | 90 |
72 return output | 91 return cmd_result |
73 | 92 |
74 | 93 |
75 class Color(object): | 94 class Color(object): |
76 """Conditionally wraps text in ANSI color escape sequences.""" | 95 """Conditionally wraps text in ANSI color escape sequences.""" |
77 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) | 96 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
78 BOLD = -1 | 97 BOLD = -1 |
79 COLOR_START = '\033[1;%dm' | 98 COLOR_START = '\033[1;%dm' |
80 BOLD_START = '\033[1m' | 99 BOLD_START = '\033[1m' |
81 RESET = '\033[0m' | 100 RESET = '\033[0m' |
82 | 101 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 while directories: | 168 while directories: |
150 directory = directories.pop() | 169 directory = directories.pop() |
151 for name in os.listdir(directory): | 170 for name in os.listdir(directory): |
152 fullpath = os.path.join(directory, name) | 171 fullpath = os.path.join(directory, name) |
153 if os.path.isfile(fullpath): | 172 if os.path.isfile(fullpath): |
154 files_list.append(fullpath) | 173 files_list.append(fullpath) |
155 elif os.path.isdir(fullpath): | 174 elif os.path.isdir(fullpath): |
156 directories.append(fullpath) | 175 directories.append(fullpath) |
157 | 176 |
158 return files_list | 177 return files_list |
OLD | NEW |