Commit 241c9484 authored by Xavier Thompson's avatar Xavier Thompson

Use importlib instead of imp for Python >= 3.5

The imp module is deprecated and disappears in Python 3.12.
In slapos.recipe.cmmi, importlib/imp is used to run scripts
loaded directly from files. The importlib version is the now
recommanded way for Python 3.5+.
parent a957650d
import errno import errno
import imp
import logging import logging
import os import os
import re import re
...@@ -17,6 +16,20 @@ from ..utils import ( ...@@ -17,6 +16,20 @@ from ..utils import (
# from slapos.recipe.build # from slapos.recipe.build
from .. import downloadunpacked from .. import downloadunpacked
if sys.version_info >= (3, 5):
# See https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
import importlib.util
def module_from_file_location(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
else:
# BBB Python2, Python < 3.5
from imp import load_source as module_from_file_location
startup_environ = os.environ.copy() startup_environ = os.environ.copy()
# backport of shlex.quote from Python 3.3 # backport of shlex.quote from Python 3.3
...@@ -132,7 +145,7 @@ class Recipe(EnvironMixin): ...@@ -132,7 +145,7 @@ class Recipe(EnvironMixin):
try: try:
if not is_temp: if not is_temp:
filename = os.path.abspath(filename) filename = os.path.abspath(filename)
module = imp.load_source('script', filename) module = module_from_file_location('script', filename)
script = getattr(module, callable.strip()) script = getattr(module, callable.strip())
try: try:
script(self.options, self.buildout, self.environ) script(self.options, self.buildout, self.environ)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment