From d6d6fbeb988268ba7a10bd01e6b8d2fbcd063907 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 22 Dec 2020 17:09:56 +0100 Subject: [PATCH] renamed `_subprocess` to `run_subprocess` and moved to `execute.py` --- pype/lib/__init__.py | 18 +++++----- pype/lib/applications.py | 65 ----------------------------------- pype/lib/execute.py | 73 +++++++++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 98 deletions(-) diff --git a/pype/lib/__init__.py b/pype/lib/__init__.py index 09cc998b7c..39f08340fd 100644 --- a/pype/lib/__init__.py +++ b/pype/lib/__init__.py @@ -2,7 +2,10 @@ """Pype module API.""" from .terminal import Terminal -from .execute import execute +from .execute import ( + execute, + run_subprocess +) from .log import PypeLogger, timeit from .mongo import ( decompose_url, @@ -48,8 +51,7 @@ from .applications import ( ApplicationNotFound, ApplicationManager, PreLaunchHook, - PostLaunchHook, - _subprocess + PostLaunchHook ) from .plugin_tools import ( @@ -83,6 +85,9 @@ from .ffmpeg_utils import ( terminal = Terminal __all__ = [ + "execute", + "run_subprocess", + "env_value_to_bool", "get_paths_from_environ", @@ -98,9 +103,6 @@ __all__ = [ "get_latest_version", "BuildWorkfile", - "PypeHook", - "execute_hook", - "ApplicationLaunchFailed", "ApplictionExecutableNotFound", "ApplicationNotFound", @@ -124,8 +126,6 @@ __all__ = [ "ffprobe_streams", "get_ffmpeg_tool_path", - "_subprocess", - "terminal", "Anatomy", "get_datetime_data", @@ -134,7 +134,7 @@ __all__ = [ "get_presets", "get_init_presets", "update_dict", - "execute", + "PypeLogger", "decompose_url", "compose_url", diff --git a/pype/lib/applications.py b/pype/lib/applications.py index c7d6464418..dad2a417a9 100644 --- a/pype/lib/applications.py +++ b/pype/lib/applications.py @@ -67,71 +67,6 @@ class ApplicationLaunchFailed(Exception): pass -# Special naming case for subprocess since its a built-in method. -def _subprocess(*args, **kwargs): - """Convenience method for getting output errors for subprocess. - - Entered arguments and keyword arguments are passed to subprocess Popen. - - Args: - *args: Variable length arument list passed to Popen. - **kwargs : Arbitary keyword arguments passed to Popen. Is possible to - pass `logging.Logger` object under "logger" if want to use - different than lib's logger. - - Returns: - str: Full output of subprocess concatenated stdout and stderr. - - Raises: - RuntimeError: Exception is raised if process finished with nonzero - return code. - """ - - # Get environents from kwarg or use current process environments if were - # not passed. - env = kwargs.get("env") or os.environ - # Make sure environment contains only strings - filtered_env = {k: str(v) for k, v in env.items()} - - # Use lib's logger if was not passed with kwargs. - logger = kwargs.pop("logger", log) - - # set overrides - kwargs['stdout'] = kwargs.get('stdout', subprocess.PIPE) - kwargs['stderr'] = kwargs.get('stderr', subprocess.PIPE) - kwargs['stdin'] = kwargs.get('stdin', subprocess.PIPE) - kwargs['env'] = filtered_env - - proc = subprocess.Popen(*args, **kwargs) - - full_output = "" - _stdout, _stderr = proc.communicate() - if _stdout: - _stdout = _stdout.decode("utf-8") - full_output += _stdout - logger.debug(_stdout) - - if _stderr: - _stderr = _stderr.decode("utf-8") - # Add additional line break if output already containt stdout - if full_output: - full_output += "\n" - full_output += _stderr - logger.warning(_stderr) - - if proc.returncode != 0: - exc_msg = "Executing arguments was not successful: \"{}\"".format(args) - if _stdout: - exc_msg += "\n\nOutput:\n{}".format(_stdout) - - if _stderr: - exc_msg += "Error:\n{}".format(_stderr) - - raise RuntimeError(exc_msg) - - return full_output - - class ApplicationManager: def __init__(self): self.log = PypeLogger().get_logger(self.__class__.__name__) diff --git a/pype/lib/execute.py b/pype/lib/execute.py index d7951df384..1f1adcdf23 100644 --- a/pype/lib/execute.py +++ b/pype/lib/execute.py @@ -69,42 +69,67 @@ def execute(args, return popen.returncode -def _subprocess(*args, **kwargs): +def run_subprocess(*args, **kwargs): """Convenience method for getting output errors for subprocess. - .. seealso:: :mod:`subprocess` + Output logged when process finish. + Entered arguments and keyword arguments are passed to subprocess Popen. + + Args: + *args: Variable length arument list passed to Popen. + **kwargs : Arbitary keyword arguments passed to Popen. Is possible to + pass `logging.Logger` object under "logger" if want to use + different than lib's logger. + + Returns: + str: Full output of subprocess concatenated stdout and stderr. + + Raises: + RuntimeError: Exception is raised if process finished with nonzero + return code. """ - # make sure environment contains only strings - if not kwargs.get("env"): - filtered_env = {k: str(v) for k, v in os.environ.items()} - else: - filtered_env = {k: str(v) for k, v in kwargs.get("env").items()} + + # Get environents from kwarg or use current process environments if were + # not passed. + env = kwargs.get("env") or os.environ + # Make sure environment contains only strings + filtered_env = {k: str(v) for k, v in env.items()} + + # Use lib's logger if was not passed with kwargs. + logger = kwargs.pop("logger", log) # set overrides kwargs['stdout'] = kwargs.get('stdout', subprocess.PIPE) - kwargs['stderr'] = kwargs.get('stderr', subprocess.STDOUT) + kwargs['stderr'] = kwargs.get('stderr', subprocess.PIPE) kwargs['stdin'] = kwargs.get('stdin', subprocess.PIPE) kwargs['env'] = filtered_env proc = subprocess.Popen(*args, **kwargs) - output, error = proc.communicate() + full_output = "" + _stdout, _stderr = proc.communicate() + if _stdout: + _stdout = _stdout.decode("utf-8") + full_output += _stdout + logger.debug(_stdout) - if output: - output = output.decode("utf-8") - output += "\n" - for line in output.strip().split("\n"): - log.info(line) - - if error: - error = error.decode("utf-8") - error += "\n" - for line in error.strip().split("\n"): - log.error(line) + if _stderr: + _stderr = _stderr.decode("utf-8") + # Add additional line break if output already containt stdout + if full_output: + full_output += "\n" + full_output += _stderr + logger.warning(_stderr) if proc.returncode != 0: - raise ValueError( - "\"{}\" was not successful:\nOutput: {}\nError: {}".format( - args, output, error)) - return output + exc_msg = "Executing arguments was not successful: \"{}\"".format(args) + if _stdout: + exc_msg += "\n\nOutput:\n{}".format(_stdout) + + if _stderr: + exc_msg += "Error:\n{}".format(_stderr) + + raise RuntimeError(exc_msg) + + return full_output