Skip to content

Commit

Permalink
Merge pull request #4 from this-josh/develop
Browse files Browse the repository at this point in the history
felling-v0.1
  • Loading branch information
this-josh authored Mar 8, 2021
2 parents 26acd79 + d6f3518 commit 26e8ad2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
12 changes: 3 additions & 9 deletions felling/src/compare_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


def parse_args(cli_arguments):
# from difflib import get_close_matches

parser = argparse.ArgumentParser(description="Compare two log files")
parser.add_argument(
Expand Down Expand Up @@ -113,14 +112,9 @@ def find_differences(
)
max_lines = min((len(f1_log_messages), len(f2_log_messages)))
else:
max_lines = None

for log_number in range(len(f1_log_messages)):
if max_lines is not None and log_number >= max_lines:
print(
f"Reached line number {log_number} which is the length of the shorter log file, will stop looking for differences."
)
break
max_lines = len(f1_log_messages)

for log_number in range(max_lines):
if f1_log_messages[log_number] != f2_log_messages[log_number]:
num_differences += 1
if to_print:
Expand Down
28 changes: 17 additions & 11 deletions felling/src/configure_felling.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ def _update_filenames(

return config


def _initial_logs():
"""write some initial logs"""
logger.info(f"Username: {getpass.getuser()}")
logger.info(f"Most recent git commit hash: {subprocess.getoutput('git rev-parse HEAD')} ")
logger.info(f"Git remote and branch info: {subprocess.getoutput('git remote show origin')}")
logger.info(
f"Most recent git commit hash: {subprocess.getoutput('git rev-parse HEAD')} "
)
logger.info(
f"Git remote and branch info: {subprocess.getoutput('git remote show origin')}"
)


def _log_versions(packages_to_log: Optional[List[ModuleType]]):
def _log_versions(packages_to_log: Optional[Union[Sequence[ModuleType], ModuleType]]):
"""Use this to log which version of dependent packages are being used"""
if packages_to_log is None:
return
Expand All @@ -62,13 +67,14 @@ def _log_versions(packages_to_log: Optional[List[ModuleType]]):
logger.error(f"packages_to_log = {packages_to_log} and is not iterable")
raise
for pack in packages_to_log:
try:
if hasattr(pack, "__name__") and hasattr(pack, "__version__"):
logger.info(
f"Package {pack.__name__} has version number {pack.__version__}"
)
except AttributeError as e:
logger.error(f"Failed to log {pack} version, {e}")
raise
else:
raise AttributeError(
f"Failed to log {pack} version, as the package doesn't have a __name__ and __version__"
)


def _specific_modules(
Expand Down Expand Up @@ -110,8 +116,8 @@ def configure(
log_file_name: Optional[str] = None,
file_log_level: Optional[str] = "DEBUG",
std_out_log_level: Optional[str] = "INFO",
error_only_modules: Optional[Union[str, Sequence[str]]] = None,
modules_to_debug: Optional[str] = None,
error_only_modules: Optional[Union[ModuleType, Sequence[ModuleType]]] = None,
modules_to_debug: Optional[Union[ModuleType, Sequence[ModuleType]]] = None,
package_versions_to_log: Optional[Union[ModuleType, List[ModuleType]]] = None,
):
"""
Expand All @@ -127,9 +133,9 @@ def configure(
The minimum log level to write to file, by default "DEBUG"
std_out_log_level : Optional[str], optional
The minimum log level to write to std out, by default "INFO"
error_only_modules : Optional[Union[str, Sequence[str]]], optional
error_only_modules : Optional[Union[ModuleType, Sequence[ModuleType]]], optional
Modules to only log errors, by default None
modules_to_debug : Optional[str], optional
modules_to_debug : Optional[Union[ModuleType, Sequence[ModuleType]]], optional
Modules to log debug logs, by default None
package_versions_to_log : Optional[ModuleType], optional
Packages to log versions for, by default None
Expand Down
3 changes: 2 additions & 1 deletion tests/create_sample_logs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from felling import configure
import logging
import random
configure('./tests/sample_logs')

configure("./tests/sample_logs")
logger = logging.getLogger(__name__)
logger.info("Ash")

Expand Down
4 changes: 2 additions & 2 deletions tests/test_compare_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_tidy_logs():
"Identical logs, more verbose",
"Different logs, less verbose",
"Different logs, more verbose",
"Different length logs, les verbose",
"Different length logs, less verbose",
"Different length logs, more verbose",
],
)
Expand Down Expand Up @@ -106,4 +106,4 @@ def test_compare_log_file(first_file, second_file, verbosity):
from felling.src.compare_logs import compare_log_file

# identical files
compare_log_file(first_file, second_file, verbosity)
compare_log_file(first_file, second_file, verbosity)
37 changes: 13 additions & 24 deletions tests/test_configure_felling.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import os
import re
from pathlib import Path
import shutil
import pytest

str_log_path = "./tests/logs"


def test_configure_default_path():
"""Quick sanity check test"""
from felling.src.configure_felling import configure

log_path = "./tests/logs"
shutil.rmtree(log_path, ignore_errors=True)
shutil.rmtree(str_log_path, ignore_errors=True)

configure()

assert len(os.listdir(log_path)) == 1
shutil.rmtree(log_path)
assert len(os.listdir(str_log_path)) == 1
shutil.rmtree(str_log_path)


def test_configure_str_path():
Expand Down Expand Up @@ -46,9 +48,8 @@ def test_configure_pathlib_path():
def test_configure_file_name():
"""Check custom file names are working"""
from felling.src.configure_felling import configure
import re

log_path = Path("./tests/logs")
log_path = Path(str_log_path)
shutil.rmtree(log_path, ignore_errors=True)
file_name = "test_log"
configure(log_path, log_file_name=file_name)
Expand All @@ -65,12 +66,10 @@ def test_configure_file_name():

def test_update_filenames():
from felling.src.configure_felling import _update_filenames
import re

config = {"handlers": {"file_handler": {"filename": "NOT A FILENAME"}}}
log_path = Path("./logs")
log_file_name = "test_file_name"
expected_path = log_path / (log_file_name + ".log")
config = _update_filenames(config, log_file_name, log_path)
assert (
re.match(
Expand Down Expand Up @@ -103,18 +102,14 @@ def test_log_versions_one_package():
def test_log_versions_multiple_package():
from felling.src.configure_felling import _log_versions
import felling
import re

assert _log_versions((felling, re)) is None
assert _log_versions([felling, re]) is None


def test_log_versions_invalid_package():
from felling.src.configure_felling import _log_versions
import os

with pytest.raises(
AttributeError, match="module 'os' has no attribute '__version__'"
):
with pytest.raises(AttributeError):
assert _log_versions(os) is None


Expand All @@ -127,7 +122,6 @@ def test_log_versions_other_error():

def test_specific_modules_one_module_error_only():
from felling.src.configure_felling import _specific_modules
import re

config = {"loggers": {"ERROR only": "ERROR only handler"}}
config = _specific_modules(config, re, "ERROR")
Expand All @@ -136,8 +130,6 @@ def test_specific_modules_one_module_error_only():

def test_specific_modules_multiple_module_error_only():
from felling.src.configure_felling import _specific_modules
import re
import os

config = {"loggers": {"ERROR only": "ERROR only handler"}}
config = _specific_modules(config, [re, os], "ERROR")
Expand All @@ -147,7 +139,6 @@ def test_specific_modules_multiple_module_error_only():

def test_specific_modules_one_module_debug_only():
from felling.src.configure_felling import _specific_modules
import re

config = {"loggers": {"DEBUG only": "DEBUG only handler"}}
config = _specific_modules(config, re, "DEBUG")
Expand All @@ -156,8 +147,6 @@ def test_specific_modules_one_module_debug_only():

def test_specific_modules_multiple_module_debug_only():
from felling.src.configure_felling import _specific_modules
import re
import os

config = {"loggers": {"DEBUG only": "DEBUG only handler"}}
config = _specific_modules(config, [re, os], "DEBUG")
Expand All @@ -169,16 +158,16 @@ def test_specific_modules_single_str_module():
from felling.src.configure_felling import _specific_modules

config = {"loggers": {"DEBUG only": "DEBUG only handler"}}
with pytest.raises(TypeError) as e:
config = _specific_modules(config, "Ash", "DEBUG")
with pytest.raises(TypeError, match="module A must be a ModuleType.") as e:
_specific_modules(config, "Ash", "DEBUG")


def test_specific_modules_multiple_str_module():
from felling.src.configure_felling import _specific_modules

config = {"loggers": {"DEBUG only": "DEBUG only handler"}}
with pytest.raises(TypeError) as e:
config = _specific_modules(config, ["Ash", "Birch"], "DEBUG")
with pytest.raises(TypeError, match="module Ash must be a ModuleType.") as e:
_specific_modules(config, ["Ash", "Birch"], "DEBUG")


def test_logging_disabled():
Expand Down

0 comments on commit 26e8ad2

Please sign in to comment.