diff --git a/requirements.txt b/requirements.txt index c834db5..69b4ccc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ pandas xlrd openpyxl progress -PyYAML \ No newline at end of file +PyYAML +lastversion diff --git a/src/fosslight_util/set_log.py b/src/fosslight_util/set_log.py index b84dff1..974ede0 100755 --- a/src/fosslight_util/set_log.py +++ b/src/fosslight_util/set_log.py @@ -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): @@ -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) @@ -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] @@ -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 diff --git a/tests/test_log.py b/tests/test_log.py index 4c4b22b..d36809a 100755 --- a/tests/test_log.py +++ b/tests/test_log.py @@ -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 diff --git a/tox.ini b/tox.ini index 07f0240..8a55ba4 100644 --- a/tox.ini +++ b/tox.ini @@ -9,6 +9,8 @@ install_command = pip install {opts} {packages} basepython= python3.6 whitelist_externals = cat ls +setenv = + PYTHONPATH=. [flake8] max-line-length = 130 @@ -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 \ No newline at end of file + # 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 \ No newline at end of file