From 2666eae65683d37792004b0189819b637eeea1f3 Mon Sep 17 00:00:00 2001 From: Nivin Srinivas Date: Sat, 10 Feb 2024 09:36:32 +0530 Subject: [PATCH 1/8] Add raise_error option to run and run_and_log methods in process.py --- meltano/edk/process.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 17aaf84..9110b39 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -60,6 +60,7 @@ def run( stdout: None | int | t.IO = subprocess.PIPE, stderr: None | int | t.IO = subprocess.PIPE, text: bool = True, + raise_error: bool = True, **kwargs: t.Any, ) -> subprocess.CompletedProcess: """Run a subprocess. Simple wrapper around subprocess.run. @@ -75,7 +76,7 @@ def run( to override these you're likely better served using `subprocess.run` directly. Lastly note that this method is blocking AND `subprocess.run` is called with - `check=True`. This means that if the subprocess fails a `CalledProcessError` + `check=True` and `raise_error=True`. This means that if the subprocess fails a `CalledProcessError` will be raised. Args: @@ -83,6 +84,7 @@ def run( stdout: The stdout stream to use. stderr: The stderr stream to use. text: If true, decode stdin, stdout and stderr using the system default. + raise_error: If true, decode stdin, stdout and stderr using the system default. **kwargs: Additional keyword arguments to pass to subprocess.run. Returns: @@ -96,6 +98,7 @@ def run( stderr=stderr, check=True, text=text, + raise_error=raise_error **kwargs, ) @@ -162,6 +165,7 @@ async def _exec( def run_and_log( self, sub_command: str | None = None, + raise_error: bool = True, *args: ExecArg, ) -> None: """Run a subprocess and stream the output to the logger. @@ -171,13 +175,20 @@ def run_and_log( Args: sub_command: The subcommand to run. + raise_error: If True (default), raises a CalledProcessError if the subprocess fails. *args: The arguments to pass to the subprocess. Raises: CalledProcessError: If the subprocess failed. """ - result = asyncio.run(self._exec(sub_command, *args)) - if result.returncode: - raise subprocess.CalledProcessError( - result.returncode, cmd=self.bin, stderr=None - ) + try: + result = asyncio.run(self._exec(sub_command, raise_error, *args)) + if result.returncode and raise_error: + raise subprocess.CalledProcessError( + result.returncode, cmd=self.bin, stderr=None + ) + except subprocess.CalledProcessError as e: + if raise_error: + raise + else: + log_subprocess_error(self.bin, e, "Error running subprocess") From ad9f9d74ce8ed0b9bc896ea226123119e9313fe2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Feb 2024 04:08:28 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- meltano/edk/process.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 9110b39..8e80a9d 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -84,7 +84,7 @@ def run( stdout: The stdout stream to use. stderr: The stderr stream to use. text: If true, decode stdin, stdout and stderr using the system default. - raise_error: If true, decode stdin, stdout and stderr using the system default. + raise_error: If true, decode stdin, stdout and stderr using the system default. **kwargs: Additional keyword arguments to pass to subprocess.run. Returns: @@ -98,8 +98,7 @@ def run( stderr=stderr, check=True, text=text, - raise_error=raise_error - **kwargs, + raise_error=raise_error**kwargs, ) @staticmethod @@ -175,13 +174,13 @@ def run_and_log( Args: sub_command: The subcommand to run. - raise_error: If True (default), raises a CalledProcessError if the subprocess fails. + raise_error: If True (default), raises a CalledProcessError if the subprocess fails. *args: The arguments to pass to the subprocess. Raises: CalledProcessError: If the subprocess failed. """ - try: + try: result = asyncio.run(self._exec(sub_command, raise_error, *args)) if result.returncode and raise_error: raise subprocess.CalledProcessError( From 4a47352cdca25486fb688c69fad383230f55d3ba Mon Sep 17 00:00:00 2001 From: Nivin Srinivas Date: Sat, 10 Feb 2024 09:45:51 +0530 Subject: [PATCH 3/8] Fix missing delimiter in return --- meltano/edk/process.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 8e80a9d..fe081aa 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -98,7 +98,8 @@ def run( stderr=stderr, check=True, text=text, - raise_error=raise_error**kwargs, + raise_error=raise_error, + **kwargs, ) @staticmethod From 7800d63f20faa47f29142c744bbc410ec080f1a1 Mon Sep 17 00:00:00 2001 From: Nivin Srinivas Date: Sat, 10 Feb 2024 10:20:00 +0530 Subject: [PATCH 4/8] Removed raise_error in _exec method --- meltano/edk/process.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index fe081aa..208f7f4 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -98,7 +98,6 @@ def run( stderr=stderr, check=True, text=text, - raise_error=raise_error, **kwargs, ) @@ -182,7 +181,7 @@ def run_and_log( CalledProcessError: If the subprocess failed. """ try: - result = asyncio.run(self._exec(sub_command, raise_error, *args)) + result = asyncio.run(self._exec(sub_command, *args)) if result.returncode and raise_error: raise subprocess.CalledProcessError( result.returncode, cmd=self.bin, stderr=None From 96bf2a722af422a5e83f710eb414e94d986842e2 Mon Sep 17 00:00:00 2001 From: Nivin Srinivas Date: Sat, 10 Feb 2024 10:27:33 +0530 Subject: [PATCH 5/8] flake8 fixes --- meltano/edk/process.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 208f7f4..7361510 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -76,15 +76,16 @@ def run( to override these you're likely better served using `subprocess.run` directly. Lastly note that this method is blocking AND `subprocess.run` is called with - `check=True` and `raise_error=True`. This means that if the subprocess fails a `CalledProcessError` - will be raised. + `check=True` and `raise_error=True`. This means that if the subprocess + fails a `CalledProcessError` will be raised. Args: *args: The arguments to pass to the subprocess. stdout: The stdout stream to use. stderr: The stderr stream to use. text: If true, decode stdin, stdout and stderr using the system default. - raise_error: If true, decode stdin, stdout and stderr using the system default. + raise_error: If true, decode stdin, stdout and stderr using the + system default. **kwargs: Additional keyword arguments to pass to subprocess.run. Returns: @@ -174,7 +175,8 @@ def run_and_log( Args: sub_command: The subcommand to run. - raise_error: If True (default), raises a CalledProcessError if the subprocess fails. + raise_error: If True (default), raises a CalledProcessError if + the subprocess fails. *args: The arguments to pass to the subprocess. Raises: From 52495f83f27998ac223f9fb4878aada15f943327 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Feb 2024 04:57:46 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- meltano/edk/process.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 7361510..8cb9fe9 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -76,7 +76,7 @@ def run( to override these you're likely better served using `subprocess.run` directly. Lastly note that this method is blocking AND `subprocess.run` is called with - `check=True` and `raise_error=True`. This means that if the subprocess + `check=True` and `raise_error=True`. This means that if the subprocess fails a `CalledProcessError` will be raised. Args: @@ -84,7 +84,7 @@ def run( stdout: The stdout stream to use. stderr: The stderr stream to use. text: If true, decode stdin, stdout and stderr using the system default. - raise_error: If true, decode stdin, stdout and stderr using the + raise_error: If true, decode stdin, stdout and stderr using the system default. **kwargs: Additional keyword arguments to pass to subprocess.run. @@ -175,7 +175,7 @@ def run_and_log( Args: sub_command: The subcommand to run. - raise_error: If True (default), raises a CalledProcessError if + raise_error: If True (default), raises a CalledProcessError if the subprocess fails. *args: The arguments to pass to the subprocess. From 10f38ce4188c82b6ee5ea28a01caa1da92df0dd9 Mon Sep 17 00:00:00 2001 From: Nivin Srinivas Date: Sat, 10 Feb 2024 10:46:09 +0530 Subject: [PATCH 7/8] Else block for raise_error=False --- meltano/edk/process.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index 8cb9fe9..c42b2f4 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -182,14 +182,14 @@ def run_and_log( Raises: CalledProcessError: If the subprocess failed. """ - try: - result = asyncio.run(self._exec(sub_command, *args)) - if result.returncode and raise_error: - raise subprocess.CalledProcessError( - result.returncode, cmd=self.bin, stderr=None - ) - except subprocess.CalledProcessError as e: - if raise_error: - raise - else: - log_subprocess_error(self.bin, e, "Error running subprocess") + + result = asyncio.run(self._exec(sub_command, *args)) + if result.returncode and raise_error: + raise subprocess.CalledProcessError( + result.returncode, cmd=self.bin, stderr=None + ) + elif result.returncode and not raise_error: + log_subprocess_error(self.bin, subprocess.CalledProcessError( + result.returncode, cmd=self.bin, stderr=None), + "Error running subprocess" + ) From d3460769a240f9012c3d42d9cf972aed9d22f544 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Feb 2024 05:16:32 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- meltano/edk/process.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/meltano/edk/process.py b/meltano/edk/process.py index c42b2f4..cb97c0a 100644 --- a/meltano/edk/process.py +++ b/meltano/edk/process.py @@ -182,14 +182,16 @@ def run_and_log( Raises: CalledProcessError: If the subprocess failed. """ - result = asyncio.run(self._exec(sub_command, *args)) if result.returncode and raise_error: raise subprocess.CalledProcessError( result.returncode, cmd=self.bin, stderr=None ) elif result.returncode and not raise_error: - log_subprocess_error(self.bin, subprocess.CalledProcessError( - result.returncode, cmd=self.bin, stderr=None), - "Error running subprocess" + log_subprocess_error( + self.bin, + subprocess.CalledProcessError( + result.returncode, cmd=self.bin, stderr=None + ), + "Error running subprocess", )