• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights
2# reserved. Use of this source code is governed by a BSD-style license that
3# can be found in the LICENSE file
4
5from __future__ import absolute_import
6from subprocess import Popen, PIPE
7import sys
8
9
10def exec_cmd(cmd, path, input_string=None):
11  """ Execute the specified command and return the result. """
12  out = ''
13  err = ''
14  ret = -1
15  parts = cmd.split()
16  try:
17    if input_string is None:
18      process = Popen(
19          parts,
20          cwd=path,
21          stdout=PIPE,
22          stderr=PIPE,
23          shell=(sys.platform == 'win32'))
24      out, err = process.communicate()
25      ret = process.returncode
26    else:
27      process = Popen(
28          parts,
29          cwd=path,
30          stdin=PIPE,
31          stdout=PIPE,
32          stderr=PIPE,
33          shell=(sys.platform == 'win32'))
34      out, err = process.communicate(input=input_string)
35      ret = process.returncode
36  except IOError as e:
37    (errno, strerror) = e.args
38    raise
39  except:
40    raise
41  return {'out': out.decode('utf-8'), 'err': err.decode('utf-8'), 'ret': ret}
42