Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/psyclone/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@
'''

import argparse
import importlib
import logging
import os
import pathlib
import shutil
import sys
import traceback
import importlib
import shutil
from typing import Callable, Iterable, List, Optional, Tuple, Union
import logging

from fparser.api import get_reader
from fparser.two import Fortran2003
Expand Down Expand Up @@ -149,12 +150,12 @@ def load_script(
raise GenerationError(
f"generator: expected the script file '{filename}' to have "
f"the '.py' extension")
# prepend file path - if none, the empty string equates to the current
# working directory - to the system path to guarantee we find the user
# provided module instead of a similarly named module that might
# already exist elsewhere in the system path

sys.path.insert(0, filepath)
recipe_module = importlib.import_module(module_name)
script_path = pathlib.Path(script_name)
spec = importlib.util.spec_from_file_location(module_name, script_path)
recipe_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(recipe_module)

if hasattr(recipe_module, "FILES_TO_SKIP"):
files_to_skip = recipe_module.FILES_TO_SKIP
Expand Down Expand Up @@ -287,6 +288,7 @@ def generate(filename: str,
# Apply provided recipe to PSyIR
recipe, _, _ = load_script(script_name)
recipe(psy.container.root)
del sys.path[0]
alg_gen = None

elif api in GOCEAN_API_NAMES or (api in LFRIC_API_NAMES and LFRIC_TESTING):
Expand Down Expand Up @@ -340,6 +342,7 @@ def generate(filename: str,
is_optional=True)
if recipe:
recipe(psyir)
del sys.path[0]

# For each kernel called from the algorithm layer
kernels = {}
Expand Down Expand Up @@ -428,6 +431,7 @@ def generate(filename: str,
# Call the optimisation script for psy-layer optimisations
recipe, _, _ = load_script(script_name)
recipe(psy.container.root)
del sys.path[0]

# TODO issue #1618 remove Alg class and tests from PSyclone
if api in LFRIC_API_NAMES and not LFRIC_TESTING:
Expand Down Expand Up @@ -979,3 +983,5 @@ def code_transformation_mode(input_file, recipe_file, output_file,
else:
print(f"File '{input_file}' skipped because it is listed in "
"FILES_TO_SKIP.", file=sys.stdout)

del sys.path[0]
15 changes: 8 additions & 7 deletions src/psyclone/tests/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,26 +540,27 @@ def test_script_null_trans(script_factory):
'\n'.join(str(psy2).split('\n')[1:])


def test_script_null_trans_relative(script_factory):
def test_script_null_trans_relative(script_factory, monkeypatch, tmpdir):
'''Checks that generator.py works correctly when the trans() function
in a valid script file does no transformations (it simply passes
input to output). In this case the valid script file contains no
path and must therefore be found via the PYTHOPATH path list.
path, but is invoked from the script's directory, as a relative
path.

'''
alg1, psy1 = generate(os.path.join(BASE_PATH, "lfric",
"1_single_invoke.f90"),
api="lfric")
empty_script = script_factory("def trans(psyir):\n pass")
basename = os.path.basename(empty_script)
path = os.path.dirname(empty_script)
# Set the script directory in the PYTHONPATH
os.sys.path.append(path)

# Change into the script's directory so it's found as a relative import.
monkeypatch.chdir(tmpdir)

alg2, psy2 = generate(os.path.join(BASE_PATH, "lfric",
"1_single_invoke.f90"),
api="lfric", script_name=basename)
# Remove the path from PYTHONPATH
os.sys.path.pop()

# we need to remove the first line before comparing output as
# this line is an instance specific header
assert '\n'.join(str(alg1).split('\n')[1:]) == \
Expand Down
Loading