Skip to content

Commit 16405ab

Browse files
authored
Refactor command logs (archlinux#3549)
1 parent c2ea6ff commit 16405ab

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

archinstall/lib/general.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,9 @@ def peak(self, output: str | bytes) -> bool:
237237
except UnicodeDecodeError:
238238
return False
239239

240-
peak_logfile = Path(f'{storage["LOG_PATH"]}/cmd_output.txt')
240+
_cmd_output(output)
241241

242-
change_perm = False
243-
if peak_logfile.exists() is False:
244-
change_perm = True
245-
246-
with peak_logfile.open('a') as peek_output_log:
247-
peek_output_log.write(str(output))
248-
249-
if change_perm:
250-
peak_logfile.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
251-
252-
sys.stdout.write(str(output))
242+
sys.stdout.write(output)
253243
sys.stdout.flush()
254244

255245
return True
@@ -296,7 +286,7 @@ def execute(self) -> bool:
296286

297287
# https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
298288
if not self.pid:
299-
_log_cmd(self.cmd)
289+
_cmd_history(self.cmd)
300290

301291
try:
302292
os.execve(self.cmd[0], list(self.cmd), {**os.environ, **self.environment_vars})
@@ -424,29 +414,36 @@ def trace_log(self) -> bytes | None:
424414
return None
425415

426416

427-
def _log_cmd(cmd: list[str]) -> None:
428-
history_logfile = Path(f'{storage["LOG_PATH"]}/cmd_history.txt')
417+
def _append_log(file: str, content: str) -> None:
418+
path = Path(f'{storage["LOG_PATH"]}/{file}')
429419

430-
change_perm = False
431-
if history_logfile.exists() is False:
432-
change_perm = True
420+
change_perm = not path.exists()
433421

434422
try:
435-
with history_logfile.open('a') as cmd_log:
436-
cmd_log.write(f'{time.time()} {cmd}\n')
423+
with path.open('a') as f:
424+
f.write(content)
437425

438426
if change_perm:
439-
history_logfile.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
427+
path.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
440428
except (PermissionError, FileNotFoundError):
441-
# If history_logfile does not exist, ignore the error
429+
# If the file does not exist, ignore the error
442430
pass
443431

444432

433+
def _cmd_history(cmd: list[str]) -> None:
434+
content = f'{time.time()} {cmd}\n'
435+
_append_log('cmd_history.txt', content)
436+
437+
438+
def _cmd_output(output: str) -> None:
439+
_append_log('cmd_output.txt', output)
440+
441+
445442
def run(
446443
cmd: list[str],
447444
input_data: bytes | None = None,
448445
) -> subprocess.CompletedProcess[bytes]:
449-
_log_cmd(cmd)
446+
_cmd_history(cmd)
450447

451448
return subprocess.run(
452449
cmd,

0 commit comments

Comments
 (0)