| 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 signal | 10 import signal |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 | 28 |
| 29 class RunCommandError(Exception): | 29 class RunCommandError(Exception): |
| 30 """Error caught in RunCommand() method.""" | 30 """Error caught in RunCommand() method.""" |
| 31 pass | 31 pass |
| 32 | 32 |
| 33 | 33 |
| 34 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, | 34 def RunCommand(cmd, print_cmd=True, error_ok=False, error_message=None, |
| 35 exit_code=False, redirect_stdout=False, redirect_stderr=False, | 35 exit_code=False, redirect_stdout=False, redirect_stderr=False, |
| 36 cwd=None, input=None, enter_chroot=False, shell=False, | 36 cwd=None, input=None, enter_chroot=False, shell=False, |
| 37 env=None, ignore_sigint=False): | 37 env=None, ignore_sigint=False, combine_stdout_stderr=False): |
| 38 """Runs a command. | 38 """Runs a command. |
| 39 | 39 |
| 40 Args: | 40 Args: |
| 41 cmd: cmd to run. Should be input to subprocess.Popen. | 41 cmd: cmd to run. Should be input to subprocess.Popen. |
| 42 print_cmd: prints the command before running it. | 42 print_cmd: prints the command before running it. |
| 43 error_ok: does not raise an exception on error. | 43 error_ok: does not raise an exception on error. |
| 44 error_message: prints out this message when an error occurrs. | 44 error_message: prints out this message when an error occurrs. |
| 45 exit_code: returns the return code of the shell command. | 45 exit_code: returns the return code of the shell command. |
| 46 redirect_stdout: returns the stdout. | 46 redirect_stdout: returns the stdout. |
| 47 redirect_stderr: holds stderr output until input is communicated. | 47 redirect_stderr: holds stderr output until input is communicated. |
| 48 cwd: the working directory to run this cmd. | 48 cwd: the working directory to run this cmd. |
| 49 input: input to pipe into this command through stdin. | 49 input: input to pipe into this command through stdin. |
| 50 enter_chroot: this command should be run from within the chroot. If set, | 50 enter_chroot: this command should be run from within the chroot. If set, |
| 51 cwd must point to the scripts directory. | 51 cwd must point to the scripts directory. |
| 52 shell: If shell is True, the specified command will be executed through | 52 shell: If shell is True, the specified command will be executed through |
| 53 the shell. | 53 the shell. |
| 54 env: If non-None, this is the environment for the new process. | 54 env: If non-None, this is the environment for the new process. |
| 55 ignore_sigint: If True, we'll ignore signal.SIGINT before calling the | 55 ignore_sigint: If True, we'll ignore signal.SIGINT before calling the |
| 56 child. This is the desired behavior if we know our child will handle | 56 child. This is the desired behavior if we know our child will handle |
| 57 Ctrl-C. If we don't do this, I think we and the child will both get | 57 Ctrl-C. If we don't do this, I think we and the child will both get |
| 58 Ctrl-C at the same time, which means we'll forcefully kill the child. | 58 Ctrl-C at the same time, which means we'll forcefully kill the child. |
| 59 combine_stdout_stderr: Combines stdout and stdin streams into stdout. |
| 59 | 60 |
| 60 Returns: | 61 Returns: |
| 61 A CommandResult object. | 62 A CommandResult object. |
| 62 | 63 |
| 63 Raises: | 64 Raises: |
| 64 Exception: Raises generic exception on error with optional error_message. | 65 Exception: Raises generic exception on error with optional error_message. |
| 65 """ | 66 """ |
| 66 # Set default for variables. | 67 # Set default for variables. |
| 67 stdout = None | 68 stdout = None |
| 68 stderr = None | 69 stderr = None |
| 69 stdin = None | 70 stdin = None |
| 70 cmd_result = CommandResult() | 71 cmd_result = CommandResult() |
| 71 | 72 |
| 72 # Modify defaults based on parameters. | 73 # Modify defaults based on parameters. |
| 73 if redirect_stdout: stdout = subprocess.PIPE | 74 if redirect_stdout: stdout = subprocess.PIPE |
| 74 if redirect_stderr: stderr = subprocess.PIPE | 75 if redirect_stderr: stderr = subprocess.PIPE |
| 76 if combine_stdout_stderr: stderr = subprocess.STDOUT |
| 75 # TODO(sosa): gpylint complains about redefining built-in 'input'. | 77 # TODO(sosa): gpylint complains about redefining built-in 'input'. |
| 76 # Can we rename this variable? | 78 # Can we rename this variable? |
| 77 if input: stdin = subprocess.PIPE | 79 if input: stdin = subprocess.PIPE |
| 78 if isinstance(cmd, basestring): | 80 if isinstance(cmd, basestring): |
| 79 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd | 81 if enter_chroot: cmd = './enter_chroot.sh -- ' + cmd |
| 80 cmd_str = cmd | 82 cmd_str = cmd |
| 81 else: | 83 else: |
| 82 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd | 84 if enter_chroot: cmd = ['./enter_chroot.sh', '--'] + cmd |
| 83 cmd_str = ' '.join(cmd) | 85 cmd_str = ' '.join(cmd) |
| 84 | 86 |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 except RunCommandException as e: | 348 except RunCommandException as e: |
| 347 if not error_ok and retry_count == num_retries: | 349 if not error_ok and retry_count == num_retries: |
| 348 raise e | 350 raise e |
| 349 else: | 351 else: |
| 350 Warning(str(e)) | 352 Warning(str(e)) |
| 351 if print_cmd: | 353 if print_cmd: |
| 352 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % | 354 Info('PROGRAM(%s) -> RunCommand: retrying %r in dir %s' % |
| 353 (GetCallerName(), cmd, cwd)) | 355 (GetCallerName(), cmd, cwd)) |
| 354 | 356 |
| 355 return output | 357 return output |
| OLD | NEW |