| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium 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 """A wrapper for subprocess to make calling shell commands easier.""" | 5 """A wrapper for subprocess to make calling shell commands easier.""" |
| 6 | 6 |
| 7 import os | 7 import os |
| 8 import logging | 8 import logging |
| 9 import subprocess | 9 import subprocess |
| 10 | 10 |
| 11 import constants | 11 import constants |
| 12 | 12 |
| 13 |
| 13 def RunCmd(args, cwd=None): | 14 def RunCmd(args, cwd=None): |
| 14 """Opens a subprocess to execute a program and returns its return value. | 15 """Opens a subprocess to execute a program and returns its return value. |
| 15 | 16 |
| 16 Args: | 17 Args: |
| 17 args: A string or a sequence of program arguments. The program to execute is | 18 args: A string or a sequence of program arguments. The program to execute is |
| 18 the string or the first item in the args sequence. | 19 the string or the first item in the args sequence. |
| 19 cwd: If not None, the subprocess's current directory will be changed to | 20 cwd: If not None, the subprocess's current directory will be changed to |
| 20 |cwd| before it's executed. | 21 |cwd| before it's executed. |
| 21 | 22 |
| 22 Returns: | 23 Returns: |
| 23 Return code from the command execution. | 24 Return code from the command execution. |
| 24 """ | 25 """ |
| 25 logging.info(str(args) + ' ' + (cwd or '')) | 26 logging.info(str(args) + ' ' + (cwd or '')) |
| 26 p = subprocess.Popen(args=args, cwd=cwd) | 27 return subprocess.call(args, cwd=cwd) |
| 27 return p.wait() | |
| 28 | 28 |
| 29 | 29 |
| 30 def GetCmdOutput(args, cwd=None, shell=False): | 30 def GetCmdOutput(args, cwd=None, shell=False): |
| 31 """Open a subprocess to execute a program and returns its output. | 31 """Open a subprocess to execute a program and returns its output. |
| 32 | 32 |
| 33 Args: | 33 Args: |
| 34 args: A string or a sequence of program arguments. The program to execute is | 34 args: A string or a sequence of program arguments. The program to execute is |
| 35 the string or the first item in the args sequence. | 35 the string or the first item in the args sequence. |
| 36 cwd: If not None, the subprocess's current directory will be changed to | 36 cwd: If not None, the subprocess's current directory will be changed to |
| 37 |cwd| before it's executed. | 37 |cwd| before it's executed. |
| 38 shell: Whether to execute args as a shell command. | 38 shell: Whether to execute args as a shell command. |
| 39 | 39 |
| 40 Returns: | 40 Returns: |
| 41 Captures and returns the command's stdout. | 41 Captures and returns the command's stdout. |
| 42 Prints the command's stderr to logger (which defaults to stdout). | 42 Prints the command's stderr to logger (which defaults to stdout). |
| 43 """ | 43 """ |
| 44 (_, output) = GetCmdStatusAndOutput(args, cwd, shell) | 44 (_, output) = GetCmdStatusAndOutput(args, cwd, shell) |
| 45 return output | 45 return output |
| 46 | 46 |
| 47 |
| 47 def GetCmdStatusAndOutput(args, cwd=None, shell=False): | 48 def GetCmdStatusAndOutput(args, cwd=None, shell=False): |
| 48 """Executes a subprocess and returns its exit code and output. | 49 """Executes a subprocess and returns its exit code and output. |
| 49 | 50 |
| 50 Args: | 51 Args: |
| 51 args: A string or a sequence of program arguments. The program to execute is | 52 args: A string or a sequence of program arguments. The program to execute is |
| 52 the string or the first item in the args sequence. | 53 the string or the first item in the args sequence. |
| 53 cwd: If not None, the subprocess's current directory will be changed to | 54 cwd: If not None, the subprocess's current directory will be changed to |
| 54 |cwd| before it's executed. | 55 |cwd| before it's executed. |
| 55 shell: Whether to execute args as a shell command. | 56 shell: Whether to execute args as a shell command. |
| 56 | 57 |
| 57 Returns: | 58 Returns: |
| 58 The tuple (exit code, output). | 59 The tuple (exit code, output). |
| 59 """ | 60 """ |
| 60 logging.info(str(args) + ' ' + (cwd or '')) | 61 logging.info(str(args) + ' ' + (cwd or '')) |
| 61 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, | 62 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, |
| 62 stderr=subprocess.PIPE, shell=shell) | 63 stderr=subprocess.PIPE, shell=shell) |
| 63 stdout, stderr = p.communicate() | 64 stdout, stderr = p.communicate() |
| 64 exit_code = p.returncode | 65 exit_code = p.returncode |
| 65 if stderr: | 66 if stderr: |
| 66 logging.critical(stderr) | 67 logging.critical(stderr) |
| 67 logging.info(stdout[:4096]) # Truncate output longer than 4k. | 68 logging.info(stdout[:4096]) # Truncate output longer than 4k. |
| 68 return (exit_code, stdout) | 69 return (exit_code, stdout) |
| 69 | 70 |
| 71 |
| 70 class OutDirectory(object): | 72 class OutDirectory(object): |
| 71 _out_directory = os.path.join(constants.CHROME_DIR, 'out') | 73 _out_directory = os.path.join(constants.CHROME_DIR, 'out') |
| 72 @staticmethod | 74 @staticmethod |
| 73 def set(out_directory): | 75 def set(out_directory): |
| 74 OutDirectory._out_directory = out_directory | 76 OutDirectory._out_directory = out_directory |
| 75 @staticmethod | 77 @staticmethod |
| 76 def get(): | 78 def get(): |
| 77 return OutDirectory._out_directory | 79 return OutDirectory._out_directory |
| OLD | NEW |