summaryrefslogtreecommitdiff
path: root/tools/exec_util.py
blob: a6c23479b07bb32707d786a2e1717ccb21f86453 (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
41
# Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file

from __future__ import absolute_import
from subprocess import Popen, PIPE
import sys


def exec_cmd(cmd, path, input_string=None):
  """ Execute the specified command and return the result. """
  out = ''
  err = ''
  ret = -1
  parts = cmd.split()
  try:
    if input_string is None:
      process = Popen(
          parts,
          cwd=path,
          stdout=PIPE,
          stderr=PIPE,
          shell=(sys.platform == 'win32'))
      out, err = process.communicate()
      ret = process.returncode
    else:
      process = Popen(
          parts,
          cwd=path,
          stdin=PIPE,
          stdout=PIPE,
          stderr=PIPE,
          shell=(sys.platform == 'win32'))
      out, err = process.communicate(input=input_string)
      ret = process.returncode
  except IOError as e:
    (errno, strerror) = e.args
    raise
  except:
    raise
  return {'out': out.decode('utf-8'), 'err': err.decode('utf-8'), 'ret': ret}