Skip to content

Commit

Permalink
ENH: add error if we can't locate the project in the editable hook
Browse files Browse the repository at this point in the history
Signed-off-by: Filipe Laíns <lains@riseup.net>
  • Loading branch information
FFY00 committed Dec 28, 2022
1 parent c10fc01 commit d0011ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ def build_editable(self, directory: Path, verbose: bool = False) -> pathlib.Path
hook_module_name = f'_mesonpy_hook_{self.normalized_name.replace(".", "_")}'
hook_install_code = textwrap.dedent(f'''
MesonpyFinder.install(
project_name={_as_python_declaration(self._project.name)},
hook_name={_as_python_declaration(hook_module_name)},
project_path={_as_python_declaration(self._source_dir)},
build_path={_as_python_declaration(self._build_dir)},
import_paths={_as_python_declaration(import_paths)},
Expand Down
29 changes: 28 additions & 1 deletion mesonpy/_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,36 @@ class MesonpyFinder(importlib.abc.MetaPathFinder):

def __init__(
self,
project_name: str,
hook_name: str,
project_path: str,
build_path: str,
import_paths: List[str],
top_level_modules: List[str],
rebuild_commands: List[List[str]],
verbose: bool = False,
) -> None:
self._project_name = project_name
self._hook_name = hook_name
self._project_path = project_path
self._build_path = build_path
self._import_paths = import_paths
self._top_level_modules = top_level_modules
self._rebuild_commands = rebuild_commands
self._verbose = verbose

for path in (self._project_path, self._build_path):
if not os.path.isdir(path):
raise ImportError(
f'{path} is not a directory, but it is required to rebuild '
f'"{self._project_name}", which is installed in editable '
'mode. Please reinstall the project to get it back to '
'working condition. If there are any issues uninstalling '
'this installation, you can manually remove '
f'{self._hook_name} and {os.path.basename(__file__)}, '
f'located in {os.path.dirname(__file__)}.'
)

def __repr__(self) -> str:
return f'{self.__class__.__name__}({self._project_path})'

Expand Down Expand Up @@ -128,6 +144,8 @@ def find_spec(
@classmethod
def install(
cls,
project_name: str,
hook_name: str,
project_path: str,
build_path: str,
import_paths: List[str],
Expand All @@ -140,7 +158,16 @@ def install(
if os.environ.get('MESONPY_EDITABLE_VERBOSE', ''):
verbose = True
# install our finder
finder = cls(project_path, build_path, import_paths, top_level_modules, rebuild_commands, verbose)
finder = cls(
project_name,
hook_name,
project_path,
build_path,
import_paths,
top_level_modules,
rebuild_commands,
verbose,
)
if finder not in sys.meta_path:
# prepend our finder to sys.meta_path, so that it is queried before
# the normal finders, and can trigger a project rebuild
Expand Down

0 comments on commit d0011ec

Please sign in to comment.