From 685146175529f59177dd1d1e83c72c548d1a5232 Mon Sep 17 00:00:00 2001 From: Dan Puttick Date: Sat, 18 Mar 2017 00:10:17 -0400 Subject: [PATCH] Change from two separate logs to one --- bin/filecheck.py | 7 +++---- kittengroomer/helpers.py | 20 +++++++++++--------- tests/logging.py | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bin/filecheck.py b/bin/filecheck.py index 0a9febe..a6fe282 100644 --- a/bin/filecheck.py +++ b/bin/filecheck.py @@ -190,7 +190,8 @@ class File(FileBase): self.mime_processing_options.get(self.main_type, self.unknown)() def write_log(self): - pass + props = self.get_all_props() + self.logger.add_file(props) # ##### Helper functions ##### def _make_method_dict(self, list_of_tuples): @@ -441,8 +442,7 @@ class File(FileBase): # LOG: handle metadata self.set_property('metadata', 'png') img.close() - # Catch decompression bombs - except Exception as e: + except Exception as e: # Catch decompression bombs # TODO: only catch DecompressionBombWarnings here? self.add_error(e, "Caught exception processing metadata for {}".format(self.src_path)) self.make_dangerous('exception processing metadata') @@ -548,7 +548,6 @@ class KittenGroomerFileCheck(KittenGroomerBase): to an archive. """ self.recursive_archive_depth += 1 - # LOG: write_log or somehow log the archive file here if self.recursive_archive_depth >= self.max_recursive_depth: file.make_dangerous('Archive bomb') else: diff --git a/kittengroomer/helpers.py b/kittengroomer/helpers.py index 5fc5d7a..85696a8 100644 --- a/kittengroomer/helpers.py +++ b/kittengroomer/helpers.py @@ -14,7 +14,7 @@ import shutil import argparse import magic -import twiggy +# import twiggy class KittenGroomerError(Exception): @@ -41,7 +41,6 @@ class FileBase(object): """ Initialized with the source path and expected destination path. - self.logger should be a logging object with an add_file method. Create various properties and determine the file's mimetype. """ self.src_path = src_path @@ -181,6 +180,10 @@ class FileBase(object): else: return None + def get_all_props(self): + """Return a dict containing all stored properties of this file.""" + return self._file_props + def add_error(self, error, info): """Add an error: info pair to _file_props['errors'].""" self._file_props['errors'].update({error: info}) @@ -267,10 +270,9 @@ class GroomerLogger(object): def __init__(self, root_dir_path, debug=False): self.root_dir = root_dir_path self.log_dir_path = self._make_log_dir(root_dir_path) - self.log_processing = os.path.join(self.log_dir_path, 'processing.log') - self.log_content = os.path.join(self.log_dir_path, 'content.log') - twiggy.quick_setup(file=self.log_processing) - self.log = twiggy.log.name('files') + self.log_path = os.path.join(self.log_dir_path, 'log.txt') + # twiggy.quick_setup(file=self.log_processing) + # self.log = twiggy.log.name('files') if debug: self.log_debug_err = os.path.join(self.log_dir_path, 'debug_stderr.log') self.log_debug_out = os.path.join(self.log_dir_path, 'debug_stdout.log') @@ -287,7 +289,7 @@ class GroomerLogger(object): def tree(self, base_dir, padding=' '): """Write a graphical tree to the log for `base_dir`.""" - with open(self.log_content, 'ab') as lf: + with open(self.log_path, 'ab') as lf: lf.write(bytes('#' * 80 + '\n', 'UTF-8')) lf.write(bytes('{}+- {}/\n'.format(padding, os.path.basename(os.path.abspath(base_dir)).encode()), 'utf8')) padding += '| ' @@ -312,8 +314,8 @@ class GroomerLogger(object): s.update(buf) return s.hexdigest() - def add_file(self, file): - """Add a file to the log.""" + def add_file(self, file_props): + """Add a file to the log. Takes a dict of file properties.""" pass diff --git a/tests/logging.py b/tests/logging.py index 0b5237d..c06ad94 100644 --- a/tests/logging.py +++ b/tests/logging.py @@ -6,7 +6,7 @@ def save_logs(groomer, test_description): test_log_path = 'tests/test_logs/{}.log'.format(test_description) with open(test_log_path, 'w+') as test_log: test_log.write(divider.format('TEST LOG')) - with open(groomer.logger.log_processing, 'r') as logfile: + with open(groomer.logger.log_path, 'r') as logfile: log = logfile.read() test_log.write(log) if os.path.exists(groomer.logger.log_debug_err):