From 639f27d1452492f74631251d8e3acae6a1408589 Mon Sep 17 00:00:00 2001 From: Rafael Leira Date: Mon, 8 May 2023 12:30:45 +0200 Subject: [PATCH] Migrated from subprocess.run -> subprocess.popen. Requested by @ulmitov in Issue #13 --- CHANGELOG.md | 4 ++++ pystorcli2/cmdRunner.py | 18 +++++++++++++++--- pystorcli2/storcli.py | 6 ++++-- tests/storclifile.py | 6 +++--- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d5f011..33b4055 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ Version 0.6.4 - [**Drive**] Added some aliases to `drive.state` & `drive.set_state` method. (Issue [#12](https://github.com/Naudit/pystorcli2/issues/12)) - Added enum-class StorcliError. This Enum stores any posible (documented) error reported by storcli. We can expect & catch some of them if we want to +- **Changes**: + ---------- + - [**Storcli**] Migrated from subprocess.run -> subprocess.popen. Requested by @ulmitov in Issue [#13](https://github.com/Naudit/pystorcli2/issues/13) + Thanks to @ulmitov & @dgilbert101 for the contributions to this release Version 0.6.3 diff --git a/pystorcli2/cmdRunner.py b/pystorcli2/cmdRunner.py index 3358270..31c2848 100644 --- a/pystorcli2/cmdRunner.py +++ b/pystorcli2/cmdRunner.py @@ -8,18 +8,30 @@ import os import shutil -import subprocess +from subprocess import Popen, PIPE from . import exc +from typing import List, Tuple + + +class StorcliRet(): + def __init__(self, stdout: str, stderr: str, returncode: int): + self.stdout = stdout + self.stderr = stderr + self.returncode = returncode class CMDRunner(): """This is a simple wrapper for subprocess.Popen()/subprocess.run(). The main idea is to inherit this class and create easy mockable tests. """ - def run(self, args, **kwargs) -> subprocess.CompletedProcess: + def run(self, args, **kwargs) -> StorcliRet: """Runs a command and returns the output. """ - return subprocess.run(args, **kwargs) + proc = Popen(args, stdout=PIPE, stderr=PIPE, **kwargs) + + _stdout, _stderr = [i for i in proc.communicate()] + + return StorcliRet(_stdout, _stderr, proc.returncode) def binaryCheck(self, binary) -> str: """Verify and return full binary path diff --git a/pystorcli2/storcli.py b/pystorcli2/storcli.py index c9acf90..9f89c2d 100644 --- a/pystorcli2/storcli.py +++ b/pystorcli2/storcli.py @@ -216,12 +216,14 @@ def run(self, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, allow_error_ with self.__cache_lock: try: ret = self.__cmdrunner.run( - args=cmd, stdout=stdout, stderr=stderr, universal_newlines=True, **kwargs) + args=cmd, universal_newlines=True, **kwargs) try: ret_json = json.loads(ret.stdout) self.check_response_status( cmd, ret_json, allow_error_codes) - ret.check_returncode() + if ret.returncode != 0: + raise subprocess.CalledProcessError( + ret.returncode, cmd, ret.stdout, ret.stderr) if self.cache_enable: self.__response_cache[cmd_cache_key] = ret_json return ret_json diff --git a/tests/storclifile.py b/tests/storclifile.py index e8a38c0..c05f406 100644 --- a/tests/storclifile.py +++ b/tests/storclifile.py @@ -10,7 +10,7 @@ import re import os import subprocess -from pystorcli2.cmdRunner import CMDRunner +from pystorcli2.cmdRunner import CMDRunner, StorcliRet from .exceptions import StorclifileSampleNotFound from typing import Union, Tuple, List @@ -28,7 +28,7 @@ def __init__(self, storcli_path, options: List[str] = []): self.storcli_path = storcli_path self.options: List[str] = options - def run(self, args, pass_options=False, **kwargs) -> subprocess.CompletedProcess: + def run(self, args, pass_options=False, **kwargs) -> StorcliRet: """Runs a command and returns the output. """ @@ -54,7 +54,7 @@ def run(self, args, pass_options=False, **kwargs) -> subprocess.CompletedProcess _stdout = raw_data - ret = subprocess.CompletedProcess(args, 0, _stdout, None) + ret = StorcliRet(_stdout, '', 0) return ret