/usr/lib/python2.7/dist-packages/neutron_lib/hacking/translation_checks.py is in python-neutron-lib 0.4.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | # Copyright (c) 2014 OpenStack Foundation.
#
# 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
import pep8
_all_log_levels = {
'critical': '_LC',
'error': '_LE',
'exception': '_LE',
'info': '_LI',
'reserved': '_', # this should never be used with a log unless
# it is a variable used for a log message and
# a exception
'warning': '_LW',
}
_all_hints = set(_all_log_levels.values())
_log_warn = re.compile(
r"(.)*LOG\.(warn)\(\s*('|\"|_)")
def _regex_for_level(level, hint):
return r".*LOG\.%(level)s\(\s*((%(wrong_hints)s)\(|'|\")" % {
'level': level,
'wrong_hints': '|'.join(_all_hints - set([hint])),
}
_log_translation_hint = re.compile(
'|'.join('(?:%s)' % _regex_for_level(level, hint)
for level, hint in _all_log_levels.items()))
_log_string_interpolation = re.compile(
r".*LOG\.(error|warning|info|critical|exception|debug)\([^,]*%[^,]*[,)]")
def _translation_is_not_expected(filename):
# Do not do these validations on tests
return any(pat in filename for pat in ["/tests/", "rally-jobs/plugins/"])
def validate_log_translations(logical_line, physical_line, filename):
"""N531 - Log messages require translation hints.
:param logical_line: The logical line to check.
:param physical_line: The physical line to check.
:param filename: The file name where the logical line exists.
:returns: None if the logical line passes the check, otherwise a tuple
is yielded that contains the offending index in logical line and a
message describe the check validation failure.
"""
if _translation_is_not_expected(filename):
return
if pep8.noqa(physical_line):
return
msg = "N531: Log messages require translation hints!"
if _log_translation_hint.match(logical_line):
yield (0, msg)
def check_log_warn_deprecated(logical_line, filename):
"""N532 - Use LOG.warning due to compatibility with py3.
:param logical_line: The logical line to check.
:param filename: The file name where the logical line exists.
:returns: None if the logical line passes the check, otherwise a tuple
is yielded that contains the offending index in logical line and a
message describe the check validation failure.
"""
msg = "N532: Use LOG.warning due to compatibility with py3"
if _log_warn.match(logical_line):
yield (0, msg)
def no_translate_debug_logs(logical_line, filename):
"""N533 - Don't translate debug level logs.
Check for 'LOG.debug(_(' and 'LOG.debug(_Lx('
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.
:param logical_line: The logical line to check.
:param filename: The file name where the logical line exists.
:returns: None if the logical line passes the check, otherwise a tuple
is yielded that contains the offending index in logical line and a
message describe the check validation failure.
"""
for hint in _all_hints:
if logical_line.startswith("LOG.debug(%s(" % hint):
yield(0, "N533 Don't translate debug level logs")
def check_raised_localized_exceptions(logical_line, filename):
"""N534 - Untranslated exception message.
:param logical_line: The logical line to check.
:param filename: The file name where the logical line exists.
:returns: None if the logical line passes the check, otherwise a tuple
is yielded that contains the offending index in logical line and a
message describe the check validation failure.
"""
if _translation_is_not_expected(filename):
return
logical_line = logical_line.strip()
raised_search = re.compile(
r"raise (?:\w*)\((.*)\)").match(logical_line)
if raised_search:
exception_msg = raised_search.groups()[0]
if exception_msg.startswith("\"") or exception_msg.startswith("\'"):
msg = "N534: Untranslated exception message."
yield (logical_line.index(exception_msg), msg)
def check_delayed_string_interpolation(logical_line, filename, noqa):
"""N536 - String interpolation should be delayed at logging calls.
N536: LOG.debug('Example: %s' % 'bad')
Okay: LOG.debug('Example: %s', 'good')
:param logical_line: The logical line to check.
:param filename: The file name where the logical line exists.
:param noqa: Noqa indicator.
:returns: None if the logical line passes the check, otherwise a tuple
is yielded that contains the offending index in logical line and a
message describe the check validation failure.
"""
msg = ("N536 String interpolation should be delayed to be "
"handled by the logging code, rather than being done "
"at the point of the logging call. "
"Use ',' instead of '%'.")
if noqa:
return
if '/tests/' in filename:
return
if _log_string_interpolation.match(logical_line):
yield(logical_line.index('%'), msg)
|