Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pandas
xlrd
openpyxl
progress
PyYAML
PyYAML
lastversion
45 changes: 43 additions & 2 deletions src/fosslight_util/set_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
import sys
import platform
from . import constant as constant
from lastversion import lastversion


class CustomFormatter(logging.Formatter):
"""Logging Formatter to add colors and count warning / errors"""

grey = "\033[37m"
yellow = "\033[33m"
orange = "\033[31m"
red = "\033[91m"
bold_red = "\033[91m"
reset = "\033[0m"
format = "%(message)s"

FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}

def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)


def init_log(log_file, create_file=True, stream_log_level=logging.INFO, file_log_level=logging.DEBUG):
Expand All @@ -32,8 +58,7 @@ def init_log(log_file, create_file=True, stream_log_level=logging.INFO, file_log

console_handler = logging.StreamHandler()
console_handler.setLevel(stream_log_level)
console_formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(console_formatter)
console_handler.setFormatter(CustomFormatter())
console_handler.propagate = False
logger.addHandler(console_handler)

Expand All @@ -42,6 +67,21 @@ def init_log(log_file, create_file=True, stream_log_level=logging.INFO, file_log
return logger


def init_check_latest_version(pkg_version="", main_package_name=""):

logger = logging.getLogger(constant.LOGGER_NAME)

try:
has_update = lastversion.has_update(repo=main_package_name, at='pip', current_version=pkg_version)
if has_update:
logger.info('### Version Info ###')
logger.warning('Newer version is available : v.{}'.format(str(has_update)))
logger.warning('You can update it with command (\'pip install ' + main_package_name + ' --upgrade\')')
except TypeError:
logger.warning('Cannot check the lastest version on PIP')
logger.warning('You could use already installed version\n')


def init_log_item(main_package_name="", path_to_analyze=""):

_PYTHON_VERSION = sys.version_info[0]
Expand All @@ -52,6 +92,7 @@ def init_log_item(main_package_name="", path_to_analyze=""):
}
if main_package_name != "":
pkg_version = pkg_resources.get_distribution(main_package_name).version
init_check_latest_version(pkg_version, main_package_name)
_result_log["Tool Info"] = main_package_name + " v." + pkg_version
if path_to_analyze != "":
_result_log["Path to analyze"] = path_to_analyze
Expand Down
3 changes: 1 addition & 2 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# Copyright (c) 2021 LG Electronics Inc.
# SPDX-License-Identifier: Apache-2.0
import yaml
from fosslight_util.set_log import init_log
from fosslight_util.set_log import init_log_item
from fosslight_util.set_log import init_log, init_log_item
from _print_log import print_log
from _print_log_with_another_logger import print_log_another_logger

Expand Down
55 changes: 36 additions & 19 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ install_command = pip install {opts} {packages}
basepython= python3.6
whitelist_externals = cat
ls
setenv =
PYTHONPATH=.

[flake8]
max-line-length = 130
Expand All @@ -18,26 +20,41 @@ exclude = .tox/*
filterwarnings = ignore::DeprecationWarning

[testenv:test_run]
setenv =
PYTHONPATH=.
deps =
-r{toxinidir}/requirements.txt

commands =
# Test - logging
python tests/test_log.py
ls test_result
cat test_result/log_file1.txt
# Test - writing text
python tests/test_text.py
cat test_result/txt/test.txt
# Test - writing excel
python tests/test_excel.py
ls test_result/excel
cat test_result/excel/OSS-Report.csv

[testenv:release]
deps =
-r{toxinidir}/requirements-dev.txt

commands =
# Test - print help msg
python tests/test_help.py
# Test - logging
python tests/test_log.py
ls test_result
cat test_result/log_file1.txt
# Test - writing text
python tests/test_text.py
cat test_result/txt/test.txt
# Test - writing excel
python tests/test_excel.py
ls test_result/excel
cat test_result/excel/OSS-Report.csv
# Test - timer
python tests/test_timer.py
# Test - check PEP8
pytest -v --flake8
# Test - print help msg
python tests/test_help.py
# Test - logging
python tests/test_log.py
ls test_result
cat test_result/log_file1.txt
# Test - writing text
python tests/test_text.py
cat test_result/txt/test.txt
# Test - writing excel
python tests/test_excel.py
ls test_result/excel
cat test_result/excel/OSS-Report.csv
# Test - timer
python tests/test_timer.py
# Test - check PEP8
pytest -v --flake8