From 0a3df6f99ec12fa263519f80eea62552fdd4e11b Mon Sep 17 00:00:00 2001 From: Tom Barron Date: Fri, 31 Mar 2017 08:17:33 -0400 Subject: [PATCH] Hacking: do not translate log messages Log messages are no longer being translated. See: http://lists.openstack.org/pipermail/openstack-i18n/2016-November/002574.htmli http://lists.openstack.org/pipermail/openstack-dev/2017-March/113365.html Change-Id: I422fa934b27da1c252f72ac3bf94ef468f0d7ef6 --- HACKING.rst | 2 +- manila/hacking/checks.py | 30 ++++++++++-------------------- manila/tests/test_hacking.py | 20 ++++++++++++-------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 813348cfbc..61a459cc93 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -12,7 +12,6 @@ Manila Specific Commandments - [M310] Check for improper use of logging format arguments. - [M312] Use assertIsNone(...) instead of assertEqual(None, ...). - [M313] Use assertTrue(...) rather than assertEqual(True, ...). -- [M319] Validate that debug level logs are not translated. - [M323] Ensure that the _() function is explicitly imported to ensure proper translations. - [M325] str() and unicode() cannot be used on an exception. Remove or use six.text_type(). - [M326] Translated messages cannot be concatenated. String should be @@ -23,6 +22,7 @@ Manila Specific Commandments - [M337] Ensure to not use xrange(). - [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4(). - [M338] Ensure to not use LOG.warn(). +- [M359] Validate that log messages are not translated. LOG Translations ---------------- diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py index d5887a0b3e..91a25f9b4f 100644 --- a/manila/hacking/checks.py +++ b/manila/hacking/checks.py @@ -37,8 +37,11 @@ Guidelines for writing new hacking checks UNDERSCORE_IMPORT_FILES = [] translated_log = re.compile( - r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)" - "\(\s*_\(\s*('|\")") + r"(.)*LOG\." + r"(audit|debug|error|info|warn|warning|critical|exception)" + r"\(" + r"(_|_LE|_LI|_LW)" + r"\(") string_translation = re.compile(r"[^_]*_\(\s*('|\")") underscore_import_check = re.compile(r"(.)*import _$") underscore_import_check_multi = re.compile(r"(.)*import (.)*_, (.)*") @@ -97,21 +100,9 @@ class BaseASTChecker(ast.NodeVisitor): return False -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. - - * This check assumes that 'LOG' is a logger. - * Use filename so we can start enforcing this in specific folders instead - of needing to do so all at once. - - M319 - """ - if logical_line.startswith("LOG.debug(_("): - yield(0, "M319 Don't translate debug level logs") +def no_translate_logs(logical_line): + if translated_log.match(logical_line): + yield(0, "M359 Don't translate log messages!") class CheckLoggingFormatArgs(BaseASTChecker): @@ -196,8 +187,7 @@ def check_explicit_underscore_import(logical_line, filename): underscore_import_check_multi.match(logical_line) or custom_underscore_check.match(logical_line)): UNDERSCORE_IMPORT_FILES.append(filename) - elif (translated_log.match(logical_line) or - string_translation.match(logical_line)): + elif string_translation.match(logical_line): yield(0, "M323: Found use of _() without explicit import of _ !") @@ -340,7 +330,7 @@ def no_log_warn_check(logical_line): def factory(register): register(check_explicit_underscore_import) - register(no_translate_debug_logs) + register(no_translate_logs) register(CheckForStrUnicodeExc) register(CheckLoggingFormatArgs) register(CheckForTransAdd) diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py index 0e1a850599..50dd9e4b27 100644 --- a/manila/tests/test_hacking.py +++ b/manila/tests/test_hacking.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import itertools import sys import textwrap @@ -57,15 +58,18 @@ class HackingTestCase(test.TestCase): should pass. """ - def test_no_translate_debug_logs(self): - self.assertEqual(1, len(list(checks.no_translate_debug_logs( - "LOG.debug(_('foo'))", "manila/scheduler/foo.py")))) + @ddt.data(*itertools.product( + ('', '_', '_LE', '_LI', '_LW'), + ('audit', 'debug', 'error', 'info', 'warn', 'warning', 'critical', + 'exception',))) + @ddt.unpack + def test_no_translate_logs(self, log_marker, log_method): + code = "LOG.{0}({1}('foo'))".format(log_method, log_marker) - self.assertEqual(0, len(list(checks.no_translate_debug_logs( - "LOG.debug('foo')", "manila/scheduler/foo.py")))) - - self.assertEqual(0, len(list(checks.no_translate_debug_logs( - "LOG.info(_('foo'))", "manila/scheduler/foo.py")))) + if log_marker: + self.assertEqual(1, len(list(checks.no_translate_logs(code)))) + else: + self.assertEqual(0, len(list(checks.no_translate_logs(code)))) def test_check_explicit_underscore_import(self): self.assertEqual(1, len(list(checks.check_explicit_underscore_import(