summaryrefslogtreecommitdiffstats
path: root/build/android/cmd_helper.py
blob: 901cbe9aed76e110da5f2c06bc652fcda522fb00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


import logging
import subprocess


def RunCmd(args, cwd=None):
  """Opens a subprocess to execute a program and returns its return value.

  Args:
    args: A string or a sequence of program arguments. The program to execute is
      the string or the first item in the args sequence.
    cwd: If not None, the subprocess's current directory will be changed to
      |cwd| before it's executed.
  """
  logging.info(str(args) + ' ' + (cwd or ''))
  p = subprocess.Popen(args=args, cwd=cwd)
  return p.wait()


def GetCmdOutput(args, cwd=None):
  """Open a subprocess to execute a program and returns its output.

  Args:
    args: A string or a sequence of program arguments. The program to execute is
      the string or the first item in the args sequence.
    cwd: If not None, the subprocess's current directory will be changed to
      |cwd| before it's executed.
  """
  logging.info(str(args) + ' ' + (cwd or ''))
  p = subprocess.Popen(args=args, cwd=cwd, stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
  stdout, stderr = p.communicate()
  if stderr:
    logging.critical(stderr)
  logging.info(stdout[:4096])  # Truncate output longer than 4k.
  return stdout