1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/build/android/pylib/cmd_helper.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,50 @@ 1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +# Use of this source code is governed by a BSD-style license that can be 1.6 +# found in the LICENSE file. 1.7 + 1.8 +"""A wrapper for subprocess to make calling shell commands easier.""" 1.9 + 1.10 + 1.11 +import logging 1.12 +import subprocess 1.13 + 1.14 + 1.15 +def RunCmd(args, cwd=None): 1.16 + """Opens a subprocess to execute a program and returns its return value. 1.17 + 1.18 + Args: 1.19 + args: A string or a sequence of program arguments. The program to execute is 1.20 + the string or the first item in the args sequence. 1.21 + cwd: If not None, the subprocess's current directory will be changed to 1.22 + |cwd| before it's executed. 1.23 + 1.24 + Returns: 1.25 + Return code from the command execution. 1.26 + """ 1.27 + logging.info(str(args) + ' ' + (cwd or '')) 1.28 + p = subprocess.Popen(args=args, cwd=cwd) 1.29 + return p.wait() 1.30 + 1.31 + 1.32 +def GetCmdOutput(args, cwd=None, shell=False): 1.33 + """Open a subprocess to execute a program and returns its output. 1.34 + 1.35 + Args: 1.36 + args: A string or a sequence of program arguments. The program to execute is 1.37 + the string or the first item in the args sequence. 1.38 + cwd: If not None, the subprocess's current directory will be changed to 1.39 + |cwd| before it's executed. 1.40 + shell: Whether to execute args as a shell command. 1.41 + 1.42 + Returns: 1.43 + Captures and returns the command's stdout. 1.44 + Prints the command's stderr to logger (which defaults to stdout). 1.45 + """ 1.46 + logging.info(str(args) + ' ' + (cwd or '')) 1.47 + p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE, 1.48 + stderr=subprocess.PIPE, shell=shell) 1.49 + stdout, stderr = p.communicate() 1.50 + if stderr: 1.51 + logging.critical(stderr) 1.52 + logging.info(stdout[:4096]) # Truncate output longer than 4k. 1.53 + return stdout