Prevent new code from using namespaced oslo imports

Ib63da2d845843410634a1df0261af33b973daf32 is an example where
new code had to be fixed for oslo_concurrency imports. Let us
add a hacking check to prevent such regression. When we update
to new oslo libraries with non-namespace'd imports, we should
update the regexp in this hacking rule

Change-Id: I44536d477d06ddc1205b824bcb888b666405dce3
This commit is contained in:
Davanum Srinivas 2014-12-16 08:30:37 -05:00 committed by Davanum Srinivas (dims)
parent a7c105aadb
commit 19aa89149c
3 changed files with 21 additions and 0 deletions

View File

@ -45,6 +45,7 @@ Nova Specific Commandments
- [N330] Validate that LOG.warning messages use _LW.
- [N331] Change LOG.warn on LOG.warning.
- [N332] Check that the api_version decorator is the first decorator on a method
- [N333] Check for oslo library imports use the non-namespaced packages
Creating Unit Tests
-------------------

View File

@ -75,6 +75,10 @@ underscore_import_check = re.compile(r"(.)*import _(.)*")
custom_underscore_check = re.compile(r"(.)*_\s*=\s*(.)*")
api_version_re = re.compile(r"@.*api_version")
# TODO(dims): When other oslo libraries switch over non-namespace'd
# imports, we need to add them to the regexp below.
oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](concurrency)")
class BaseASTChecker(ast.NodeVisitor):
"""Provides a simple framework for writing AST-based checks.
@ -445,6 +449,14 @@ class CheckForTransAdd(BaseASTChecker):
super(CheckForTransAdd, self).generic_visit(node)
def check_oslo_namespace_imports(logical_line, blank_before, filename):
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(import_no_db_in_virt)
register(no_db_session_in_public_api)
@ -466,3 +478,4 @@ def factory(register):
register(check_api_version_decorator)
register(CheckForStrUnicodeExc)
register(CheckForTransAdd)
register(check_oslo_namespace_imports)

View File

@ -367,6 +367,13 @@ class HackingTestCase(test.NoDBTestCase):
self._assert_has_errors(code, checks.check_api_version_decorator,
expected_errors=[(2, 0, "N332")])
def test_oslo_namespace_imports_check(self):
code = """
from oslo.concurrency import processutils
"""
self._assert_has_errors(code, checks.check_oslo_namespace_imports,
expected_errors=[(1, 0, "N333")])
def test_trans_add(self):
checker = checks.CheckForTransAdd