1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from vstf.common import constants
15 LOG = logging.getLogger(__name__)
18 class CommandLine(object):
21 super(CommandLine, self).__init__()
23 self.is_timeout = False
25 def __kill_proc(self):
26 self.is_timeout = True
29 def execute(self, cmd, timeout=constants.TIMEOUT, shell=False):
30 """this func call subprocess.Popen(),
31 here setup a timer to deal with timeout.
32 :param cmd: cmd list like ['ls', 'home']
33 :param timeout: for timer count for timeout
34 :return: (ret, output) the output (stdout+'\n'+stderr)
36 # reset the timeout flag
37 self.is_timeout = False
38 self.proc = subprocess.Popen(cmd,
39 stdout=subprocess.PIPE,
40 stderr=subprocess.PIPE,
43 timer = threading.Timer(timeout, self.__kill_proc, [])
45 stdout, stderr = self.proc.communicate()
48 if self.proc.returncode or self.is_timeout:
50 LOG.error("run cmd<%(cmd)s> timeout", {"cmd": cmd})
52 output = "".join([stderr, stdout])