@@ -202,22 +202,21 @@ def fileprefix_from_cmd(cmd, allowed_chars=False):
202202 return '' .join ([c for c in cmd if c in allowed_chars ])
203203
204204
205- def save_cmd (cmd_str , work_dir , env ):
206- cmd_name = fileprefix_from_cmd (os .path .basename (cmd_str .split (' ' )[0 ]))
205+ def save_cmd (cmd_str , work_dir , env , filename ):
207206 full_env = os .environ .copy ()
208207 if env is not None :
209208 full_env .update (env )
210209
211- with tempfile . NamedTemporaryFile ( prefix = f" { cmd_name } -" , suffix = ".sh" , delete = False ) as fid :
212- fid .write ('\n ' .join (f'{ key } ={ shlex .quote (value )} ' for key , value in full_env .items ()). encode ( 'utf-8' ) )
210+ with open ( filename , 'w' ) as fid :
211+ fid .write ('\n ' .join (f'{ key } ={ shlex .quote (value )} ' for key , value in full_env .items ()))
213212 fid .write ('\n ' .join ([
214213 f'cd "{ work_dir } "' ,
215214 f'history -s "{ shlex .quote (cmd_str )} "' ,
216215 f'echo Shell for the command: "{ shlex .quote (cmd_str )} "' ,
217216 'echo Use command history, exit to stop' ,
218217 'export PS1="eb-shell> $PS1"' ,
219218 'bash' ,
220- ]). encode ( 'utf-8' ))
219+ ])
221220
222221
223222@run_shell_cmd_cache
@@ -288,11 +287,14 @@ def to_cmd_str(cmd):
288287 os .makedirs (toptmpdir , exist_ok = True )
289288 cmd_name = fileprefix_from_cmd (os .path .basename (cmd_str .split (' ' )[0 ]))
290289 tmpdir = tempfile .mkdtemp (dir = toptmpdir , prefix = f'{ cmd_name } -' )
290+ cmd_log_fp = os .path .join (tmpdir , 'cmd.sh' )
291+ _log .info (f'run_shell_cmd: command environment of "{ cmd_str } " will be saved to { cmd_log_fp } ' )
292+ save_cmd (cmd_str , work_dir , env , cmd_log_fp )
291293 cmd_out_fp = os .path .join (tmpdir , 'out.txt' )
292- _log .info (f'run_cmd : Output of "{ cmd_str } " will be logged to { cmd_out_fp } ' )
294+ _log .info (f'run_shell_cmd : Output of "{ cmd_str } " will be logged to { cmd_out_fp } ' )
293295 if split_stderr :
294296 cmd_err_fp = os .path .join (tmpdir , 'err.txt' )
295- _log .info (f'run_cmd : Errors and warnings of "{ cmd_str } " will be logged to { cmd_err_fp } ' )
297+ _log .info (f'run_shell_cmd : Errors and warnings of "{ cmd_str } " will be logged to { cmd_err_fp } ' )
296298 else :
297299 cmd_err_fp = None
298300 else :
@@ -341,7 +343,6 @@ def to_cmd_str(cmd):
341343 log_msg += f" (via thread with ID { thread_id } )"
342344 _log .info (log_msg )
343345
344- save_cmd (cmd_str , work_dir , env )
345346 proc = subprocess .Popen (cmd , stdout = subprocess .PIPE , stderr = stderr , stdin = subprocess .PIPE ,
346347 cwd = work_dir , env = env , shell = shell , executable = executable )
347348
0 commit comments