1111
1212from ..exceptions import ExecUtilException
1313from .os_ops import ConnectionParams , OsOperations , pglib , get_default_encoding
14+ from .raise_error import RaiseError
15+ from .helpers import Helpers
1416
1517try :
1618 from shutil import which as find_executable
@@ -47,14 +49,6 @@ def __init__(self, conn_params=None):
4749 self .remote = False
4850 self .username = conn_params .username or getpass .getuser ()
4951
50- @staticmethod
51- def _raise_exec_exception (message , command , exit_code , output ):
52- """Raise an ExecUtilException."""
53- raise ExecUtilException (message = message .format (output ),
54- command = ' ' .join (command ) if isinstance (command , list ) else command ,
55- exit_code = exit_code ,
56- out = output )
57-
5852 @staticmethod
5953 def _process_output (encoding , temp_file_path ):
6054 """Process the output of a command from a temporary file."""
@@ -65,6 +59,8 @@ def _process_output(encoding, temp_file_path):
6559 return output , None # In Windows stderr writing in stdout
6660
6761 def _run_command__nt (self , cmd , shell , input , stdin , stdout , stderr , get_process , timeout , encoding ):
62+ # TODO: why don't we use the data from input?
63+
6864 with tempfile .NamedTemporaryFile (mode = 'w+b' , delete = False ) as temp_file :
6965 stdout = temp_file
7066 stderr = subprocess .STDOUT
@@ -86,25 +82,36 @@ def _run_command__nt(self, cmd, shell, input, stdin, stdout, stderr, get_process
8682 return process , output , error
8783
8884 def _run_command__generic (self , cmd , shell , input , stdin , stdout , stderr , get_process , timeout , encoding ):
85+ input_prepared = None
86+ if not get_process :
87+ input_prepared = Helpers .PrepareProcessInput (input , encoding ) # throw
88+
89+ assert input_prepared is None or (type (input_prepared ) == bytes ) # noqa: E721
90+
8991 process = subprocess .Popen (
9092 cmd ,
9193 shell = shell ,
9294 stdin = stdin or subprocess .PIPE if input is not None else None ,
9395 stdout = stdout or subprocess .PIPE ,
9496 stderr = stderr or subprocess .PIPE ,
9597 )
98+ assert not (process is None )
9699 if get_process :
97100 return process , None , None
98101 try :
99- output , error = process .communicate (input = input .encode (encoding ) if input else None , timeout = timeout )
100- if encoding :
101- output = output .decode (encoding )
102- error = error .decode (encoding )
103- return process , output , error
102+ output , error = process .communicate (input = input_prepared , timeout = timeout )
104103 except subprocess .TimeoutExpired :
105104 process .kill ()
106105 raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
107106
107+ assert type (output ) == bytes # noqa: E721
108+ assert type (error ) == bytes # noqa: E721
109+
110+ if encoding :
111+ output = output .decode (encoding )
112+ error = error .decode (encoding )
113+ return process , output , error
114+
108115 def _run_command (self , cmd , shell , input , stdin , stdout , stderr , get_process , timeout , encoding ):
109116 """Execute a command and return the process and its output."""
110117 if os .name == 'nt' and stdout is None : # Windows
@@ -120,11 +127,20 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
120127 """
121128 Execute a command in a subprocess and handle the output based on the provided parameters.
122129 """
130+ assert type (expect_error ) == bool # noqa: E721
131+ assert type (ignore_errors ) == bool # noqa: E721
132+
123133 process , output , error = self ._run_command (cmd , shell , input , stdin , stdout , stderr , get_process , timeout , encoding )
124134 if get_process :
125135 return process
126136 if not ignore_errors and ((process .returncode != 0 or has_errors (output = output , error = error )) and not expect_error ):
127- self ._raise_exec_exception ('Utility exited with non-zero code. Error `{}`' , cmd , process .returncode , error or output )
137+ RaiseError .UtilityExitedWithNonZeroCode (
138+ cmd = cmd ,
139+ exit_code = process .returncode ,
140+ msg_arg = error or output ,
141+ error = error ,
142+ out = output
143+ )
128144
129145 if verbose :
130146 return process .returncode , output , error
0 commit comments