OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 #!/usr/bin/python |
| 2 # |
| 3 # 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 5 # found in the LICENSE file. |
4 | 6 |
| 7 """A wrapper for subprocess to make calling shell commands easier.""" |
| 8 |
5 | 9 |
6 import logging | 10 import logging |
7 import subprocess | 11 import subprocess |
8 | 12 |
9 | 13 |
10 def RunCmd(args, cwd=None): | 14 def RunCmd(args, cwd=None): |
11 """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. |
12 | 16 |
13 Args: | 17 Args: |
14 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 |
15 the string or the first item in the args sequence. | 19 the string or the first item in the args sequence. |
16 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 |
17 |cwd| before it's executed. | 21 |cwd| before it's executed. |
| 22 |
| 23 Returns: |
| 24 Return code from the command execution. |
18 """ | 25 """ |
19 logging.info(str(args) + ' ' + (cwd or '')) | 26 logging.info(str(args) + ' ' + (cwd or '')) |
20 p = subprocess.Popen(args=args, cwd=cwd) | 27 p = subprocess.Popen(args=args, cwd=cwd) |
21 return p.wait() | 28 return p.wait() |
22 | 29 |
23 | 30 |
24 def GetCmdOutput(args, cwd=None): | 31 def GetCmdOutput(args, cwd=None, shell=False): |
25 """Open a subprocess to execute a program and returns its output. | 32 """Open a subprocess to execute a program and returns its output. |
26 | 33 |
27 Args: | 34 Args: |
28 args: A string or a sequence of program arguments. The program to execute is | 35 args: A string or a sequence of program arguments. The program to execute is |
29 the string or the first item in the args sequence. | 36 the string or the first item in the args sequence. |
30 cwd: If not None, the subprocess's current directory will be changed to | 37 cwd: If not None, the subprocess's current directory will be changed to |
31 |cwd| before it's executed. | 38 |cwd| before it's executed. |
| 39 shell: Whether to execute args as a shell command. |
| 40 |
| 41 Returns: |
| 42 Captures and returns the command's stdout. |
| 43 Prints the command's stderr to logger (which defaults to stdout). |
32 """ | 44 """ |
33 logging.info(str(args) + ' ' + (cwd or '')) | 45 logging.info(str(args) + ' ' + (cwd or '')) |
34 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, | 46 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, |
35 stderr=subprocess.PIPE) | 47 stderr=subprocess.PIPE, shell=shell) |
36 stdout, stderr = p.communicate() | 48 stdout, stderr = p.communicate() |
37 if stderr: | 49 if stderr: |
38 logging.critical(stderr) | 50 logging.critical(stderr) |
39 logging.info(stdout[:4096]) # Truncate output longer than 4k. | 51 logging.info(stdout[:4096]) # Truncate output longer than 4k. |
40 return stdout | 52 return stdout |
OLD | NEW |