From 8407f72c2ce87faddc70c50f7fc87e1ee75bba9b Mon Sep 17 00:00:00 2001 From: Ha Van Tu Date: Tue, 27 Sep 2016 10:14:47 +0700 Subject: [PATCH] [hacking] Ensure not to use LOG.warn This patchs ensures not to use LOG.warn because LOG.warn deprecated in Python 3 [1], use LOG.warning instead. [1] https://docs.python.org/3/library/logging.html#logging.warning TrivialFix Change-Id: I50e44a89092e0ee15f5be294837a87c321427921 --- HACKING.rst | 1 + manila/hacking/checks.py | 15 +++++++++++++++ manila/tests/test_hacking.py | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 3045daf3..c6b28bb4 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -27,6 +27,7 @@ Manila Specific Commandments with a sequence of key-value pairs. - [M337] Ensure to not use xrange(). - [M354] Use oslo_utils.uuidutils to generate UUID instead of uuid4(). +- [M338] Ensure to not use LOG.warn(). LOG Translations ---------------- diff --git a/manila/hacking/checks.py b/manila/hacking/checks.py index 068026a3..669ec32e 100644 --- a/manila/hacking/checks.py +++ b/manila/hacking/checks.py @@ -59,6 +59,7 @@ dict_constructor_with_list_copy_re = re.compile(r".*\bdict\((\[)?(\(|\[)") assert_no_xrange_re = re.compile(r"\s*xrange\s*\(") assert_True = re.compile(r".*assertEqual\(True, .*\)") assert_None = re.compile(r".*assertEqual\(None, .*\)") +no_log_warn = re.compile(r"\s*LOG.warn\(.*") class BaseASTChecker(ast.NodeVisitor): @@ -360,6 +361,19 @@ def check_uuid4(logical_line): yield (0, msg) +def no_log_warn_check(logical_line): + """Disallow 'LOG.warn' + + Deprecated LOG.warn(), instead use LOG.warning + ://bugs.launchpad.net/manila/+bug/1508442 + + M338 + """ + msg = ("M338: LOG.warn is deprecated, use LOG.warning.") + if re.match(no_log_warn, logical_line): + yield(0, msg) + + def factory(register): register(validate_log_translations) register(check_explicit_underscore_import) @@ -373,3 +387,4 @@ def factory(register): register(validate_assertTrue) register(validate_assertIsNone) register(check_uuid4) + register(no_log_warn_check) diff --git a/manila/tests/test_hacking.py b/manila/tests/test_hacking.py index bfe773c3..0e1a8505 100644 --- a/manila/tests/test_hacking.py +++ b/manila/tests/test_hacking.py @@ -329,3 +329,15 @@ class HackingTestCase(test.TestCase): hex_uuid = uuid.uuid4().hex """ self._assert_has_no_errors(code, checks.check_uuid4) + + def test_no_log_warn_check(self): + self.assertEqual(0, len(list(checks.no_log_warn_check( + "LOG.warning('This should not trigger LOG.warn " + "hacking check.')")))) + self.assertEqual(1, len(list(checks.no_log_warn_check( + "LOG.warn('We should not use LOG.warn')")))) + foo = """ + LOG.warn('Catch me too, please' + ) + """ + self.assertEqual(1, len(list(checks.no_log_warn_check(foo))))