Move logging code outside of command execution try-except block (#2739)

This commit is contained in:
codefiles 2024-11-04 06:38:58 -05:00 committed by GitHub
parent 8ec1715eb8
commit 481b570dae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 19 additions and 18 deletions

View File

@ -302,28 +302,29 @@ class SysCommandWorker:
# https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
if not self.pid:
history_logfile = pathlib.Path(f"{storage['LOG_PATH']}/cmd_history.txt")
change_perm = False
if history_logfile.exists() is False:
change_perm = True
try:
change_perm = False
if history_logfile.exists() is False:
change_perm = True
with history_logfile.open("a") as cmd_log:
cmd_log.write(f"{time.time()} {self.cmd}\n")
try:
with history_logfile.open("a") as cmd_log:
cmd_log.write(f"{time.time()} {self.cmd}\n")
if change_perm:
os.chmod(str(history_logfile), stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
except (PermissionError, FileNotFoundError):
# If history_logfile does not exist, ignore the error
pass
except Exception as e:
exception_type = type(e).__name__
error(f"Unexpected {exception_type} occurred in {self.cmd}: {e}")
raise e
if change_perm:
os.chmod(str(history_logfile), stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
except (PermissionError, FileNotFoundError):
# If history_logfile does not exist, ignore the error
pass
except Exception as e:
exception_type = type(e).__name__
error(f"Unexpected {exception_type} occurred in {self.cmd}: {e}")
raise e
if storage.get('arguments', {}).get('debug'):
debug(f"Executing: {self.cmd}")
if storage.get('arguments', {}).get('debug'):
debug(f"Executing: {self.cmd}")
try:
os.execve(self.cmd[0], list(self.cmd), {**os.environ, **self.environment_vars})
except FileNotFoundError:
error(f"{self.cmd[0]} does not exist.")