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):
20 super(CommandLine, self).__init__()
22 self.is_timeout = False
24 def __kill_proc(self):
25 self.is_timeout = True
28 def execute(self, cmd, timeout=constants.TIMEOUT, shell=False):
29 """this func call subprocess.Popen(),
30 here setup a timer to deal with timeout.
31 :param cmd: cmd list like ['ls', 'home']
32 :param timeout: for timer count for timeout
33 :return: (ret, output) the output (stdout+'\n'+stderr)
35 # reset the timeout flag
36 self.is_timeout = False
37 self.proc = subprocess.Popen(cmd,
38 stdout=subprocess.PIPE,
39 stderr=subprocess.PIPE,
42 timer = threading.Timer(timeout, self.__kill_proc, [])
44 stdout, stderr = self.proc.communicate()
47 if self.proc.returncode or self.is_timeout:
49 LOG.error("run cmd<%(cmd)s> timeout", {"cmd": cmd})
51 output = "".join([stderr, stdout])