from .obj import BaseObjectError
from .obj import BaseObject
-__version__ = '1.0.0'
+__version__ = '1.1.0'
LOG = logging.getLogger(__name__)
used_stdout = subprocess.PIPE
if stdout is not None:
used_stdout = stdout
+ use_stdout = True
+ if used_stdout is None:
+ use_stdout = False
used_stderr = subprocess.PIPE
if drop_stderr:
used_stderr = None
elif stderr is not None:
used_stderr = stderr
+ use_stderr = True
+ if used_stderr is None:
+ use_stderr = False
cur_locale = locale.getlocale()
cur_encoding = cur_locale[1]
# cwd=self.base_dir,
# Display Output of executable
- stdoutdata = ''
- stderrdata = ''
- if six.PY3:
- stdoutdata = bytearray()
- stderrdata = bytearray()
-
if hb_handler is not None:
- if not quiet or self.verbose > 1:
- LOG.debug((
- "Starting asynchronous communication with '{cmd}', "
- "heartbeat interval is {interval:0.1f} seconds.").format(
- cmd=cmd_str, interval=hb_interval))
-
- out_flags = fcntl(cmd_obj.stdout, F_GETFL)
- err_flags = fcntl(cmd_obj.stderr, F_GETFL)
- fcntl(cmd_obj.stdout, F_SETFL, out_flags | os.O_NONBLOCK)
- fcntl(cmd_obj.stderr, F_SETFL, err_flags | os.O_NONBLOCK)
-
- start_time = time.time()
-
- while True:
-
- if self.verbose > 3:
- LOG.debug("Checking for the end of the communication ...")
- if cmd_obj.poll() is not None:
- cmd_obj.wait()
- break
-
- # Heartbeat handling ...
- cur_time = time.time()
- time_diff = cur_time - start_time
- if time_diff >= hb_interval:
- if not quiet or self.verbose > 1:
- LOG.debug("Time to execute the heartbeat handler.")
- if hb_handler:
- hb_handler()
- start_time = cur_time
- if self.verbose > 3:
- LOG.debug("Sleeping {:0.2f} seconds ...".format(poll_interval))
- time.sleep(poll_interval)
-
- # Reading out file descriptors
- if used_stdout is not None:
- try:
- stdoutdata += os.read(cmd_obj.stdout.fileno(), 1024)
- if self.verbose > 3:
- LOG.debug(" stdout is now: {!r}".format(stdoutdata))
- except OSError:
- pass
- if used_stderr is not None:
- try:
- stderrdata += os.read(cmd_obj.stderr.fileno(), 1024)
- if self.verbose > 3:
- LOG.debug(" stderr is now: {!r}".format(stderrdata))
- except OSError:
- pass
+ (stdoutdata, stderrdata) = self._wait_for_proc_with_heartbeat(
+ cmd_obj=cmd_obj, cmd_str=cmd_str, hb_handler=hb_handler, hb_interval=hb_interval,
+ use_stdout=use_stdout, use_stderr=use_stderr,
+ poll_interval=poll_interval, quiet=quiet)
+
else:
+
if not quiet or self.verbose > 1:
LOG.debug("Starting synchronous communication with '{}'.".format(cmd_str))
(stdoutdata, stderrdata) = cmd_obj.communicate()
LOG.debug("Finished communication with '{}'.".format(cmd_str))
ret = cmd_obj.wait()
+
+ return self._eval_call_results(
+ ret, stderrdata, stdoutdata, cur_encoding=cur_encoding,
+ log_output=log_output, quiet=quiet)
+
+ # -------------------------------------------------------------------------
+ def _eval_call_results(
+ self, ret, stderrdata, stdoutdata,
+ cur_encoding='utf-8', log_output=True, quiet=False):
+
if not quiet:
LOG.debug("Returncode: {}".format(ret))
return (ret, stdoutdata, stderrdata)
+ # -------------------------------------------------------------------------
+ def _wait_for_proc_with_heartbeat(
+ self, cmd_obj, cmd_str, hb_handler, hb_interval, use_stdout=True, use_stderr=True,
+ poll_interval=0.2, quiet=False):
+
+ stdoutdata = ''
+ stderrdata = ''
+ if six.PY3:
+ stdoutdata = bytearray()
+ stderrdata = bytearray()
+
+ if not quiet or self.verbose > 1:
+ LOG.debug((
+ "Starting asynchronous communication with '{cmd}', "
+ "heartbeat interval is {interval:0.1f} seconds.").format(
+ cmd=cmd_str, interval=hb_interval))
+
+ out_flags = fcntl(cmd_obj.stdout, F_GETFL)
+ err_flags = fcntl(cmd_obj.stderr, F_GETFL)
+ fcntl(cmd_obj.stdout, F_SETFL, out_flags | os.O_NONBLOCK)
+ fcntl(cmd_obj.stderr, F_SETFL, err_flags | os.O_NONBLOCK)
+
+ start_time = time.time()
+
+ while True:
+
+ if self.verbose > 3:
+ LOG.debug("Checking for the end of the communication ...")
+ if cmd_obj.poll() is not None:
+ cmd_obj.wait()
+ break
+
+ # Heartbeat handling ...
+ cur_time = time.time()
+ time_diff = cur_time - start_time
+ if time_diff >= hb_interval:
+ if not quiet or self.verbose > 1:
+ LOG.debug("Time to execute the heartbeat handler.")
+ hb_handler()
+ start_time = cur_time
+ if self.verbose > 3:
+ LOG.debug("Sleeping {:0.2f} seconds ...".format(poll_interval))
+ time.sleep(poll_interval)
+
+ # Reading out file descriptors
+ if use_stdout:
+ try:
+ stdoutdata += os.read(cmd_obj.stdout.fileno(), 1024)
+ if self.verbose > 3:
+ LOG.debug(" stdout is now: {!r}".format(stdoutdata))
+ except OSError:
+ pass
+
+ if use_stderr:
+ try:
+ stderrdata += os.read(cmd_obj.stderr.fileno(), 1024)
+ if self.verbose > 3:
+ LOG.debug(" stderr is now: {!r}".format(stderrdata))
+ except OSError:
+ pass
+
+ return (stdoutdata, stderrdata)
+
# -------------------------------------------------------------------------
def read_file(self, filename, timeout=2, quiet=False):
"""
return
-# =============================================================================
+# =============================================================================
if __name__ == "__main__":
pass