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 pipes | 9 import pipes |
10 import signal | 10 import signal |
11 import subprocess | 11 import subprocess |
12 import tempfile | 12 import tempfile |
13 | 13 |
14 import constants | 14 import constants |
15 | 15 |
16 | 16 |
17 def _Call(args, stdout=None, stderr=None, shell=None, cwd=None): | 17 def Call(args, stdout=None, stderr=None, shell=None, cwd=None, env=None): |
18 return subprocess.call( | 18 return subprocess.call( |
19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, | 19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, |
20 shell=shell, close_fds=True, | 20 shell=shell, close_fds=True, env=env, |
21 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) | 21 preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)) |
22 | 22 |
23 | 23 |
24 def RunCmd(args, cwd=None): | 24 def RunCmd(args, cwd=None): |
25 """Opens a subprocess to execute a program and returns its return value. | 25 """Opens a subprocess to execute a program and returns its return value. |
26 | 26 |
27 Args: | 27 Args: |
28 args: A string or a sequence of program arguments. The program to execute is | 28 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. | 29 the string or the first item in the args sequence. |
30 cwd: If not None, the subprocess's current directory will be changed to | 30 cwd: If not None, the subprocess's current directory will be changed to |
31 |cwd| before it's executed. | 31 |cwd| before it's executed. |
32 | 32 |
33 Returns: | 33 Returns: |
34 Return code from the command execution. | 34 Return code from the command execution. |
35 """ | 35 """ |
36 logging.info(str(args) + ' ' + (cwd or '')) | 36 logging.info(str(args) + ' ' + (cwd or '')) |
37 return _Call(args, cwd=cwd) | 37 return Call(args, cwd=cwd) |
38 | 38 |
39 | 39 |
40 def GetCmdOutput(args, cwd=None, shell=False): | 40 def GetCmdOutput(args, cwd=None, shell=False): |
41 """Open a subprocess to execute a program and returns its output. | 41 """Open a subprocess to execute a program and returns its output. |
42 | 42 |
43 Args: | 43 Args: |
44 args: A string or a sequence of program arguments. The program to execute is | 44 args: A string or a sequence of program arguments. The program to execute is |
45 the string or the first item in the args sequence. | 45 the string or the first item in the args sequence. |
46 cwd: If not None, the subprocess's current directory will be changed to | 46 cwd: If not None, the subprocess's current directory will be changed to |
47 |cwd| before it's executed. | 47 |cwd| before it's executed. |
(...skipping 29 matching lines...) Expand all Loading... |
77 else: | 77 else: |
78 args_repr = ' '.join(map(pipes.quote, args)) | 78 args_repr = ' '.join(map(pipes.quote, args)) |
79 | 79 |
80 s = '[host]' | 80 s = '[host]' |
81 if cwd: | 81 if cwd: |
82 s += ':' + cwd | 82 s += ':' + cwd |
83 s += '> ' + args_repr | 83 s += '> ' + args_repr |
84 logging.info(s) | 84 logging.info(s) |
85 tmpout = tempfile.TemporaryFile(bufsize=0) | 85 tmpout = tempfile.TemporaryFile(bufsize=0) |
86 tmperr = tempfile.TemporaryFile(bufsize=0) | 86 tmperr = tempfile.TemporaryFile(bufsize=0) |
87 exit_code = _Call(args, cwd=cwd, stdout=tmpout, stderr=tmperr, shell=shell) | 87 exit_code = Call(args, cwd=cwd, stdout=tmpout, stderr=tmperr, shell=shell) |
88 tmperr.seek(0) | 88 tmperr.seek(0) |
89 stderr = tmperr.read() | 89 stderr = tmperr.read() |
90 tmperr.close() | 90 tmperr.close() |
91 if stderr: | 91 if stderr: |
92 logging.critical(stderr) | 92 logging.critical(stderr) |
93 tmpout.seek(0) | 93 tmpout.seek(0) |
94 stdout = tmpout.read() | 94 stdout = tmpout.read() |
95 tmpout.close() | 95 tmpout.close() |
96 if len(stdout) > 4096: | 96 if len(stdout) > 4096: |
97 logging.debug('Truncated output:') | 97 logging.debug('Truncated output:') |
98 logging.debug(stdout[:4096]) | 98 logging.debug(stdout[:4096]) |
99 return (exit_code, stdout) | 99 return (exit_code, stdout) |
100 | 100 |
101 | 101 |
102 class OutDirectory(object): | 102 class OutDirectory(object): |
103 _out_directory = os.path.join(constants.DIR_SOURCE_ROOT, | 103 _out_directory = os.path.join(constants.DIR_SOURCE_ROOT, |
104 os.environ.get('CHROMIUM_OUT_DIR','out')) | 104 os.environ.get('CHROMIUM_OUT_DIR','out')) |
105 @staticmethod | 105 @staticmethod |
106 def set(out_directory): | 106 def set(out_directory): |
107 OutDirectory._out_directory = out_directory | 107 OutDirectory._out_directory = out_directory |
108 @staticmethod | 108 @staticmethod |
109 def get(): | 109 def get(): |
110 return OutDirectory._out_directory | 110 return OutDirectory._out_directory |
OLD | NEW |