Fixes import_modules_recursively for Windows

The neutron agents fail to start on Windows, due to the fact that
neutron.common.utils.import_modules_recursively expects the paths
to be forwardslash separated, while on Windows they are backslash
separated.

Change-Id: I5e15273c2f70b70fad0cf1216ef0b33c6a99d702
Closes-Bug: #1694220
(cherry picked from commit 572deac00b)
This commit is contained in:
Claudiu Belu 2017-05-29 02:44:03 -07:00 committed by Ihar Hrachyshka
parent fbd60b0991
commit 8b116afd35
2 changed files with 13 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import importlib
import os
import os.path
import random
import re
import signal
import sys
import threading
@ -60,6 +61,8 @@ SYNCHRONIZED_PREFIX = 'neutron-'
DEFAULT_THROTTLER_VALUE = 2
_SEPARATOR_REGEX = re.compile(r'[/\\]+')
synchronized = lockutils.synchronized_with_prefix(SYNCHRONIZED_PREFIX)
@ -871,6 +874,7 @@ def extract_exc_details(e):
def import_modules_recursively(topdir):
'''Import and return all modules below the topdir directory.'''
topdir = _SEPARATOR_REGEX.sub('/', topdir)
modules = []
for root, dirs, files in os.walk(topdir):
for file_ in files:
@ -881,7 +885,7 @@ def import_modules_recursively(topdir):
if module == '__init__':
continue
import_base = root.replace('/', '.')
import_base = _SEPARATOR_REGEX.sub('.', root)
# NOTE(ihrachys): in Python3, or when we are not located in the
# directory containing neutron code, __file__ is absolute, so we

View File

@ -14,8 +14,10 @@
import os.path
import random
import re
import sys
import ddt
import eventlet
import mock
import netaddr
@ -804,17 +806,20 @@ class TestExcDetails(base.BaseTestCase):
utils.extract_exc_details(Exception()), six.text_type)
@ddt.ddt
class ImportModulesRecursivelyTestCase(base.BaseTestCase):
def test_recursion(self):
@ddt.data('/', r'\\')
def test_recursion(self, separator):
expected_modules = (
'neutron.tests.unit.tests.example.dir.example_module',
'neutron.tests.unit.tests.example.dir.subdir.example_module',
)
for module in expected_modules:
sys.modules.pop(module, None)
modules = utils.import_modules_recursively(
os.path.dirname(tests.__file__))
topdir = re.sub(r'[/\\]+', separator, os.path.dirname(tests.__file__))
modules = utils.import_modules_recursively(topdir)
for module in expected_modules:
self.assertIn(module, modules)
self.assertIn(module, sys.modules)