From 8e9727364c6ee0b567bd8a9a0c140d03509420d2 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Wed, 20 Mar 2024 12:19:07 +0200 Subject: [PATCH] Updated for written log --- slpkg/multi_process.py | 59 ++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/slpkg/multi_process.py b/slpkg/multi_process.py index ec411994..7c5e4886 100644 --- a/slpkg/multi_process.py +++ b/slpkg/multi_process.py @@ -22,6 +22,10 @@ class MultiProcess(Configs): self.ascii = AsciiBox() self.errors = Errors() + self.timestamp: str = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + self.head_message: str = f'Timestamp: {self.timestamp}' + self.bottom_message: str = 'EOF - End of log file' + self.option_for_silent: bool = self.utils.is_option( ('-n', '--silent'), flags) @@ -71,45 +75,54 @@ class MultiProcess(Configs): Returns: None. """ - timestamp: str = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - head_message: str = f'Timestamp: {timestamp}' - bottom_message: str = 'EOF - End of log file' - process = subprocess.Popen(command, shell=True, stdout=stdout, stderr=stderr, text=True) - # Write the timestamp at the head of the log file. - with open(self.slpkg_log_file, 'a') as log: - log.write(f"{len(head_message) * '='}\n") - log.write(f'{head_message}\n') - log.write(f"{len(head_message) * '='}\n") + self._write_log_head() # Write the process to the log file and to the terminal. with process.stdout as output: for i, line in enumerate(output): if not self.silent_mode and not self.option_for_silent: print(line.strip()) # Print to console - with open(self.slpkg_log_file, 'a') as log: - log.write(line) # Write to log file + if self.process_log: + with open(self.slpkg_log_file, 'a') as log: + log.write(line) # Write to log file - # Write the bottom of the log file. - with open(self.slpkg_log_file, 'a') as log: - log.write(f"\n{len(bottom_message) * '='}\n") - log.write(f'{bottom_message}\n') - log.write(f"{len(bottom_message) * '='}\n\n") + self._write_log_eof() process.wait() # Wait for the process to finish # If the process failed, return exit code. if process.returncode != 0: - if not self.silent_mode and not self.option_for_silent: - message: str = f'Error occurred with process. Please check the log file.' - print() - print(len(message) * '=') - print(f'{self.bred}{message}{self.endc}') - print(len(message) * '=') - print() + self._error_process() raise SystemExit(process.returncode) + def _error_process(self): + """ Prints error message for process. """ + if not self.silent_mode and not self.option_for_silent: + message: str = f'Error occurred with process. Please check the log file.' + print() + print(len(message) * '=') + print(f'{self.bred}{message}{self.endc}') + print(len(message) * '=') + print() + + def _write_log_head(self) -> None: + """ Write the timestamp at the head of the log file. """ + if self.process_log: + with open(self.slpkg_log_file, 'a') as log: + log.write(f"{len(self.head_message) * '='}\n") + log.write(f'{self.head_message}\n') + log.write(f"{len(self.head_message) * '='}\n") + + def _write_log_eof(self) -> None: + """ Write the bottom of the log file. """ + if self.process_log: + with open(self.slpkg_log_file, 'a') as log: + log.write(f"\n{len(self.bottom_message) * '='}\n") + log.write(f'{self.bottom_message}\n') + log.write(f"{len(self.bottom_message) * '='}\n\n") + def process(self, command: str, stderr=None, stdout=None) -> None: """ Build the package and write a log file.