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 signal | 10 import signal |
10 import subprocess | 11 import subprocess |
11 import tempfile | 12 import tempfile |
12 | 13 |
13 import constants | 14 import constants |
14 | 15 |
15 | 16 |
16 def _Call(args, stdout=None, stderr=None, shell=None, cwd=None): | 17 def _Call(args, stdout=None, stderr=None, shell=None, cwd=None): |
17 return subprocess.call( | 18 return subprocess.call( |
18 args=args, cwd=cwd, stdout=stdout, stderr=stderr, | 19 args=args, cwd=cwd, stdout=stdout, stderr=stderr, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 Args: | 61 Args: |
61 args: A string or a sequence of program arguments. The program to execute is | 62 args: A string or a sequence of program arguments. The program to execute is |
62 the string or the first item in the args sequence. | 63 the string or the first item in the args sequence. |
63 cwd: If not None, the subprocess's current directory will be changed to | 64 cwd: If not None, the subprocess's current directory will be changed to |
64 |cwd| before it's executed. | 65 |cwd| before it's executed. |
65 shell: Whether to execute args as a shell command. | 66 shell: Whether to execute args as a shell command. |
66 | 67 |
67 Returns: | 68 Returns: |
68 The tuple (exit code, output). | 69 The tuple (exit code, output). |
69 """ | 70 """ |
70 logging.info(str(args) + ' ' + (cwd or '')) | 71 if isinstance(args, basestring): |
| 72 args_repr = args |
| 73 if not shell: |
| 74 raise Exception('string args must be run with shell=True') |
| 75 elif shell: |
| 76 raise Exception('array args must be run with shell=False') |
| 77 else: |
| 78 args_repr = ' '.join(map(pipes.quote, args)) |
| 79 |
| 80 s = '[host]' |
| 81 if cwd: |
| 82 s += ':' + cwd |
| 83 s += '> ' + args_repr |
| 84 logging.info(s) |
71 tmpout = tempfile.TemporaryFile(bufsize=0) | 85 tmpout = tempfile.TemporaryFile(bufsize=0) |
72 tmperr = tempfile.TemporaryFile(bufsize=0) | 86 tmperr = tempfile.TemporaryFile(bufsize=0) |
73 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) |
74 tmperr.seek(0) | 88 tmperr.seek(0) |
75 stderr = tmperr.read() | 89 stderr = tmperr.read() |
76 tmperr.close() | 90 tmperr.close() |
77 if stderr: | 91 if stderr: |
78 logging.critical(stderr) | 92 logging.critical(stderr) |
79 tmpout.seek(0) | 93 tmpout.seek(0) |
80 stdout = tmpout.read() | 94 stdout = tmpout.read() |
81 tmpout.close() | 95 tmpout.close() |
82 logging.info(stdout[:4096]) # Truncate output longer than 4k. | 96 if len(stdout) > 4096: |
| 97 logging.debug('Truncated output:') |
| 98 logging.debug(stdout[:4096]) |
83 return (exit_code, stdout) | 99 return (exit_code, stdout) |
84 | 100 |
85 | 101 |
86 class OutDirectory(object): | 102 class OutDirectory(object): |
87 _out_directory = os.path.join(constants.CHROME_DIR, 'out') | 103 _out_directory = os.path.join(constants.CHROME_DIR, 'out') |
88 @staticmethod | 104 @staticmethod |
89 def set(out_directory): | 105 def set(out_directory): |
90 OutDirectory._out_directory = out_directory | 106 OutDirectory._out_directory = out_directory |
91 @staticmethod | 107 @staticmethod |
92 def get(): | 108 def get(): |
93 return OutDirectory._out_directory | 109 return OutDirectory._out_directory |
OLD | NEW |