imperva / incapsula-logs-downloader

A Python script for downloading log files from Incapsula
MIT License
30 stars 35 forks source link

possible flaw in "with open" - in HandlingLogs.py #78

Closed AVitg closed 7 months ago

AVitg commented 7 months ago

in HandlingLogs.py (see code below), there might be a logical flaw, that only on windows shows up (in the logs), my assumption is, that it has to do with below lines:

my assumption is, windows tries to delete the file, while it is still open. error looks like this:

Thread-5 (worker) ERROR Deleting file C:\incapsula\var\process\123_1234567890.log - [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\incapsula\\var\\process\\123_1234567890.log'

any suggestions on a fix?

   def send_file(self, file) -> tuple:
        if not os.path.isfile(os.path.join(self.config.PROCESS_DIR, file)) and not self.SEND_GOOD:
            return False, file
        else:
            file_path = os.path.join(self.config.PROCESS_DIR, file)
            with open(file_path, "r") as fp:
                try:
                    # Get all the lines from the file
                    messages = fp.readlines()
                    if not len(messages) > 0:
                        self.logger.warning("No messages added for {}".format(file))
                        return False, file
                    if self.remote_logger is not None:
                        self.logger.info("Number of messages added: {}".format(len(messages)))
                        # Sent the array of message to the remote logger
                        if self.remote_logger.send(messages):
                            # Archive the log if sent successfully
                            if bool(self.config.ARCHIVE_DIR):
                                self.archive_log(file_path, file)
                            else:
                                self.delete_log(file_path)
                            return True, file
AVitg commented 7 months ago

seems this "change" works, but i have no idea, if this is the way it should be done in python....

                            if bool(self.config.ARCHIVE_DIR):
                                self.archive_log(file_path, file)
                            else:
                                fp.close()
                                self.delete_log(file_path)
                            return True, file
joeymoore commented 7 months ago

This is a problem and I will resolve this ASAP. Thank you @AVitg

joeymoore commented 7 months ago

Please test @AVitg

AVitg commented 7 months ago

as I am running a dev version or that file, i just manually merged the changes... i would say it works THANKS