| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 | 6 |
| 7 """Run a command and report its results in machine-readable format.""" | 7 """Run a command and report its results in machine-readable format.""" |
| 8 | 8 |
| 9 | 9 |
| 10 import collections | 10 import collections |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 165 |
| 166 | 166 |
| 167 def _launch_cmd(cmd): | 167 def _launch_cmd(cmd): |
| 168 """Launch the given command. Non-blocking. | 168 """Launch the given command. Non-blocking. |
| 169 | 169 |
| 170 Args: | 170 Args: |
| 171 cmd: list of strings; command to run. | 171 cmd: list of strings; command to run. |
| 172 Returns: | 172 Returns: |
| 173 subprocess.Popen instance. | 173 subprocess.Popen instance. |
| 174 """ | 174 """ |
| 175 print ' '.join(cmd) | |
| 176 subprocess.call(cmd) | |
| 177 return subprocess.Popen(cmd, shell=False, stderr=subprocess.PIPE, | 175 return subprocess.Popen(cmd, shell=False, stderr=subprocess.PIPE, |
| 178 stdout=subprocess.PIPE) | 176 stdout=subprocess.PIPE) |
| 179 | 177 |
| 180 | 178 |
| 181 def _get_result(popen): | 179 def _get_result(popen): |
| 182 """Get the results from a running process. Blocks until the process completes. | 180 """Get the results from a running process. Blocks until the process completes. |
| 183 | 181 |
| 184 Args: | 182 Args: |
| 185 popen: subprocess.Popen instance. | 183 popen: subprocess.Popen instance. |
| 186 Returns: | 184 Returns: |
| 187 A dictionary with stdout, stderr, and returncode as keys. | 185 A dictionary with stdout, stderr, and returncode as keys. |
| 188 """ | 186 """ |
| 189 for fd in (popen.stdout, popen.stderr): | |
| 190 for line in iter(fd.readline, ''): | |
| 191 print line | |
| 192 stdout, stderr = popen.communicate() | 187 stdout, stderr = popen.communicate() |
| 193 return CommandResults.make(stdout=stdout, | 188 return CommandResults.make(stdout=stdout, |
| 194 stderr=stderr, | 189 stderr=stderr, |
| 195 returncode=popen.returncode) | 190 returncode=popen.returncode) |
| 196 | 191 |
| 197 | 192 |
| 198 def run(cmd): | 193 def run(cmd): |
| 199 """Run the command, block until it completes, and return a results dictionary. | 194 """Run the command, block until it completes, and return a results dictionary. |
| 200 | 195 |
| 201 Args: | 196 Args: |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 procs = [] | 300 procs = [] |
| 306 | 301 |
| 307 for hostname in slave_hosts_cfg.SLAVE_HOSTS.iterkeys(): | 302 for hostname in slave_hosts_cfg.SLAVE_HOSTS.iterkeys(): |
| 308 if not slave_hosts_cfg.SLAVE_HOSTS[hostname].login_cmd: | 303 if not slave_hosts_cfg.SLAVE_HOSTS[hostname].login_cmd: |
| 309 results.update( | 304 results.update( |
| 310 {hostname: CommandResults.make(stderr='No procedure for login.')}) | 305 {hostname: CommandResults.make(stderr='No procedure for login.')}) |
| 311 else: | 306 else: |
| 312 procs.append((hostname, _launch_on_remote_host(hostname, cmd))) | 307 procs.append((hostname, _launch_on_remote_host(hostname, cmd))) |
| 313 | 308 |
| 314 for slavename, proc in procs: | 309 for slavename, proc in procs: |
| 315 print 'Awaiting results from %s' % slavename | |
| 316 results.update(_get_remote_host_results(slavename, proc)) | 310 results.update(_get_remote_host_results(slavename, proc)) |
| 317 | 311 |
| 318 return results | 312 return results |
| 319 | 313 |
| 320 | 314 |
| 321 def parse_args(positional_args=None): | 315 def parse_args(positional_args=None): |
| 322 """Common argument parser for scripts using this module. | 316 """Common argument parser for scripts using this module. |
| 323 | 317 |
| 324 Args: | 318 Args: |
| 325 positional_args: optional list of strings; extra positional arguments to | 319 positional_args: optional list of strings; extra positional arguments to |
| (...skipping 28 matching lines...) Expand all Loading... |
| 354 setattr(options, cmd, args) | 348 setattr(options, cmd, args) |
| 355 except IndexError: | 349 except IndexError: |
| 356 parser.print_usage() | 350 parser.print_usage() |
| 357 sys.exit(1) | 351 sys.exit(1) |
| 358 return options | 352 return options |
| 359 | 353 |
| 360 | 354 |
| 361 if '__main__' == __name__: | 355 if '__main__' == __name__: |
| 362 parsed_args = parse_args() | 356 parsed_args = parse_args() |
| 363 run(parsed_args.cmd).print_results(pretty=parsed_args.pretty) | 357 run(parsed_args.cmd).print_results(pretty=parsed_args.pretty) |
| OLD | NEW |