Skip to content

Commit

Permalink
Merge pull request #4306 from boegel/filetools_load_source
Browse files Browse the repository at this point in the history
copy `load_source` function from `easybuild.tools.py2vs3.py3` to `easybuild.tools.filetools` so it can be used `easybuild.tools.hooks`
  • Loading branch information
ocaisa authored Aug 3, 2023
2 parents d09bf2e + bef3d38 commit ef03b03
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion easybuild/tools/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions easybuild/tools/py2vs3/py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand Down

0 comments on commit ef03b03

Please sign in to comment.