@ -6,9 +6,10 @@ from typing import List, Union
class BashProcess :
class BashProcess :
""" Executes bash commands and returns the output. """
""" Executes bash commands and returns the output. """
def __init__ ( self , strip_newlines : bool = False ):
def __init__ ( self , strip_newlines : bool = False , return_err_output : bool = False ):
""" Initialize with stripping newlines. """
""" Initialize with stripping newlines. """
self . strip_newlines = strip_newlines
self . strip_newlines = strip_newlines
self . return_err_output = return_err_output
def run ( self , commands : Union [ str , List [ str ] ] ) - > str :
def run ( self , commands : Union [ str , List [ str ] ] ) - > str :
""" Run commands and return final output. """
""" Run commands and return final output. """
@ -16,8 +17,16 @@ class BashProcess:
commands = [ commands ]
commands = [ commands ]
commands = " ; " . join ( commands )
commands = " ; " . join ( commands )
try :
try :
output = subprocess . check_output ( commands , shell = True ) . decode ( )
output = subprocess . run (
commands ,
shell = True ,
check = True ,
stdout = subprocess . PIPE ,
stderr = subprocess . STDOUT ,
) . stdout . decode ( )
except subprocess . CalledProcessError as error :
except subprocess . CalledProcessError as error :
if self . return_err_output :
return error . stdout . decode ( )
return str ( error )
return str ( error )
if self . strip_newlines :
if self . strip_newlines :
output = output . strip ( )
output = output . strip ( )