From bef3d3873e0320f24ea9b210278818eafbb5f980 Mon Sep 17 00:00:00 2001 From: Kenneth Hoste Date: Wed, 2 Aug 2023 22:19:54 +0200 Subject: [PATCH] copy load_source function from easybuild.tools.py2vs3.py3 to easybuild.tools.filetools so it can be used easybuild.tools.hooks --- easybuild/tools/filetools.py | 9 +++++++++ easybuild/tools/hooks.py | 2 +- easybuild/tools/py2vs3/py3.py | 1 + test/framework/filetools.py | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index 698d38c391..c3faf7be00 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -58,6 +58,7 @@ import zlib from functools import partial from html.parser import HTMLParser +from importlib.util import spec_from_file_location, module_from_spec import urllib.request as std_urllib from easybuild.base import fancylogger @@ -2776,6 +2777,14 @@ def install_fake_vsc(): return fake_vsc_path +def load_source(filename, path): + """Load file as Python module""" + spec = spec_from_file_location(filename, path) + module = module_from_spec(spec) + spec.loader.exec_module(module) + return module + + def get_easyblock_class_name(path): """Make sure file is an easyblock and get easyblock class name""" fn = os.path.basename(path).split('.')[0] diff --git a/easybuild/tools/hooks.py b/easybuild/tools/hooks.py index 3c21d4e104..4f6b6fb37e 100644 --- a/easybuild/tools/hooks.py +++ b/easybuild/tools/hooks.py @@ -33,9 +33,9 @@ import os from easybuild.base import fancylogger -from easybuild.tools.py2vs3 import load_source from easybuild.tools.build_log import EasyBuildError, print_msg from easybuild.tools.config import build_option +from easybuild.tools.filetools import load_source _log = fancylogger.getLogger('hooks', fname=False) diff --git a/easybuild/tools/py2vs3/py3.py b/easybuild/tools/py2vs3/py3.py index 99707e0a0c..f37cbf0f08 100644 --- a/easybuild/tools/py2vs3/py3.py +++ b/easybuild/tools/py2vs3/py3.py @@ -66,6 +66,7 @@ string_type = str +# note: also available in easybuild.tools.filetools, should be imported from there! def load_source(filename, path): """Load file as Python module""" spec = spec_from_file_location(filename, path) diff --git a/test/framework/filetools.py b/test/framework/filetools.py index 7522c36cbf..da2ffb3c29 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -39,7 +39,9 @@ import stat import sys import tempfile +import textwrap import time +import types from io import StringIO from test.framework.github import requires_github_access from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered, init_config @@ -3010,6 +3012,18 @@ def test_is_generic_easyblock(self): for name in ['EB_bzip2', 'EB_DL_underscore_POLY_underscore_Classic', 'EB_GCC', 'EB_WRF_minus_Fire']: self.assertFalse(ft.is_generic_easyblock(name)) + def test_load_source(self): + """Test for load_source function.""" + txt = textwrap.dedent(""" + def foobar(): + pass + """) + fp = os.path.join(self.test_prefix, 'foobar.py') + ft.write_file(fp, txt) + foobar = ft.load_source('foobar', fp) + self.assertTrue(isinstance(foobar, types.ModuleType)) + self.assertTrue(isinstance(foobar.foobar, types.FunctionType)) + def test_get_easyblock_class_name(self): """Test for get_easyblock_class_name function."""