Change from two separate logs to one

pull/14/head
Dan Puttick 2017-03-18 00:10:17 -04:00
parent 51760ebbb1
commit 6851461755
3 changed files with 15 additions and 14 deletions

View File

@ -190,7 +190,8 @@ class File(FileBase):
self.mime_processing_options.get(self.main_type, self.unknown)() self.mime_processing_options.get(self.main_type, self.unknown)()
def write_log(self): def write_log(self):
pass props = self.get_all_props()
self.logger.add_file(props)
# ##### Helper functions ##### # ##### Helper functions #####
def _make_method_dict(self, list_of_tuples): def _make_method_dict(self, list_of_tuples):
@ -441,8 +442,7 @@ class File(FileBase):
# LOG: handle metadata # LOG: handle metadata
self.set_property('metadata', 'png') self.set_property('metadata', 'png')
img.close() img.close()
# Catch decompression bombs except Exception as e: # Catch decompression bombs
except Exception as e:
# TODO: only catch DecompressionBombWarnings here? # TODO: only catch DecompressionBombWarnings here?
self.add_error(e, "Caught exception processing metadata for {}".format(self.src_path)) self.add_error(e, "Caught exception processing metadata for {}".format(self.src_path))
self.make_dangerous('exception processing metadata') self.make_dangerous('exception processing metadata')
@ -548,7 +548,6 @@ class KittenGroomerFileCheck(KittenGroomerBase):
to an archive. to an archive.
""" """
self.recursive_archive_depth += 1 self.recursive_archive_depth += 1
# LOG: write_log or somehow log the archive file here
if self.recursive_archive_depth >= self.max_recursive_depth: if self.recursive_archive_depth >= self.max_recursive_depth:
file.make_dangerous('Archive bomb') file.make_dangerous('Archive bomb')
else: else:

View File

@ -14,7 +14,7 @@ import shutil
import argparse import argparse
import magic import magic
import twiggy # import twiggy
class KittenGroomerError(Exception): class KittenGroomerError(Exception):
@ -41,7 +41,6 @@ class FileBase(object):
""" """
Initialized with the source path and expected destination path. 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. Create various properties and determine the file's mimetype.
""" """
self.src_path = src_path self.src_path = src_path
@ -181,6 +180,10 @@ class FileBase(object):
else: else:
return None 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): def add_error(self, error, info):
"""Add an error: info pair to _file_props['errors'].""" """Add an error: info pair to _file_props['errors']."""
self._file_props['errors'].update({error: info}) self._file_props['errors'].update({error: info})
@ -267,10 +270,9 @@ class GroomerLogger(object):
def __init__(self, root_dir_path, debug=False): def __init__(self, root_dir_path, debug=False):
self.root_dir = root_dir_path self.root_dir = root_dir_path
self.log_dir_path = self._make_log_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_path = os.path.join(self.log_dir_path, 'log.txt')
self.log_content = os.path.join(self.log_dir_path, 'content.log') # twiggy.quick_setup(file=self.log_processing)
twiggy.quick_setup(file=self.log_processing) # self.log = twiggy.log.name('files')
self.log = twiggy.log.name('files')
if debug: if debug:
self.log_debug_err = os.path.join(self.log_dir_path, 'debug_stderr.log') 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') 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=' '): def tree(self, base_dir, padding=' '):
"""Write a graphical tree to the log for `base_dir`.""" """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('#' * 80 + '\n', 'UTF-8'))
lf.write(bytes('{}+- {}/\n'.format(padding, os.path.basename(os.path.abspath(base_dir)).encode()), 'utf8')) lf.write(bytes('{}+- {}/\n'.format(padding, os.path.basename(os.path.abspath(base_dir)).encode()), 'utf8'))
padding += '| ' padding += '| '
@ -312,8 +314,8 @@ class GroomerLogger(object):
s.update(buf) s.update(buf)
return s.hexdigest() return s.hexdigest()
def add_file(self, file): def add_file(self, file_props):
"""Add a file to the log.""" """Add a file to the log. Takes a dict of file properties."""
pass pass

View File

@ -6,7 +6,7 @@ def save_logs(groomer, test_description):
test_log_path = 'tests/test_logs/{}.log'.format(test_description) test_log_path = 'tests/test_logs/{}.log'.format(test_description)
with open(test_log_path, 'w+') as test_log: with open(test_log_path, 'w+') as test_log:
test_log.write(divider.format('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() log = logfile.read()
test_log.write(log) test_log.write(log)
if os.path.exists(groomer.logger.log_debug_err): if os.path.exists(groomer.logger.log_debug_err):