From ba8c41486c18792a211835f90f83d05ba9e50155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Fri, 21 Aug 2020 08:00:08 +0200 Subject: [PATCH 1/2] Load audioservices using importlib instead of imp imp has been deprecated for quite some time --- mycroft/audio/audioservice.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/mycroft/audio/audioservice.py b/mycroft/audio/audioservice.py index ffefc45f76..99e5faed4f 100644 --- a/mycroft/audio/audioservice.py +++ b/mycroft/audio/audioservice.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import imp +import importlib import sys import time from os import listdir @@ -31,7 +31,7 @@ MAINMODULE = '__init__' sys.path.append(abspath(dirname(__file__))) -def create_service_descriptor(service_folder): +def create_service_spec(service_folder): """Prepares a descriptor that can be used together with imp. Args: @@ -40,7 +40,11 @@ def create_service_descriptor(service_folder): Returns: Dict with import information """ - info = imp.find_module(MAINMODULE, [service_folder]) + module_name = basename(service_folder) + path = join(service_folder, MAINMODULE + '.py') + spec = importlib.util.spec_from_file_location(module_name, path) + mod = importlib.util.module_from_spec(spec) + info = {'spec': spec, 'mod': mod, 'module_name': module_name} return {"name": basename(service_folder), "info": info} @@ -67,7 +71,7 @@ def get_services(services_folder): not MAINMODULE + ".py" in listdir(name)): continue try: - services.append(create_service_descriptor(name)) + services.append(create_service_spec(name)) except Exception: LOG.error('Failed to create service from ' + name, exc_info=True) @@ -75,7 +79,7 @@ def get_services(services_folder): not MAINMODULE + ".py" in listdir(location)): continue try: - services.append(create_service_descriptor(location)) + services.append(create_service_spec(location)) except Exception: LOG.error('Failed to create service from ' + location, exc_info=True) @@ -100,8 +104,11 @@ def load_services(config, bus, path=None): for descriptor in service_directories: LOG.info('Loading ' + descriptor['name']) try: - service_module = imp.load_module(descriptor["name"] + MAINMODULE, - *descriptor["info"]) + service_module = descriptor['info']['mod'] + spec = descriptor['info']['spec'] + module_name = descriptor['info']['module_name'] + sys.modules[module_name] = service_module + spec.loader.exec_module(service_module) except Exception as e: LOG.error('Failed to import module ' + descriptor['name'] + '\n' + repr(e)) From e2a7fe9b2a3d2adc11a8ceacf5654c6ec1666ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20Forslund?= Date: Fri, 21 Aug 2020 08:00:57 +0200 Subject: [PATCH 2/2] Load test environment with importlib instead of imp --- .../integrationtests/skills/discover_tests.py | 16 ++------ test/integrationtests/skills/runner.py | 37 ++++++++++++++----- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/test/integrationtests/skills/discover_tests.py b/test/integrationtests/skills/discover_tests.py index 32d8479c2b..e6553a04ac 100644 --- a/test/integrationtests/skills/discover_tests.py +++ b/test/integrationtests/skills/discover_tests.py @@ -16,13 +16,14 @@ import pytest import glob import os -from os.path import exists, join, expanduser, abspath -import imp +from os.path import join, expanduser, abspath from mycroft.configuration import Configuration from test.integrationtests.skills.skill_tester import MockSkillsLoader from test.integrationtests.skills.skill_tester import SkillTest +from .runner import load_test_environment + def discover_tests(skills_dir): """ Find all tests for the skills in the default skill path, @@ -43,16 +44,7 @@ def discover_tests(skills_dir): for skill in skills: # Load test environment file - test_env = None - if exists(os.path.join(skill, 'test/__init__.py')): - module = imp.load_source(skill + '.test_env', - os.path.join(skill, 'test/__init__.py')) - if (hasattr(module, 'test_runner') and - callable(module.test_runner) or - hasattr(module, 'test_setup') and - callable(module.test_setup)): - test_env = module - + test_env = load_test_environment(skill) # Find all intent test files test_intent_files = [ (f, test_env) for f diff --git a/test/integrationtests/skills/runner.py b/test/integrationtests/skills/runner.py index 4c7f7849d7..a26f11e052 100644 --- a/test/integrationtests/skills/runner.py +++ b/test/integrationtests/skills/runner.py @@ -27,7 +27,7 @@ import unittest import os from os.path import exists import sys -import imp +import importlib import argparse from test.integrationtests.skills.skill_tester import MockSkillsLoader from test.integrationtests.skills.skill_tester import SkillTest @@ -47,6 +47,31 @@ HOME_DIR = os.path.dirname(args.skill_path + '/') sys.argv = sys.argv[:1] +def load_test_environment(skill): + """Load skill's test environment if present + + Arguments: + skill (str): path to skill root folder + + Returns: + Module if a valid test environment module was found else None + """ + test_env = None + test_env_path = os.path.join(skill, 'test/__init__.py') + if exists(test_env_path): + skill_env = skill + '.test_env' + spec = importlib.util.spec_from_file_location(skill_env, test_env_path) + module = importlib.util.module_from_spec(spec) + sys.modules[skill_env] = module + spec.loader.exec_module(module) + if (hasattr(module, 'test_runner') and + callable(module.test_runner) or + hasattr(module, 'test_setup') and + callable(module.test_setup)): + test_env = module + return test_env + + def discover_tests(): """Find skills with test files @@ -74,15 +99,7 @@ def discover_tests(): tests[skill] = test_intent_files # Load test environment script - test_env = None - if exists(os.path.join(skill, 'test/__init__.py')): - module = imp.load_source(skill + '.test_env', - os.path.join(skill, 'test/__init__.py')) - if (hasattr(module, 'test_runner') and - callable(module.test_runner) or - hasattr(module, 'test_setup') and - callable(module.test_setup)): - test_env = module + test_env = load_test_environment(skill) test_envs[skill] = test_env return tests, test_envs