def shell_command(cmd, timeout=15): """shell communication for quick return in sync mode""" proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time_cnt = 0 exit_code = None result = [] while time_cnt < timeout: exit_code = proc.poll() if not exit_code is None: break time_cnt += 0.2 time.sleep(0.2) if exit_code is None: killall(proc.pid) exit_code = -1 result = [] elif not cmd.endswith('&'): while True: line = proc.stdout.readline() if not line or line.find('daemon started') >= 0: break result.append(line) return [exit_code, result]
def kill_testkit_lite(pid_file): """ kill testkit lite""" try: with open(pid_file, "r") as pidfile: pid = pidfile.readline().rstrip("\n") if pid: killall(pid) except IOError as error: pattern = re.compile("No such file or directory|No such process") match = pattern.search(str(error)) if not match: LOGGER.info("[ Error: fail to kill existing testkit-lite, " "error: %s ]\n" % error) return None
def kill_testkit_lite(pid_file): """ kill testkit lite""" try: with open(pid_file, "r") as pidfile: pid = pidfile.readline().rstrip("\n") if pid: killall(pid) except IOError as error: pattern = re.compile('No such file or directory|No such process') match = pattern.search(str(error)) if not match: LOGGER.info("[ Error: fail to kill existing testkit-lite, "\ "error: %s ]\n" % error) return None
def debug_trace(cmdline, logfile): global debug_flag, metux wbuffile = file(logfile, "a") import subprocess exit_code = None proc = subprocess.Popen(args=cmdline, shell=True, stdout=wbuffile, stderr=None) while True: exit_code = proc.poll() if exit_code is not None: break time.sleep(0.5) metux.acquire() proc_flag = debug_flag metux.release() if not proc_flag: break wbuffile.close() if exit_code is None: killall(proc.pid)
def debug_trace(cmdline, logfile): global debug_flag, metux wbuffile = file(logfile, "w") import subprocess exit_code = None proc = subprocess.Popen(args=cmdline, shell=True, stdout=wbuffile, stderr=None) while True: exit_code = proc.poll() if exit_code is not None: break time.sleep(0.5) metux.acquire() proc_flag = debug_flag metux.release() if not proc_flag: break wbuffile.close() if exit_code is None: killall(proc.pid)
def shell_command_ext(cmd="", timeout=None, boutput=False, stdout_file=None, stderr_file=None, callbk=None): """shell executor, return [exitcode, stdout/stderr] timeout: None means unlimited timeout boutput: specify whether print output during the command running """ if stdout_file is None: stdout_file = os.path.expanduser("~") + os.sep + "shell_stdout" if os.path.isfile(stdout_file): stdout_file = "%s%d" % (stdout_file, len(glob.glob("%s*" % stdout_file))) if stderr_file is None: stderr_file = os.path.expanduser("~") + os.sep + "shell_stderr" if os.path.isfile(stderr_file): stderr_file = "%s%d" % (stderr_file, len(glob.glob("%s*" % stderr_file))) exit_code = None wbuffile1 = file(stdout_file, "w") wbuffile2 = file(stderr_file, "w") rbuffile1 = file(stdout_file, "r") rbuffile2 = file(stderr_file, "r") cmd_open = subprocess.Popen(args=cmd, shell=True, stdout=wbuffile1, stderr=wbuffile2) rbuffile1.seek(0) rbuffile2.seek(0) def print_log(): """print the stdout to terminate""" if callbk and callable(callbk): callbk(rbuffile1.read()) else: sys.stdout.write(rbuffile1.read()) sys.stdout.write(rbuffile2.read()) sys.stdout.flush() while True: exit_code = cmd_open.poll() if exit_code is not None: break if boutput: print_log() if timeout is not None: timeout -= 0.1 if timeout <= 0: exit_code = "timeout" killall(cmd_open.pid) time.sleep(3) break time.sleep(0.1) if boutput: print_log() rbuffile1.seek(0) rbuffile2.seek(0) stdout_log = str2str(rbuffile1.read()) stderr_log = str2str(rbuffile2.read()) if 'returncode=' in stdout_log: index = stdout_log.find('returncode=') + 11 exit_code = str(stdout_log[index:]).strip('\r\n') stdout_log = '<![CDATA[' + stdout_log + ']]>' stderr_log = '<![CDATA[' + stderr_log + ']]>' wbuffile1.close() wbuffile2.close() rbuffile1.close() rbuffile2.close() os.remove(stdout_file) os.remove(stderr_file) return [exit_code, stdout_log, stderr_log]
def shell_command_ext(cmd="", timeout=None, boutput=False, stdout_file=None, stderr_file=None, callbk=None): """shell executor, return [exitcode, stdout/stderr] timeout: None means unlimited timeout boutput: specify whether print output during the command running """ if stdout_file is None: stdout_file = os.path.expanduser("~") + os.sep + "shell_stdout" if stderr_file is None: stderr_file = os.path.expanduser("~") + os.sep + "shell_stderr" exit_code = None wbuffile1 = file(stdout_file, "w") wbuffile2 = file(stderr_file, "w") rbuffile1 = file(stdout_file, "r") rbuffile2 = file(stderr_file, "r") cmd_open = subprocess.Popen(args=cmd, shell=True, stdout=wbuffile1, stderr=wbuffile2) rbuffile1.seek(0) rbuffile2.seek(0) def print_log(): """print the stdout to terminate""" if callbk and callable(callbk): callbk(rbuffile1.read()) else: sys.stdout.write(rbuffile1.read()) sys.stdout.write(rbuffile2.read()) sys.stdout.flush() while True: exit_code = cmd_open.poll() if exit_code is not None: break if boutput: print_log() if timeout is not None: timeout -= 0.1 if timeout <= 0: exit_code = "timeout" killall(cmd_open.pid) time.sleep(3) break time.sleep(0.1) if boutput: print_log() rbuffile1.seek(0) rbuffile2.seek(0) stdout_log = str2str(rbuffile1.read()) stderr_log = str2str(rbuffile2.read()) if 'returncode=' in stdout_log: index = stdout_log.find('returncode=') + 11 exit_code = str(stdout_log[index:]).strip('\r\n') stdout_log = '<![CDATA[' + stdout_log + ']]>' stderr_log = '<![CDATA[' + stderr_log + ']]>' wbuffile1.close() wbuffile2.close() rbuffile1.close() rbuffile2.close() os.remove(stdout_file) os.remove(stderr_file) return [exit_code, stdout_log, stderr_log]