Chromium Code Reviews| 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 | |
| 79 | 73 |
| 80 if proc.returncode == 0: | 74 # If it's not the first attempt, it's a retry |
| 81 break | 75 if retry_count > 0 and print_cmd: |
| 76 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % | |
|
sosa
2011/02/24 00:23:36
Too much indention
| |
| 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: | |
| 83 raise RunCommandException('Command "%r" failed.\n' % (cmd) + | 94 raise RunCommandException('Command "%r" failed.\n' % (cmd) + |
| 84 (error_message or error or output or '')) | 95 (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)) | |
| 93 | 96 |
| 94 return output | 97 # return final result |
| 98 if exit_code: | |
| 99 return proc.returncode | |
| 100 else: | |
| 101 return output | |
| 95 | 102 |
| 96 | 103 |
| 97 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, | 104 def RunCommandCaptureOutput(cmd, print_cmd=True, cwd=None, input=None, |
| 98 enter_chroot=False, | 105 enter_chroot=False, |
| 99 combine_stdout_stderr=True, | 106 combine_stdout_stderr=True, |
| 100 verbose=False): | 107 verbose=False): |
| 101 """Runs a shell command. Differs from RunCommand, because it allows | 108 """Runs a shell command. Differs from RunCommand, because it allows |
| 102 you to run a command and capture the exit code, output, and stderr | 109 you to run a command and capture the exit code, output, and stderr |
| 103 all at the same time. | 110 all at the same time. |
| 104 | 111 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 either via a VM or remote machine on the same network. | 264 either via a VM or remote machine on the same network. |
| 258 """ | 265 """ |
| 259 ifconfig_output = RunCommand(['/sbin/ifconfig', device], | 266 ifconfig_output = RunCommand(['/sbin/ifconfig', device], |
| 260 redirect_stdout=True, print_cmd=False) | 267 redirect_stdout=True, print_cmd=False) |
| 261 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) | 268 match = re.search('.*inet addr:(\d+\.\d+\.\d+\.\d+).*', ifconfig_output) |
| 262 if match: | 269 if match: |
| 263 return match.group(1) | 270 return match.group(1) |
| 264 else: | 271 else: |
| 265 Warning('Failed to find ip address in %s' % ifconfig_output) | 272 Warning('Failed to find ip address in %s' % ifconfig_output) |
| 266 return None | 273 return None |
| OLD | NEW |