Skip to content

Commit

Permalink
Migrated from subprocess.run -> subprocess.popen. Requested by @ulmitov
Browse files Browse the repository at this point in the history
… in Issue #13
  • Loading branch information
ralequi committed May 8, 2023
1 parent 62c5209 commit 639f27d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions pystorcli2/cmdRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():

This comment has been minimized.

Copy link
@ulmitov

ulmitov May 11, 2023

this one is same like subprocess.CompletedProcess

This comment has been minimized.

Copy link
@ralequi

ralequi May 11, 2023

Author Member

Yeah, it's almost the same

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()]

This comment has been minimized.

Copy link
@ulmitov

ulmitov May 10, 2023

if you can please make this runner same for pystorcli and pysmart :)


return StorcliRet(_stdout, _stderr, proc.returncode)

def binaryCheck(self, binary) -> str:
"""Verify and return full binary path
Expand Down
6 changes: 4 additions & 2 deletions pystorcli2/storcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/storclifile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
"""

Expand All @@ -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

Expand Down

0 comments on commit 639f27d

Please sign in to comment.