mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Stop using the deprecated imp
module
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
parent
ad706711d3
commit
8331264fdb
2 changed files with 26 additions and 36 deletions
|
@ -8,7 +8,6 @@
|
|||
import collections
|
||||
import compileall
|
||||
import copy
|
||||
import imp
|
||||
import inspect
|
||||
import logging
|
||||
import os
|
||||
|
@ -35,15 +34,15 @@ log = logging.getLogger(__name__)
|
|||
|
||||
def remove_bytecode(module_path):
|
||||
paths = [module_path + "c"]
|
||||
if hasattr(imp, "get_tag"):
|
||||
modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
|
||||
paths.append(
|
||||
os.path.join(
|
||||
os.path.dirname(module_path),
|
||||
"__pycache__",
|
||||
"{}.{}.pyc".format(modname, imp.get_tag()),
|
||||
)
|
||||
cache_tag = sys.implementation.cache_tag
|
||||
modname, ext = os.path.splitext(module_path.split(os.sep)[-1])
|
||||
paths.append(
|
||||
os.path.join(
|
||||
os.path.dirname(module_path),
|
||||
"__pycache__",
|
||||
f"{modname}.{cache_tag}.pyc",
|
||||
)
|
||||
)
|
||||
for path in paths:
|
||||
if os.path.exists(path):
|
||||
os.unlink(path)
|
||||
|
@ -84,9 +83,7 @@ class LazyLoaderTest(TestCase):
|
|||
# Setup the module
|
||||
self.module_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
||||
self.addCleanup(shutil.rmtree, self.module_dir, ignore_errors=True)
|
||||
self.module_file = os.path.join(
|
||||
self.module_dir, "{}.py".format(self.module_name)
|
||||
)
|
||||
self.module_file = os.path.join(self.module_dir, f"{self.module_name}.py")
|
||||
with salt.utils.files.fopen(self.module_file, "w") as fh:
|
||||
fh.write(salt.utils.stringutils.to_str(loader_template))
|
||||
fh.flush()
|
||||
|
@ -163,16 +160,14 @@ class LazyLoaderUtilsTest(TestCase):
|
|||
def setUp(self):
|
||||
# Setup the module
|
||||
self.module_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
||||
self.module_file = os.path.join(
|
||||
self.module_dir, "{}.py".format(self.module_name)
|
||||
)
|
||||
self.module_file = os.path.join(self.module_dir, f"{self.module_name}.py")
|
||||
with salt.utils.files.fopen(self.module_file, "w") as fh:
|
||||
fh.write(salt.utils.stringutils.to_str(loader_template_module))
|
||||
fh.flush()
|
||||
os.fsync(fh.fileno())
|
||||
|
||||
self.utils_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
||||
self.utils_file = os.path.join(self.utils_dir, "{}.py".format(self.utils_name))
|
||||
self.utils_file = os.path.join(self.utils_dir, f"{self.utils_name}.py")
|
||||
with salt.utils.files.fopen(self.utils_file, "w") as fh:
|
||||
fh.write(salt.utils.stringutils.to_str(loader_template_utils))
|
||||
fh.flush()
|
||||
|
@ -516,7 +511,7 @@ class LazyLoaderSingleItem(TestCase):
|
|||
Checks that a KeyError is raised when the function key does not contain a '.'
|
||||
"""
|
||||
key = "testing_no_dot"
|
||||
expected = "The key '{}' should contain a '.'".format(key)
|
||||
expected = f"The key '{key}' should contain a '.'"
|
||||
with self.assertRaises(KeyError) as err:
|
||||
inspect.isfunction(self.loader["testing_no_dot"])
|
||||
|
||||
|
@ -619,7 +614,7 @@ class LazyLoaderReloadingTest(TestCase):
|
|||
|
||||
@property
|
||||
def module_path(self):
|
||||
return os.path.join(self.tmp_dir, "{}.py".format(self.module_name))
|
||||
return os.path.join(self.tmp_dir, f"{self.module_name}.py")
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_alias(self):
|
||||
|
@ -630,17 +625,15 @@ class LazyLoaderReloadingTest(TestCase):
|
|||
self.assertNotIn(self.module_key, self.loader)
|
||||
|
||||
self.update_module()
|
||||
self.assertNotIn("{}.test_alias".format(self.module_name), self.loader)
|
||||
self.assertNotIn(f"{self.module_name}.test_alias", self.loader)
|
||||
self.assertTrue(
|
||||
isinstance(
|
||||
self.loader["{}.working_alias".format(self.module_name)],
|
||||
self.loader[f"{self.module_name}.working_alias"],
|
||||
salt.loader.lazy.LoadedFunc,
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
inspect.isfunction(
|
||||
self.loader["{}.working_alias".format(self.module_name)].func
|
||||
)
|
||||
inspect.isfunction(self.loader[f"{self.module_name}.working_alias"].func)
|
||||
)
|
||||
|
||||
@pytest.mark.slow_test
|
||||
|
@ -802,7 +795,7 @@ class LazyLoaderVirtualAliasTest(TestCase):
|
|||
|
||||
@property
|
||||
def module_path(self):
|
||||
return os.path.join(self.tmp_dir, "{}.py".format(self.module_name))
|
||||
return os.path.join(self.tmp_dir, f"{self.module_name}.py")
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_virtual_alias(self):
|
||||
|
@ -1199,7 +1192,7 @@ class LazyLoaderDeepSubmodReloadingTest(TestCase):
|
|||
"__salt__": self.minion_mods,
|
||||
},
|
||||
)
|
||||
self.assertIn("{}.top".format(self.module_name), self.loader)
|
||||
self.assertIn(f"{self.module_name}.top", self.loader)
|
||||
|
||||
def tearDown(self):
|
||||
del self.tmp_dir
|
||||
|
@ -1241,7 +1234,7 @@ class LazyLoaderDeepSubmodReloadingTest(TestCase):
|
|||
|
||||
@pytest.mark.slow_test
|
||||
def test_basic(self):
|
||||
self.assertIn("{}.top".format(self.module_name), self.loader)
|
||||
self.assertIn(f"{self.module_name}.top", self.loader)
|
||||
|
||||
def _verify_libs(self):
|
||||
for lib in self.libs:
|
||||
|
@ -1549,9 +1542,7 @@ class LazyLoaderOptimizationOrderTest(TestCase):
|
|||
# Setup the module
|
||||
self.module_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
||||
self.addCleanup(shutil.rmtree, self.module_dir, ignore_errors=True)
|
||||
self.module_file = os.path.join(
|
||||
self.module_dir, "{}.py".format(self.module_name)
|
||||
)
|
||||
self.module_file = os.path.join(self.module_dir, f"{self.module_name}.py")
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
|
@ -1585,7 +1576,7 @@ class LazyLoaderOptimizationOrderTest(TestCase):
|
|||
return "lazyloadertest.cpython-{}{}{}.pyc".format(
|
||||
sys.version_info[0],
|
||||
sys.version_info[1],
|
||||
"" if not optimize else ".opt-{}".format(optimize),
|
||||
"" if not optimize else f".opt-{optimize}",
|
||||
)
|
||||
|
||||
def _write_module_file(self):
|
||||
|
|
|
@ -4,6 +4,7 @@ import platform
|
|||
import posixpath
|
||||
import sys
|
||||
import tempfile
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
|
@ -40,14 +41,14 @@ class PathJoinTestCase(TestCase):
|
|||
def test_nix_paths(self):
|
||||
for idx, (parts, expected) in enumerate(self.NIX_PATHS):
|
||||
path = salt.utils.path.join(*parts)
|
||||
assert "{}: {}".format(idx, path) == "{}: {}".format(idx, expected)
|
||||
assert f"{idx}: {path}" == f"{idx}: {expected}"
|
||||
|
||||
@pytest.mark.skip(reason="Skipped until properly mocked")
|
||||
@pytest.mark.skip_unless_on_windows
|
||||
def test_windows_paths(self):
|
||||
for idx, (parts, expected) in enumerate(self.WIN_PATHS):
|
||||
path = salt.utils.path.join(*parts)
|
||||
assert "{}: {}".format(idx, path) == "{}: {}".format(idx, expected)
|
||||
assert f"{idx}: {path}" == f"{idx}: {expected}"
|
||||
|
||||
@pytest.mark.skip(reason="Skipped until properly mocked")
|
||||
@pytest.mark.skip_on_windows
|
||||
|
@ -57,7 +58,7 @@ class PathJoinTestCase(TestCase):
|
|||
try:
|
||||
for idx, (parts, expected) in enumerate(self.WIN_PATHS):
|
||||
path = salt.utils.path.join(*parts)
|
||||
assert "{}: {}".format(idx, path) == "{}: {}".format(idx, expected)
|
||||
assert f"{idx}: {path}" == f"{idx}: {expected}"
|
||||
finally:
|
||||
self.__unpatch_path()
|
||||
|
||||
|
@ -79,14 +80,12 @@ class PathJoinTestCase(TestCase):
|
|||
assert actual == expected
|
||||
|
||||
def __patch_path(self):
|
||||
import imp
|
||||
|
||||
modules = list(self.BUILTIN_MODULES[:])
|
||||
modules.pop(modules.index("posix"))
|
||||
modules.append("nt")
|
||||
|
||||
code = """'''Salt unittest loaded NT module'''"""
|
||||
module = imp.new_module("nt")
|
||||
module = types.ModuleType("nt")
|
||||
exec(code, module.__dict__)
|
||||
sys.modules["nt"] = module
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue