4 from vstf.common import constants
6 LOG = logging.getLogger(__name__)
9 class CommandLine(object):
11 super(CommandLine, self).__init__()
13 self.is_timeout = False
15 def __kill_proc(self):
16 self.is_timeout = True
19 def execute(self, cmd, timeout=constants.TIMEOUT, shell=False):
20 """this func call subprocess.Popen(),
21 here setup a timer to deal with timeout.
22 :param cmd: cmd list like ['ls', 'home']
23 :param timeout: for timer count for timeout
24 :return: (ret, output) the output (stdout+'\n'+stderr)
26 # reset the timeout flag
27 self.is_timeout = False
28 self.proc = subprocess.Popen(cmd,
29 stdout=subprocess.PIPE,
30 stderr=subprocess.PIPE,
33 timer = threading.Timer(timeout, self.__kill_proc, [])
35 stdout, stderr = self.proc.communicate()
38 if self.proc.returncode or self.is_timeout:
40 LOG.error("run cmd<%(cmd)s> timeout", {"cmd": cmd})
42 output = "".join([stderr, stdout])