From d8301e58bfbe1590d38721485dc8516b05339d66 Mon Sep 17 00:00:00 2001 From: "Jay S. Bryant" Date: Tue, 13 Jan 2015 17:11:33 -0600 Subject: [PATCH] Add hacking check for oslo namespace usage We want to make sure that we don't have usage of the old oslo.concurrency naming slipping in with new changes where we should be using the oslo_concurrency namespace. This change adds a hacking check to avoid use of the deprecated namespace. As we convert more oslo libraries to the new namespace the check will be updated to enforce use of the new namespace. This hacking check is based upon the same N333 hacking check in Cinder. Change-Id: Ibec6d09e9d313c9e723f7542cedb9da5772d3de2 --- HACKING.rst | 1 + cinder/hacking/checks.py | 13 +++++++++++++ cinder/tests/test_hacking.py | 6 ++++++ cinder/volume/targets/iscsi.py | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/HACKING.rst b/HACKING.rst index 93e0df93528..bca0ccff7f7 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -13,6 +13,7 @@ Cinder Specific Commandments - [N323] Add check for explicit import of _() to ensure proper translation. - [N324] Enforce no use of LOG.audit messages. LOG.info should be used instead. - [N327] assert_called_once is not a valid Mock method. +- [N333] Ensure that oslo namespaces are used for namespaced libraries. General diff --git a/cinder/hacking/checks.py b/cinder/hacking/checks.py index 8256c0f1058..36da480acb7 100644 --- a/cinder/hacking/checks.py +++ b/cinder/hacking/checks.py @@ -41,6 +41,10 @@ underscore_import_check = re.compile(r"(.)*i18n\s+import\s+_(.)*") custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*") no_audit_log = re.compile(r"(.)*LOG\.audit(.)*") +# NOTE(jsbryant): When other oslo libraries switch over non-namespaced +# imports, we will need to add them to the regex below. +oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](concurrency)") + def no_vi_headers(physical_line, line_number, lines): """Check for vi editor configuration in source files. @@ -124,6 +128,14 @@ def check_assert_called_once(logical_line, filename): yield (pos, msg) +def check_oslo_namespace_imports(logical_line): + if re.match(oslo_namespace_imports, logical_line): + msg = ("N333: '%s' must be used instead of '%s'.") % ( + logical_line.replace('oslo.', 'oslo_'), + logical_line) + yield(0, msg) + + def factory(register): register(no_vi_headers) register(no_translate_debug_logs) @@ -131,3 +143,4 @@ def factory(register): register(check_explicit_underscore_import) register(check_no_log_audit) register(check_assert_called_once) + register(check_oslo_namespace_imports) diff --git a/cinder/tests/test_hacking.py b/cinder/tests/test_hacking.py index 7a6bf424e01..1849eecab9e 100644 --- a/cinder/tests/test_hacking.py +++ b/cinder/tests/test_hacking.py @@ -110,3 +110,9 @@ class HackingTestCase(test.TestCase): "LOG.audit('My test audit log')"))), 1) self.assertEqual(len(list(checks.check_no_log_audit( "LOG.info('My info test log.')"))), 0) + + def test_oslo_namespace_imports_check(self): + self.assertEqual(1, len(list(checks.check_oslo_namespace_imports( + "from oslo.concurrency import foo")))) + self.assertEqual(0, len(list(checks.check_oslo_namespace_imports( + "from oslo_concurrency import bar")))) diff --git a/cinder/volume/targets/iscsi.py b/cinder/volume/targets/iscsi.py index 21588b35b54..1197add95a7 100644 --- a/cinder/volume/targets/iscsi.py +++ b/cinder/volume/targets/iscsi.py @@ -10,7 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. -from oslo.concurrency import processutils +from oslo_concurrency import processutils from cinder import exception from cinder.i18n import _, _LW, _LE