Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [C312] Validate that logs are not translated. - [H904] Delay string interpolations at logging calls. [1] [1]https://docs.openstack.org/oslo.i18n/latest/user/guidelines.html#adding-variables-to-log-messages Change-Id: I58930b6b42b00b28d80bda917834b077c380b0a0
- Loading branch information
Showing
16 changed files
with
137 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
Blazar Style Commandments | ||
========================== | ||
========================= | ||
|
||
- Step 1: Read the OpenStack Style Commandments | ||
https://docs.openstack.org/hacking/latest/ | ||
- Step 2: Read on | ||
|
||
Blazar Specific Commandments | ||
----------------------------- | ||
|
||
None so far | ||
---------------------------- | ||
|
||
- [C312] Validate that logs are not translated. | ||
- [H904] Delay string interpolations at logging calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright (c) 2017 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 | ||
|
||
|
||
_all_log_levels = {'critical', 'error', 'exception', 'info', | ||
'warning', 'debug'} | ||
|
||
# Since _Lx() have been removed, we just need to check _() | ||
_all_hints = {'_'} | ||
|
||
_log_translation_hint = re.compile( | ||
r".*LOG\.(%(levels)s)\(\s*(%(hints)s)\(" % { | ||
'levels': '|'.join(_all_log_levels), | ||
'hints': '|'.join(_all_hints), | ||
}) | ||
|
||
|
||
def no_translate_logs(logical_line): | ||
"""C312 - Don't translate logs. | ||
Check for 'LOG.*(_(' | ||
Translators don't provide translations for log messages, and operators | ||
asked not to translate them. | ||
* This check assumes that 'LOG' is a logger. | ||
:param logical_line: The logical line to check. | ||
: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 _log_translation_hint.match(logical_line): | ||
yield (0, "C312: Log messages should not be translated!") | ||
|
||
|
||
def factory(register): | ||
register(no_translate_logs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright (c) 2017 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. | ||
|
||
from blazar.hacking import checks | ||
from blazar import tests | ||
|
||
|
||
class HackingTestCase(tests.TestCase): | ||
def test_no_log_translations(self): | ||
for log in checks._all_log_levels: | ||
for hint in checks._all_hints: | ||
bad = 'LOG.%s(%s("Bad"))' % (log, hint) | ||
self.assertEqual(1, len(list(checks.no_translate_logs(bad)))) | ||
# Catch abuses when used with a variable and not a literal | ||
bad = 'LOG.%s(%s(msg))' % (log, hint) | ||
self.assertEqual(1, len(list(checks.no_translate_logs(bad)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.