From e181a9c0ffd371f71b0cff6d4cab230ab6708417 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Cuong Date: Sun, 2 Jul 2017 22:58:18 -0400 Subject: [PATCH] Update log translation hacking rule Starting with the Pike series, OpenStack no longer supports log translation. Update hacking rule to prevent log translation in all log level instead of only debug level. Change-Id: I4c6d720b2aa91a883bbf974d390028218357e1e4 --- HACKING.rst | 16 +++-------- sahara/utils/hacking/checks.py | 2 +- sahara/utils/hacking/logging_checks.py | 40 ++++++++++++-------------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index c58e2296..e89128ef 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -1,12 +1,12 @@ Sahara Style Commandments -========================== +========================= - Step 1: Read the OpenStack Style Commandments http://docs.openstack.org/developer/hacking/ - Step 2: Read on Sahara Specific Commandments ------------------------------ +---------------------------- Commit Messages --------------- @@ -30,19 +30,11 @@ Dictionaries/Lists - [S368] Must use a dict comprehension instead of a dict constructor with a sequence of key-value pairs. For more information, please refer to http://legacy.python.org/dev/peps/pep-0274/ -======= + Logs ---- -- [S369] Check LOG.info translations - -- [S370] Check LOG.error translations - -- [S371] Check LOG.warning translations - -- [S372] Check LOG.critical translation - -- [S373] LOG.debug never used for translations +- [S373] Don't translate logs - [S374] You used a deprecated log level diff --git a/sahara/utils/hacking/checks.py b/sahara/utils/hacking/checks.py index 9f767b5e..658fc395 100644 --- a/sahara/utils/hacking/checks.py +++ b/sahara/utils/hacking/checks.py @@ -133,7 +133,7 @@ def factory(register): register(import_checks.hacking_import_groups) register(import_checks.hacking_import_groups_together) register(dict_constructor_with_list_copy) - register(logging_checks.no_translate_debug_logs) + register(logging_checks.no_translate_logs) register(logging_checks.accepted_log_levels) register(use_jsonutils) register(no_mutable_default_args) diff --git a/sahara/utils/hacking/logging_checks.py b/sahara/utils/hacking/logging_checks.py index c79162c5..dec8c030 100644 --- a/sahara/utils/hacking/logging_checks.py +++ b/sahara/utils/hacking/logging_checks.py @@ -13,23 +13,21 @@ import re -# NOTE(Kezar): this checks was copied from cinder/nova and should be one day -# appear at general hacking checks. So we need to try remember it and remove it -# when it'll be happened. -# FIXME(Kezar): may be it will be better to right in the way that introduced in -# keystone but it will need additional work and total checks refactoring. +_all_log_levels = "info|exception|warning|critical|error|debug" + +_accepted_log_level = re.compile( + r"(.)*LOG\.(%(levels)s)\(" % {'levels': _all_log_levels}) + +# Since _Lx() have been removed, we just need to check _() +_translated_log = re.compile( + r"(.)*LOG\.(%(levels)s)\(\s*_\(" % {'levels': _all_log_levels}) -accepted_log_level = re.compile( - r"^LOG\.(debug|info|exception|warning|error|critical)\(") +def no_translate_logs(logical_line, filename): + """Check for 'LOG.*(_(' - -def no_translate_debug_logs(logical_line, filename): - """Check for 'LOG.debug(_(' - - As per our translation policy, - https://wiki.openstack.org/wiki/LoggingStandards#Log_Translation - we shouldn't translate debug level logs. + Translators don't provide translations for log messages, and operators + asked not to translate them. * This check assumes that 'LOG' is a logger. * Use filename so we can start enforcing this in specific folders instead @@ -37,9 +35,9 @@ def no_translate_debug_logs(logical_line, filename): S373 """ - msg = "S373 Don't translate debug level logs" - if logical_line.startswith("LOG.debug(_("): - yield(0, msg) + msg = "S373 Don't translate logs" + if _translated_log.match(logical_line): + yield (0, msg) def accepted_log_levels(logical_line, filename): @@ -47,7 +45,7 @@ def accepted_log_levels(logical_line, filename): This check is needed because we don't want new contributors to use deprecated log levels. - S373 + S374 """ # NOTE(Kezar): sahara/tests included because we don't require translations @@ -59,8 +57,8 @@ def accepted_log_levels(logical_line, filename): for directory in ignore_dirs: if directory in filename: return - msg = ("S373 You used deprecated log level. Accepted log levels are " - "debug|info|warning|error|critical") + msg = ("S374 You used deprecated log level. Accepted log levels are " + "%(levels)s" % {'levels': _all_log_levels}) if logical_line.startswith("LOG."): - if not accepted_log_level.search(logical_line): + if not _accepted_log_level.search(logical_line): yield(0, msg)