@@ -64,47 +64,55 @@ def _process_output(encoding, temp_file_path):
6464output = output .decode (encoding )
6565return output ,None # In Windows stderr writing in stdout
6666
67- def _run_command (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ):
68- """Execute a command and return the process and its output."""
69- if os .name == 'nt' and stdout is None :# Windows
70- with tempfile .NamedTemporaryFile (mode = 'w+b' ,delete = False )as temp_file :
71- stdout = temp_file
72- stderr = subprocess .STDOUT
73- process = subprocess .Popen (
74- cmd ,
75- shell = shell ,
76- stdin = stdin or subprocess .PIPE if input is not None else None ,
77- stdout = stdout ,
78- stderr = stderr ,
79- )
80- if get_process :
81- return process ,None ,None
82- temp_file_path = temp_file .name
83-
84- # Wait process finished
85- process .wait ()
86-
87- output ,error = self ._process_output (encoding ,temp_file_path )
88- return process ,output ,error
89- else :# Other OS
67+ def _run_command__nt (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ):
68+ with tempfile .NamedTemporaryFile (mode = 'w+b' ,delete = False )as temp_file :
69+ stdout = temp_file
70+ stderr = subprocess .STDOUT
9071process = subprocess .Popen (
9172cmd ,
9273shell = shell ,
9374stdin = stdin or subprocess .PIPE if input is not None else None ,
94- stdout = stdout or subprocess . PIPE ,
95- stderr = stderr or subprocess . PIPE ,
75+ stdout = stdout ,
76+ stderr = stderr ,
9677 )
9778if get_process :
9879return process ,None ,None
99- try :
100- output ,error = process .communicate (input = input .encode (encoding )if input else None ,timeout = timeout )
101- if encoding :
102- output = output .decode (encoding )
103- error = error .decode (encoding )
104- return process ,output ,error
105- except subprocess .TimeoutExpired :
106- process .kill ()
107- raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
80+ temp_file_path = temp_file .name
81+
82+ # Wait process finished
83+ process .wait ()
84+
85+ output ,error = self ._process_output (encoding ,temp_file_path )
86+ return process ,output ,error
87+
88+ def _run_command__generic (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ):
89+ process = subprocess .Popen (
90+ cmd ,
91+ shell = shell ,
92+ stdin = stdin or subprocess .PIPE if input is not None else None ,
93+ stdout = stdout or subprocess .PIPE ,
94+ stderr = stderr or subprocess .PIPE ,
95+ )
96+ if get_process :
97+ return process ,None ,None
98+ 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
104+ except subprocess .TimeoutExpired :
105+ process .kill ()
106+ raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
107+
108+ def _run_command (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding ):
109+ """Execute a command and return the process and its output."""
110+ if os .name == 'nt' and stdout is None :# Windows
111+ method = __class__ ._run_command__nt
112+ else :# Other OS
113+ method = __class__ ._run_command__generic
114+
115+ return method (self ,cmd ,shell ,input ,stdin ,stdout ,stderr ,get_process ,timeout ,encoding )
108116
109117def exec_command (self ,cmd ,wait_exit = False ,verbose = False ,expect_error = False ,encoding = None ,shell = False ,
110118text = False ,input = None ,stdin = None ,stdout = None ,stderr = None ,get_process = False ,timeout = None ,