@@ -39,23 +39,30 @@ def __init__(self, conn_params=None):
39
39
self .username = conn_params .username or self .get_user ()
40
40
41
41
@staticmethod
42
- def _run_command (cmd , shell , input , timeout , encoding , temp_file = None ):
42
+ def _run_command (cmd , shell , input , stdin , stdout , stderr , timeout , encoding , temp_file = None , get_process = None ):
43
43
"""Execute a command and return the process."""
44
44
if temp_file is not None :
45
- stdout = temp_file
46
- stderr = subprocess .STDOUT
45
+ stdout = stdout or temp_file
46
+ stderr = stderr or subprocess .STDOUT
47
47
else :
48
- stdout = subprocess .PIPE
49
- stderr = subprocess .PIPE
48
+ stdout = stdout or subprocess .PIPE
49
+ stderr = stderr or subprocess .PIPE
50
50
51
51
process = subprocess .Popen (
52
52
cmd ,
53
53
shell = shell ,
54
- stdin = subprocess .PIPE if input is not None else None ,
54
+ stdin = stdin or subprocess .PIPE if input is not None else None ,
55
55
stdout = stdout ,
56
56
stderr = stderr ,
57
57
)
58
- return process
58
+
59
+ if get_process :
60
+ return None , process
61
+ try :
62
+ return process .communicate (input = input .encode (encoding ) if input else None , timeout = timeout ), process
63
+ except subprocess .TimeoutExpired :
64
+ process .kill ()
65
+ raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
59
66
60
67
@staticmethod
61
68
def _raise_exec_exception (message , command , exit_code , output ):
@@ -67,7 +74,7 @@ def _raise_exec_exception(message, command, exit_code, output):
67
74
68
75
def exec_command (self , cmd , wait_exit = False , verbose = False ,
69
76
expect_error = False , encoding = None , shell = False , text = False ,
70
- input = None , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ,
77
+ input = None , stdin = None , stdout = None , stderr = None ,
71
78
get_process = None , timeout = None ):
72
79
"""
73
80
Execute a command in a subprocess.
@@ -143,29 +150,27 @@ def _process_output(process, encoding, temp_file=None):
143
150
144
151
def _exec_command_windows (self , cmd , wait_exit = False , verbose = False ,
145
152
expect_error = False , encoding = None , shell = False , text = False ,
146
- input = None , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ,
153
+ input = None , stdin = None , stdout = None , stderr = None ,
147
154
get_process = None , timeout = None ):
148
155
with tempfile .NamedTemporaryFile (mode = 'w+b' ) as temp_file :
149
- process = self ._run_command (cmd , shell , input , timeout , encoding , temp_file )
150
- try :
151
- output = process .communicate (input = input .encode (encoding ) if input else None , timeout = timeout )
152
- except subprocess .TimeoutExpired :
153
- process .kill ()
154
- raise ExecUtilException ("Command timed out after {} seconds." .format (timeout ))
156
+ _ , process = self ._run_command (cmd , shell , input , stdin , stdout , stderr , timeout , encoding , temp_file , get_process )
157
+ if get_process :
158
+ return process
159
+ result = self ._process_output (process , encoding , temp_file )
155
160
156
- if process .returncode != 0 or has_errors (output ):
161
+ if process .returncode != 0 or has_errors (result ):
157
162
if process .returncode == 0 :
158
163
process .returncode = 1
159
164
if expect_error :
160
165
if verbose :
161
- return process .returncode , output , output
166
+ return process .returncode , result , result
162
167
else :
163
- return output
168
+ return result
164
169
else :
165
170
self ._raise_exec_exception ('Utility exited with non-zero code. Error `{}`' , cmd , process .returncode ,
166
- output )
171
+ result )
167
172
168
- return (process .returncode , output , output ) if verbose else output
173
+ return (process .returncode , result , result ) if verbose else result
169
174
170
175
# Environment setup
171
176
def environ (self , var_name ):
0 commit comments