From: Frank Brehm Date: Fri, 14 Sep 2018 13:40:23 +0000 (+0200) Subject: Applying flake8 to lib/webhooks/handler.py X-Git-Tag: 1.6.4^2~117 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=89b0d14c66e19b883489d688b7a36c30768afd6d;p=pixelpark%2Fpuppetmaster-webhooks.git Applying flake8 to lib/webhooks/handler.py --- diff --git a/lib/webhooks/handler.py b/lib/webhooks/handler.py index 696f497..64040a4 100644 --- a/lib/webhooks/handler.py +++ b/lib/webhooks/handler.py @@ -33,7 +33,7 @@ from .errors import CommandNotFoundError from .obj import BaseObjectError from .obj import BaseObject -__version__ = '1.0.0' +__version__ = '1.1.0' LOG = logging.getLogger(__name__) @@ -389,12 +389,18 @@ class BaseHandler(BaseObject): 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] @@ -416,64 +422,15 @@ class BaseHandler(BaseObject): # 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() @@ -482,6 +439,16 @@ class BaseHandler(BaseObject): 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)) @@ -517,6 +484,69 @@ class BaseHandler(BaseObject): 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): """ @@ -675,8 +705,8 @@ class BaseHandler(BaseObject): return -# ============================================================================= +# ============================================================================= if __name__ == "__main__": pass