@@ -307,21 +307,7 @@ def execute(self) -> bool:
307307
308308 # https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
309309 if not self .pid :
310- history_logfile = Path (f"{ storage ['LOG_PATH' ]} /cmd_history.txt" )
311-
312- change_perm = False
313- if history_logfile .exists () is False :
314- change_perm = True
315-
316- try :
317- with history_logfile .open ("a" ) as cmd_log :
318- cmd_log .write (f"{ time .time ()} { self .cmd } \n " )
319-
320- if change_perm :
321- history_logfile .chmod (stat .S_IRUSR | stat .S_IWUSR | stat .S_IRGRP )
322- except (PermissionError , FileNotFoundError ):
323- # If history_logfile does not exist, ignore the error
324- pass
310+ _log_cmd (self .cmd )
325311
326312 try :
327313 os .execve (self .cmd [0 ], list (self .cmd ), {** os .environ , ** self .environment_vars })
@@ -456,6 +442,29 @@ def trace_log(self) -> bytes | None:
456442 return None
457443
458444
445+ def _log_cmd (cmd : list [str ]) -> None :
446+ history_logfile = Path (f"{ storage ['LOG_PATH' ]} /cmd_history.txt" )
447+
448+ change_perm = False
449+ if history_logfile .exists () is False :
450+ change_perm = True
451+
452+ try :
453+ with history_logfile .open ("a" ) as cmd_log :
454+ cmd_log .write (f"{ time .time ()} { cmd } \n " )
455+
456+ if change_perm :
457+ history_logfile .chmod (stat .S_IRUSR | stat .S_IWUSR | stat .S_IRGRP )
458+ except (PermissionError , FileNotFoundError ):
459+ # If history_logfile does not exist, ignore the error
460+ pass
461+
462+
463+ def run (cmd : list [str ], input_data : bytes | None = None ) -> None :
464+ _log_cmd (cmd )
465+ subprocess .run (cmd , input = input_data , check = True )
466+
467+
459468def _pid_exists (pid : int ) -> bool :
460469 try :
461470 return any (subprocess .check_output (['ps' , '--no-headers' , '-o' , 'pid' , '-p' , str (pid )]).strip ())
0 commit comments