common: Replace deprecated importlib API (2/2)

Resolve the following deprecation warning:

  DeprecationWarning: the load_module() method is deprecated and slated
  for removal in Python 3.12; use exec_module() instead

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: I403289d5df2143872503fc246a993cb19ebba68e
This commit is contained in:
Stephen Finucane 2023-09-21 11:29:02 +01:00 committed by Takashi Kajinami
parent 559e840053
commit d48a6a2170
3 changed files with 7 additions and 7 deletions

View File

@ -33,8 +33,9 @@ from oslo_config import cfg
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(BASE_DIR, "..", ".."))
CONTRIB_DIR = os.path.join(ROOT, 'contrib')
PLUGIN_DIRS = glob.glob(os.path.join(CONTRIB_DIR, '*'))
# TODO(tkajinam): Fix this
# CONTRIB_DIR = os.path.join(ROOT, 'contrib')
# PLUGIN_DIRS = glob.glob(os.path.join(CONTRIB_DIR, '*'))
ENV_DIR = os.path.join(ROOT, "etc", "heat", "environment.d")
TEMP_ENV_DIR = tempfile.mkdtemp()
@ -48,7 +49,7 @@ sys.path.insert(0, ROOT)
sys.path.insert(0, BASE_DIR)
cfg.CONF.import_opt('plugin_dirs', 'heat.common.config')
cfg.CONF.set_override(name='plugin_dirs', override=PLUGIN_DIRS)
# cfg.CONF.set_override(name='plugin_dirs', override=PLUGIN_DIRS)
cfg.CONF.import_opt('environment_dir', 'heat.common.config')
cfg.CONF.set_override(name='environment_dir', override=TEMP_ENV_DIR)

View File

@ -20,6 +20,7 @@ for them before loading them.
"""
import functools
import importlib.util
import pkgutil
import sys
import types
@ -70,7 +71,8 @@ def _import_module(importer, module_name, package):
if module_spec is None:
return None
module = module_spec.loader.load_module(module_name)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
# Make this accessible through the parent package for static imports
local_name = module_name.partition(package.__name__ + '.')[2]

View File

@ -12,7 +12,6 @@
# under the License.
import os.path
import sys
from unittest import mock
import fixtures
@ -183,7 +182,6 @@ def constraint_mapping():
plugin_file = os.path.join(plugin_dir.path, 'test.py')
with open(plugin_file, 'w+') as ef:
ef.write(constraint_content)
self.addCleanup(sys.modules.pop, "heat.engine.plugins.test")
cfg.CONF.set_override('plugin_dirs', plugin_dir.path)
env = environment.Environment({})
@ -202,7 +200,6 @@ def constraint_mapping():
plugin_file = os.path.join(plugin_dir.path, 'test.py')
with open(plugin_file, 'w+') as ef:
ef.write(constraint_content)
self.addCleanup(sys.modules.pop, "heat.engine.plugins.test")
cfg.CONF.set_override('plugin_dirs', plugin_dir.path)
env = environment.Environment({})