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.

Since all log translations are invalidated, check_explicit_underscore_import
can be simplified.

Change-Id: If941dc7c1052cea6a3d011980776ee272f8d39a3
This commit is contained in:
Ngo Quoc Cuong 2017-07-04 05:09:01 -04:00
parent efb35ee3a0
commit 0f9c00a08f
3 changed files with 24 additions and 27 deletions

View File

@ -1,5 +1,5 @@
Cloudkitty Style Commandments Cloudkitty Style Commandments
============================ =============================
- Step 1: Read the OpenStack Style Commandments - Step 1: Read the OpenStack Style Commandments
http://docs.openstack.org/developer/hacking/ http://docs.openstack.org/developer/hacking/
@ -7,12 +7,12 @@ Cloudkitty Style Commandments
Cloudkitty Specific Commandments Cloudkitty Specific Commandments
------------------------------- --------------------------------
- [C310] Check for improper use of logging format arguments. - [C310] Check for improper use of logging format arguments.
- [C311] Use assertIsNone(...) instead of assertEqual(None, ...). - [C311] Use assertIsNone(...) instead of assertEqual(None, ...).
- [C312] Use assertTrue(...) rather than assertEqual(True, ...). - [C312] Use assertTrue(...) rather than assertEqual(True, ...).
- [C313] Validate that debug level logs are not translated. - [C313] Validate that logs are not translated.
- [C314] str() and unicode() cannot be used on an exception. Remove or use six.text_type(). - [C314] str() and unicode() cannot be used on an exception. Remove or use six.text_type().
- [C315] Translated messages cannot be concatenated. String should be - [C315] Translated messages cannot be concatenated. String should be
included in translated message. included in translated message.

View File

@ -36,11 +36,12 @@ Guidelines for writing new hacking checks
UNDERSCORE_IMPORT_FILES = [] UNDERSCORE_IMPORT_FILES = []
log_translation = re.compile( _all_log_levels = {'debug', 'error', 'info', 'warning',
r"(.)*LOG\.(audit|error|info|critical|exception)\(\s*('|\")") 'critical', 'exception'}
translated_log = re.compile( # Since _Lx have been removed, we just need to check _()
r"(.)*LOG\.(audit|error|info|warn|warning|critical|exception)" translated_logs = re.compile(
"\(\s*_\(\s*('|\")") r"(.)*LOG\.(%(level)s)\(\s*_\(" % {'level': '|'.join(_all_log_levels)})
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 (.)*_, (.)*")
@ -99,12 +100,11 @@ class BaseASTChecker(ast.NodeVisitor):
return False return False
def no_translate_debug_logs(logical_line, filename): def no_translate_logs(logical_line, filename):
"""Check for 'LOG.debug(_(' """Check for 'LOG.*(_('
As per our translation policy, Starting with the Pike series, OpenStack no longer supports log
https://wiki.openstack.org/wiki/LoggingStandards#Log_Translation translation.
we shouldn't translate debug level logs.
* This check assumes that 'LOG' is a logger. * This check assumes that 'LOG' is a logger.
* Use filename so we can start enforcing this in specific folders instead * Use filename so we can start enforcing this in specific folders instead
@ -112,8 +112,8 @@ def no_translate_debug_logs(logical_line, filename):
C313 C313
""" """
if logical_line.startswith("LOG.debug(_("): if translated_logs.match(logical_line):
yield(0, "C313 Don't translate debug level logs") yield(0, "C313 Don't translate logs")
class CheckLoggingFormatArgs(BaseASTChecker): class CheckLoggingFormatArgs(BaseASTChecker):
@ -198,8 +198,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, "C321: Found use of _() without explicit import of _ !") yield(0, "C321: Found use of _() without explicit import of _ !")
@ -341,7 +340,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

@ -57,15 +57,13 @@ class HackingTestCase(tests.TestCase):
should pass. should pass.
""" """
def test_no_translate_debug_logs(self): def test_no_log_translations(self):
self.assertEqual(1, len(list(checks.no_translate_debug_logs( for log in checks._all_log_levels:
"LOG.debug(_('foo'))", "cloudkitty/scheduler/foo.py")))) bad = 'LOG.%s(_("Bad"))' % log
self.assertEqual(1, len(list(checks.no_translate_logs(bad, 'f'))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs( # Catch abuses when used with a variable and not a literal
"LOG.debug('foo')", "cloudkitty/scheduler/foo.py")))) bad = 'LOG.%s(_(msg))' % log
self.assertEqual(1, len(list(checks.no_translate_logs(bad, 'f'))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs(
"LOG.info(_('foo'))", "cloudkitty/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(