88import sys
99import traceback
1010from contextlib import contextmanager
11- from importlib import util as importlib_util
1211from os import path
1312from pathlib import Path
1413from typing import Optional
3130
3231def load_impls (step_impl_dirs = impl_dirs , project_root = project_root ):
3332 """ project_root can be overwritten in tests! """
33+
3434 os .chdir (project_root )
35- logger . debug ( 'Project root: {}' . format ( project_root ))
35+
3636 for impl_dir in step_impl_dirs :
37+
3738 resolved_impl_dir = Path (impl_dir ).resolve ()
3839 if not resolved_impl_dir .is_dir ():
3940 logger .error ('Cannot import step implementations. Error: {} does not exist.' .format (impl_dir ))
4041 logger .error ('Make sure `STEP_IMPL_DIR` env var is set to a valid directory path.' )
4142 return
4243
43- base_dir = project_root if str (resolved_impl_dir ).startswith (project_root ) else os .path .dirname (resolved_impl_dir )
44+ base_dir = os .path .commonpath ([project_root , f"{ resolved_impl_dir } " ])
45+ logger .debug ("Base directory '{}' of '{}'" .format (base_dir , resolved_impl_dir ))
4446
45- # Add temporary sys path for imports outside the project root
4647 temporary_sys_path = None
47- if base_dir != project_root :
48- logger .debug ('Found different base directory compared to the project root: {}' .format (base_dir , f"{ resolved_impl_dir } " ))
48+ if project_root != base_dir :
4949 temporary_sys_path = base_dir
5050
5151 _import_impl (base_dir , resolved_impl_dir , temporary_sys_path )
@@ -68,10 +68,8 @@ def copy_skel_files():
6868
6969def _import_impl (base_dir : str , absolute_step_impl_dir : str , temporary_sys_path : Optional [str ]):
7070 for python_file in glob .glob (f"{ absolute_step_impl_dir } /**/*.py" , recursive = True ):
71- if python_file .endswith ("__init__.py" ):
72- continue
73- relative_path = os .path .normpath (python_file .replace (base_dir + os .path .sep , '' ))
74- module_name = os .path .splitext (relative_path .replace (os .path .sep , '.' ))[0 ]
71+ relative_path = Path (python_file ).relative_to (base_dir )
72+ module_name = "." .join (relative_path .parts ).replace (".py" , "" )
7573 _import_file (module_name , python_file , temporary_sys_path )
7674
7775@contextmanager
0 commit comments