Add hacking checks related to logging guideliness

As soon as we have logging guideliness now it's better for us
to have an automated hacking checks at least for some points of it.

Change-Id: I1cef618a83a23f23304b4a40eb8d4263d8b89c70
This commit is contained in:
Nikolay Starodubtsev 2015-04-03 18:35:41 +03:00
parent 882328204c
commit 0eb845d246
3 changed files with 134 additions and 0 deletions

View File

@ -29,3 +29,18 @@ 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
- [S374] You used a deprecated log level

View File

@ -19,6 +19,7 @@ import tokenize
from sahara.utils.hacking import commit_message
from sahara.utils.hacking import import_checks
from sahara.utils.hacking import logging_checks
def _starts_with_any(line, *prefixes):
@ -105,3 +106,6 @@ 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.validate_log_translations)
register(logging_checks.no_translate_debug_logs)
register(logging_checks.accepted_log_levels)

View File

@ -0,0 +1,115 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
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.
log_translation_LI = re.compile(
r"(.)*LOG\.(info)\(\s*(_\(|'|\")")
log_translation_LE = re.compile(
r"(.)*LOG\.(exception)\(\s*(_\(|'|\")")
log_translation_LW = re.compile(
r"(.)*LOG\.(warning)\(\s*(_\(|'|\")")
log_translation_LC = re.compile(
r"(.)*LOG\.(critical)\(\s*('|\")")
accepted_log_level = re.compile(
r"^LOG\.(debug|info|exception|warning|error|critical)\(")
def validate_log_translations(logical_line, filename):
"""Check if log levels has translations and if it's correct.
S369
S370
S371
S372
"""
# NOTE(Kezar): sahara/tests included because we don't require translations
# in tests. sahara/openstack/common included because it's part imported
# from oslo and we don't change it forever and ever. sahara/db/templates
# provide separate cli interface so we don't want to translate it.
ignore_dirs = ["sahara/db/templates",
"sahara/tests",
"sahara/openstack/common"]
for directory in ignore_dirs:
if directory in filename:
return
# Translations are not required in the test directory.
# This will not catch all instances of violations, just direct
# misuse of the form LOG.info('Message').
msg = "S369: LOG.info messages require translations `_LI()`!"
if log_translation_LI.search(logical_line):
yield (0, msg)
msg = ("S370: LOG.exception and LOG.error messages require "
"translations `_LE()`!")
if log_translation_LE.search(logical_line):
yield (0, msg)
msg = "S371: LOG.warning messages require translations `_LW()`!"
if log_translation_LW.search(logical_line):
yield (0, msg)
msg = "S372: LOG.critical messages require translations `_LC()`!"
if log_translation_LC.search(logical_line):
yield (0, msg)
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.
S373
"""
msg = "S373 Don't translate debug level logs"
if logical_line.startswith("LOG.debug(_("):
yield(0, msg)
def accepted_log_levels(logical_line, filename):
"""In Sahara we use only 5 log levels.
This check is needed because we don't want new contributors to
use deprecated log levels.
S373
"""
# NOTE(Kezar): sahara/tests included because we don't require translations
# in tests. sahara/openstack/common included because it's part imported
# from oslo and we don't change it forever and ever. sahara/db/templates
# provide separate cli interface so we don't want to translate it.
ignore_dirs = ["sahara/db/templates",
"sahara/tests",
"sahara/openstack/common"]
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")
if logical_line.startswith("LOG."):
if not accepted_log_level.search(logical_line):
yield(0, msg)