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 re | 9 import re |
10 import subprocess | 10 import subprocess |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 if redirect_stderr: stderr = subprocess.PIPE | 63 if redirect_stderr: stderr = subprocess.PIPE |
64 if input: stdin = subprocess.PIPE | 64 if input: stdin = subprocess.PIPE |
65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 65 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
66 | 66 |
67 # Print out the command before running. | 67 # Print out the command before running. |
68 if print_cmd: | 68 if print_cmd: |
69 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % | 69 Info('PROGRAM(%s) -> RunCommand: %r in dir %s' % |
70 (GetCallerName(), cmd, cwd)) | 70 (GetCallerName(), cmd, cwd)) |
71 | 71 |
72 for retry_count in range(num_retries + 1): | 72 for retry_count in range(num_retries + 1): |
| 73 try: |
| 74 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, |
| 75 stdout=stdout, stderr=stderr) |
| 76 (output, error) = proc.communicate(input) |
| 77 if exit_code and retry_count == num_retries: |
| 78 return proc.returncode |
73 | 79 |
74 # If it's not the first attempt, it's a retry | 80 if proc.returncode == 0: |
75 if retry_count > 0 and print_cmd: | 81 break |
76 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % | |
77 (GetCallerName(), cmd, cwd)) | |
78 | |
79 proc = subprocess.Popen(cmd, cwd=cwd, stdin=stdin, | |
80 stdout=stdout, stderr=stderr) | |
81 (output, error) = proc.communicate(input) | |
82 | 82 |
83 # if the command worked, don't retry any more. | |
84 if proc.returncode == 0: | |
85 break | |
86 | |
87 # If the command (and all retries) failed, handle error result | |
88 if proc.returncode != 0: | |
89 if error_ok: | |
90 if print_cmd: | |
91 Warning('Command "%r" failed.\n' % (cmd) + | |
92 (error_message or error or output or '')) | |
93 else: | |
94 raise RunCommandException('Command "%r" failed.\n' % (cmd) + | 83 raise RunCommandException('Command "%r" failed.\n' % (cmd) + |
95 (error_message or error or output or '')) | 84 (error_message or error or output or '')) |
| 85 except RunCommandException as e: |
| 86 if not error_ok and retry_count == num_retries: |
| 87 raise e |
| 88 else: |
| 89 Warning(str(e)) |
| 90 if print_cmd: |
| 91 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % |
| 92 (GetCallerName(), cmd, cwd)) |
96 | 93 |
97 # return final result | 94 return output |
98 if exit_code: | |
99 return proc.returncode | |
100 else: | |
101 return output | |
102 | 95 |
103 | 96 |
104 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, | 97 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, |
105 enter_chroot=False, | 98 enter_chroot=False, |
106 combine_stdout_stderr=True, | 99 combine_stdout_stderr=True, |
107 verbose=False): | 100 verbose=False): |
108 """Runs a shell command. Differs from RunCommand, because it allows | 101 """Runs a shell command. Differs from RunCommand, because it allows |
109 you to run a command and capture the exit code, output, and stderr | 102 you to run a command and capture the exit code, output, and stderr |
110 all at the same time. | 103 all at the same time. |
111 | 104 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 either via a VM or remote machine on the same network. | 257 either via a VM or remote machine on the same network. |
265 """ | 258 """ |
266 ifconfig_output = RunCommand(['/sbin/ifconfig', device], | 259 ifconfig_output = RunCommand(['/sbin/ifconfig', device], |
267 redirect_stdout=True, print_cmd=False) | 260 redirect_stdout=True, print_cmd=False) |
268 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) | 261 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) |
269 if match: | 262 if match: |
270 return match.group(1) | 263 return match.group(1) |
271 else: | 264 else: |
272 Warning('Failed to find ip address in %s' % ifconfig_output) | 265 Warning('Failed to find ip address in %s' % ifconfig_output) |
273 return None | 266 return None |
OLD | NEW |