Skip to content

Commit

Permalink
run: allow stdin kwarg to be set. add new props. update logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
naisanzaa committed Jan 2, 2024
1 parent da59ce8 commit ee8f579
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
2 changes: 2 additions & 0 deletions automon/helpers/subprocessWrapper/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class NotSupportedCommand(Exception):
pass
90 changes: 68 additions & 22 deletions automon/helpers/subprocessWrapper/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from automon import log
from automon.helpers.dates import Dates

from .exceptions import *

logger = log.logging.getLogger(__name__)
logger.setLevel(log.DEBUG)

Expand All @@ -18,8 +20,8 @@ def __init__(self, command: str = None, *args, **kwargs):
self.last_run = None
self.command = ''

self.stdout = b''
self.stderr = b''
self._stdout = b''
self._stderr = b''

self.call = None
self.returncode = None
Expand All @@ -42,23 +44,49 @@ def print(self):
return print(self.stdout.decode())

def set_command(self, command: str) -> bool:
logger.debug(command)
if command:
self.command = command
return True
return False

@property
def stdout(self):
return self._stdout

@property
def stdout_lines(self):
return self.stdout.decode().splitlines()

@property
def stderr(self):
return self._stderr

@property
def stderr_lines(self):
return self.stderr.decode().splitlines()

def which(self, program: str, *args, **kwargs) -> bool:
"""runs which
:param program:
:return:
"""
logger.debug(str(dict(
program=program,
args=args,
kwargs=kwargs,
)))
if program:
return self.run(command=f'which {program}', *args, **kwargs)
return False

def run_command(self, *args, **kwargs) -> bool:
"""alias to run"""
logger.debug(str(dict(
args=args,
kwargs=kwargs,
)))
return self.run(*args, **kwargs)

def run(self, command: str = None,
Expand All @@ -68,17 +96,23 @@ def run(self, command: str = None,
sanitize_command: bool = True,
**kwargs) -> bool:

if command:
if sanitize_command:
command = self._command(command)
logger.debug(str(dict(
command=command,
text=text,
inBackground=inBackground,
shell=shell,
sanitize_command=sanitize_command,
kwargs=kwargs,
)))

if command and sanitize_command:
command = self._command(command)

elif self.command:
command = self.command
if sanitize_command:
command = self._command(self.command)

logger.debug(f'[command] {command}')

try:
if inBackground:
if 'text' in dir(subprocess.Popen):
Expand All @@ -90,7 +124,6 @@ def run(self, command: str = None,
if 'text' in dir(subprocess.Popen):
self.call = subprocess.Popen(
command,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
text=text,
Expand All @@ -99,7 +132,6 @@ def run(self, command: str = None,
else:
self.call = subprocess.Popen(
command,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
shell=shell,
Expand All @@ -111,40 +143,54 @@ def run(self, command: str = None,
timestamp = Dates.iso()

self.last_run = timestamp
self.stdout = stdout
self.stderr = stderr
self._stdout = stdout
self._stderr = stderr
self.returncode = self.call.returncode

if self.stdout:
logger.debug(f'[stdout] {stdout}')

if self.stderr:
logger.error(f'[stderr] {stderr}')

if self.returncode == 0:
logger.debug(str(dict(
stdout_KB=round(len(self.stdout) / 1024, 2),
stderr_KB=round(len(self.stderr) / 1024, 2),
)))
return True

except Exception as e:
self.stderr = f'{e}'.encode()
logger.error(f'{e}')
except Exception as error:
self._stderr = f'{error}'.encode()
logger.error(f'{error}')
raise RuntimeError(error)

logger.error(str(dict(
stdout_KB=round(len(self.stdout) / 1024, 2),
stderr_KB=round(len(self.stderr) / 1024, 2),
)))
return False

def _command(self, command: str) -> list:
self.command = command

if isinstance(command, str):
split_command = f'{command}'.split(' ')
split_command = [str(x).strip() for x in split_command]
split_command = [x for x in split_command if x]
self.command = split_command

for arg in split_command:
if '|' in arg:
logger.warning(f'Pipes are not supported! {split_command}')
error = f'Pipes are not supported! {split_command}'
logger.error(error)
raise NotSupportedCommand(error)

logger.debug(str(dict(
command=self.command
)))
return self.command

def __repr__(self) -> str:
return f'{self.command} stderr: ({len(self.stderr) / 1024} Kb) stdout: ({round(len(self.stdout) / 1024, 2)} Kb)'
return str(dict(
command=self.command,
stdout=f'{round(len(self.stdout) / 1024, 2)} KB',
stderr=f'{round(len(self.stderr) / 1024, 2)} KB',
))

def __len__(self):
return sum([len(self.stdout), len(self.stderr)])
Expand Down

0 comments on commit ee8f579

Please sign in to comment.