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