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: I00d1930ba2bb0e0278e0d33147e4d15c59615e74
This commit is contained in:
Ngo Quoc Cuong 2017-07-04 03:50:54 -04:00
parent 9b50f5d839
commit 7539e4510b
2 changed files with 21 additions and 33 deletions

View File

@ -41,18 +41,13 @@ asse_equal_end_with_none_re = re.compile(
asse_equal_start_with_none_re = re.compile( asse_equal_start_with_none_re = re.compile(
r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)") r"(.)*assertEqual\(None, (\w|\.|\'|\"|\[|\])+\)")
unicode_func_re = re.compile(r"(\s|\W|^)unicode\(") unicode_func_re = re.compile(r"(\s|\W|^)unicode\(")
log_translation = re.compile(
r"(.)*LOG\.(audit)\(\s*('|\")") _all_log_levels = {'debug', 'error', 'info', 'warning',
log_translation_info = re.compile( 'critical', 'exception'}
r"(.)*LOG\.(info)\(\s*(_\(|'|\")") # Since _Lx have been removed, we just need to check _()
log_translation_exception = re.compile( translated_logs = re.compile(
r"(.)*LOG\.(exception)\(\s*(_\(|'|\")") r"(.)*LOG\.(%(level)s)\(\s*_\(" % {'level': '|'.join(_all_log_levels)})
log_translation_error = re.compile(
r"(.)*LOG\.(error)\(\s*(_\(|'|\")")
log_translation_critical = re.compile(
r"(.)*LOG\.(critical)\(\s*(_\(|'|\")")
log_translation_warning = re.compile(
r"(.)*LOG\.(warning)\(\s*(_\(|'|\")")
dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)") dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)")
@ -86,18 +81,13 @@ def assert_equal_none(logical_line):
"sentences not allowed") "sentences not allowed")
def no_translate_debug_logs(logical_line, filename): def no_translate_logs(logical_line):
dirs = [ """Check for use of LOG.*(_(
"glare/api",
"glare/cmd",
"glare/common",
"glare/db",
"glare/tests",
]
if max([name in filename for name in dirs]): G319
if logical_line.startswith("LOG.debug(_("): """
yield(0, "G319: Don't translate debug level logs") if translated_logs.match(logical_line):
yield (0, "G319: Don't translate logs")
def no_direct_use_of_unicode_function(logical_line): def no_direct_use_of_unicode_function(logical_line):
@ -156,7 +146,7 @@ def factory(register):
register(assert_true_instance) register(assert_true_instance)
register(assert_equal_type) register(assert_equal_type)
register(assert_equal_none) register(assert_equal_none)
register(no_translate_debug_logs) register(no_translate_logs)
register(no_direct_use_of_unicode_function) register(no_direct_use_of_unicode_function)
register(check_no_contextlib_nested) register(check_no_contextlib_nested)
register(dict_constructor_with_list_copy) register(dict_constructor_with_list_copy)

View File

@ -44,15 +44,13 @@ class HackingTestCase(base.BaseTestCase):
self.assertEqual( self.assertEqual(
0, len(list(checks.assert_equal_none("self.assertIsNone()")))) 0, len(list(checks.assert_equal_none("self.assertIsNone()"))))
def test_no_translate_debug_logs(self): def test_no_translate_logs(self):
self.assertEqual(1, len(list(checks.no_translate_debug_logs( for log in checks._all_log_levels:
"LOG.debug(_('foo'))", "glare/common/foo.py")))) bad = 'LOG.%s(_("Bad"))' % log
self.assertEqual(1, len(list(checks.no_translate_logs(bad))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs( # Catch abuses when used with a variable and not a literal
"LOG.debug('foo')", "glare/common/foo.py")))) bad = 'LOG.%s(_(msg))' % log
self.assertEqual(1, len(list(checks.no_translate_logs(bad))))
self.assertEqual(0, len(list(checks.no_translate_debug_logs(
"LOG.info(_('foo'))", "glare/common/foo.py"))))
def test_no_direct_use_of_unicode_function(self): def test_no_direct_use_of_unicode_function(self):
self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function( self.assertEqual(1, len(list(checks.no_direct_use_of_unicode_function(