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
This commit is contained in:
Tom Barron 2017-03-31 08:17:33 -04:00
parent 2fb902d8e2
commit 0a3df6f99e
3 changed files with 23 additions and 29 deletions

View File

@ -12,7 +12,6 @@ Manila Specific Commandments
- [M310] Check for improper use of logging format arguments. - [M310] Check for improper use of logging format arguments.
- [M312] Use assertIsNone(...) instead of assertEqual(None, ...). - [M312] Use assertIsNone(...) instead of assertEqual(None, ...).
- [M313] Use assertTrue(...) rather than assertEqual(True, ...). - [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. - [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(). - [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 - [M326] Translated messages cannot be concatenated. String should be
@ -23,6 +22,7 @@ Manila Specific Commandments
- [M337] Ensure to not use xrange(). - [M337] Ensure to not use xrange().
- [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4(). - [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4().
- [M338] Ensure to not use LOG.warn(). - [M338] Ensure to not use LOG.warn().
- [M359] Validate that log messages are not translated.
LOG Translations LOG Translations
---------------- ----------------

View File

@ -37,8 +37,11 @@ Guidelines for writing new hacking checks
UNDERSCORE_IMPORT_FILES = [] UNDERSCORE_IMPORT_FILES = []
translated_log = re.compile( translated_log = re.compile(
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)" r"(.)*LOG\."
"\(\s*_\(\s*('|\")") r"(audit|debug|error|info|warn|warning|critical|exception)"
r"\("
r"(_|_LE|_LI|_LW)"
r"\(")
string_translation = re.compile(r"[^_]*_\(\s*('|\")") string_translation = re.compile(r"[^_]*_\(\s*('|\")")
underscore_import_check = re.compile(r"(.)*import _$") underscore_import_check = re.compile(r"(.)*import _$")
underscore_import_check_multi = re.compile(r"(.)*import (.)*_, (.)*") underscore_import_check_multi = re.compile(r"(.)*import (.)*_, (.)*")
@ -97,21 +100,9 @@ class BaseASTChecker(ast.NodeVisitor):
return False return False
def no_translate_debug_logs(logical_line, filename): def no_translate_logs(logical_line):
"""Check for 'LOG.debug(_(' if translated_log.match(logical_line):
yield(0, "M359 Don't translate log messages!")
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")
class CheckLoggingFormatArgs(BaseASTChecker): class CheckLoggingFormatArgs(BaseASTChecker):
@ -196,8 +187,7 @@ def check_explicit_underscore_import(logical_line, filename):
underscore_import_check_multi.match(logical_line) or underscore_import_check_multi.match(logical_line) or
custom_underscore_check.match(logical_line)): custom_underscore_check.match(logical_line)):
UNDERSCORE_IMPORT_FILES.append(filename) UNDERSCORE_IMPORT_FILES.append(filename)
elif (translated_log.match(logical_line) or elif string_translation.match(logical_line):
string_translation.match(logical_line)):
yield(0, "M323: Found use of _() without explicit import of _ !") yield(0, "M323: Found use of _() without explicit import of _ !")
@ -340,7 +330,7 @@ def no_log_warn_check(logical_line):
def factory(register): def factory(register):
register(check_explicit_underscore_import) register(check_explicit_underscore_import)
register(no_translate_debug_logs) register(no_translate_logs)
register(CheckForStrUnicodeExc) register(CheckForStrUnicodeExc)
register(CheckLoggingFormatArgs) register(CheckLoggingFormatArgs)
register(CheckForTransAdd) register(CheckForTransAdd)

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import itertools
import sys import sys
import textwrap import textwrap
@ -57,15 +58,18 @@ class HackingTestCase(test.TestCase):
should pass. should pass.
""" """
def test_no_translate_debug_logs(self): @ddt.data(*itertools.product(
self.assertEqual(1, len(list(checks.no_translate_debug_logs( ('', '_', '_LE', '_LI', '_LW'),
"LOG.debug(_('foo'))", "manila/scheduler/foo.py")))) ('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( if log_marker:
"LOG.debug('foo')", "manila/scheduler/foo.py")))) self.assertEqual(1, len(list(checks.no_translate_logs(code))))
else:
self.assertEqual(0, len(list(checks.no_translate_debug_logs( self.assertEqual(0, len(list(checks.no_translate_logs(code))))
"LOG.info(_('foo'))", "manila/scheduler/foo.py"))))
def test_check_explicit_underscore_import(self): def test_check_explicit_underscore_import(self):
self.assertEqual(1, len(list(checks.check_explicit_underscore_import( self.assertEqual(1, len(list(checks.check_explicit_underscore_import(