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 | 7 |
8 import logging | 8 import logging |
9 import subprocess | 9 import subprocess |
10 | 10 |
11 | 11 |
12 def RunCmd(args, cwd=None): | 12 def RunCmd(args, cwd=None): |
13 """Opens a subprocess to execute a program and returns its return value. | 13 """Opens a subprocess to execute a program and returns its return value. |
14 | 14 |
15 Args: | 15 Args: |
16 args: A string or a sequence of program arguments. The program to execute is | 16 args: A string or a sequence of program arguments. The program to execute is |
17 the string or the first item in the args sequence. | 17 the string or the first item in the args sequence. |
18 cwd: If not None, the subprocess's current directory will be changed to | 18 cwd: If not None, the subprocess's current directory will be changed to |
19 |cwd| before it's executed. | 19 |cwd| before it's executed. |
20 | 20 |
21 Returns: | 21 Returns: |
22 Return code from the command execution. | 22 Return code from the command execution. |
23 """ | 23 """ |
24 logging.info(str(args) + ' ' + (cwd or '')) | 24 logging.info(str(args) + ' ' + (cwd or '')) |
25 p = subprocess.Popen(args=args, cwd=cwd) | 25 p = subprocess.Popen(args=args, cwd=cwd) |
26 return p.wait() | 26 return p.wait() |
27 | 27 |
28 | 28 |
29 def GetCmdOutput(args, cwd=None, shell=False): | 29 def GetCmdOutput(args, cwd=None, shell=False): |
felipeg
2012/10/30 22:37:30
Change this function to call GetCmdStatusAndOutput
Philippe
2012/10/31 10:17:41
Done.
| |
30 """Open a subprocess to execute a program and returns its output. | 30 """Open a subprocess to execute a program and returns its output. |
31 | 31 |
32 Args: | 32 Args: |
33 args: A string or a sequence of program arguments. The program to execute is | 33 args: A string or a sequence of program arguments. The program to execute is |
34 the string or the first item in the args sequence. | 34 the string or the first item in the args sequence. |
35 cwd: If not None, the subprocess's current directory will be changed to | 35 cwd: If not None, the subprocess's current directory will be changed to |
36 |cwd| before it's executed. | 36 |cwd| before it's executed. |
37 shell: Whether to execute args as a shell command. | 37 shell: Whether to execute args as a shell command. |
38 | 38 |
39 Returns: | 39 Returns: |
40 Captures and returns the command's stdout. | 40 Captures and returns the command's stdout. |
41 Prints the command's stderr to logger (which defaults to stdout). | 41 Prints the command's stderr to logger (which defaults to stdout). |
42 """ | 42 """ |
43 logging.info(str(args) + ' ' + (cwd or '')) | 43 logging.info(str(args) + ' ' + (cwd or '')) |
44 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, | 44 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, |
45 stderr=subprocess.PIPE, shell=shell) | 45 stderr=subprocess.PIPE, shell=shell) |
46 stdout, stderr = p.communicate() | 46 stdout, stderr = p.communicate() |
47 if stderr: | 47 if stderr: |
48 logging.critical(stderr) | 48 logging.critical(stderr) |
49 logging.info(stdout[:4096]) # Truncate output longer than 4k. | 49 logging.info(stdout[:4096]) # Truncate output longer than 4k. |
50 return stdout | 50 return stdout |
51 | |
52 def GetCmdStatusAndOutput(args, cwd=None, shell=False): | |
53 """Executes a subprocess and returns its exit code and output. | |
54 | |
55 Args: | |
56 args: A string or a sequence of program arguments. The program to execute is | |
57 the string or the first item in the args sequence. | |
58 cwd: If not None, the subprocess's current directory will be changed to | |
59 |cwd| before it's executed. | |
60 shell: Whether to execute args as a shell command. | |
61 | |
62 Returns: | |
63 The tuple (exit code, output). | |
64 """ | |
65 logging.info(str(args) + ' ' + (cwd or '')) | |
66 p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, | |
67 stderr=subprocess.PIPE, shell=shell) | |
68 stdout, stderr = p.communicate() | |
69 exit_code = p.returncode | |
70 if stderr: | |
71 logging.critical(stderr) | |
72 logging.info(stdout[:4096]) # Truncate output longer than 4k. | |
73 return (exit_code, stdout) | |
OLD | NEW |