Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error handling for managed site installations #13

Merged
merged 1 commit into from
Dec 8, 2024
Merged
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
20 changes: 18 additions & 2 deletions src/maturin_import_hook/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path
from typing import Optional

from maturin_import_hook import project_importer, rust_file_importer
from maturin_import_hook._building import get_default_build_dir
from maturin_import_hook._site import (
get_sitecustomize_path,
Expand Down Expand Up @@ -94,6 +95,8 @@ def _action_site_info(format_name: str) -> None:
"usercustomize_path": str(usercustomize_path),
"usercustomize_exists": usercustomize_path.exists(),
"usercustomize_import_hook_installed": has_automatic_installation(usercustomize_path),
"project_importer_installed": project_importer.is_installed(),
"rust_file_importer_installed": rust_file_importer.is_installed(),
},
format_name,
)
Expand All @@ -108,8 +111,21 @@ def _action_site_install(
enable_rs_file_importer: bool,
detect_uv: bool,
) -> None:
module_path = get_usercustomize_path() if user else get_sitecustomize_path()
insert_automatic_installation(module_path, force, args, enable_project_importer, enable_rs_file_importer, detect_uv)
if user:
module_path = get_usercustomize_path()
uninstall_command = "python -m maturin_import_hook site uninstall --user"
else:
module_path = get_sitecustomize_path()
uninstall_command = "python -m maturin_import_hook site uninstall"
insert_automatic_installation(
module_path,
uninstall_command,
force,
args,
enable_project_importer,
enable_rs_file_importer,
detect_uv,
)


def _action_site_uninstall(*, user: bool) -> None:
Expand Down
20 changes: 10 additions & 10 deletions src/maturin_import_hook/_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@
from maturin_import_hook._logging import logger
from maturin_import_hook.settings import MaturinSettings

MANAGED_INSTALL_START = "# <maturin_import_hook>"
MANAGED_INSTALL_START = "# <maturin_import_hook>\n"
MANAGED_INSTALL_END = "# </maturin_import_hook>\n"
MANAGED_INSTALL_COMMENT = """
MANAGED_INSTALL_TEMPLATE = """\
# the following installs the maturin import hook during startup.
# see: `python -m maturin_import_hook site`
"""

INSTALL_TEMPLATE = """\
try:
import maturin_import_hook
from maturin_import_hook.settings import MaturinSettings
except ImportError:
pass
else:
maturin_import_hook.install(
settings=MaturinSettings(
{settings}
),
enable_project_importer={enable_project_importer},
enable_rs_file_importer={enable_rs_file_importer},
)
except Exception as e:
raise RuntimeError(
f"{{e}}\\n>> ERROR in managed maturin_import_hook installation. "
"Remove with `{uninstall_command}`\\n",
)
"""


Expand Down Expand Up @@ -104,6 +103,7 @@ def _should_use_uv() -> bool:

def insert_automatic_installation(
module_path: Path,
uninstall_command: str,
force: bool,
args: Optional[str],
enable_project_importer: bool,
Expand Down Expand Up @@ -142,11 +142,11 @@ def insert_automatic_installation(

parts.extend([
MANAGED_INSTALL_START,
MANAGED_INSTALL_COMMENT,
INSTALL_TEMPLATE.format(
MANAGED_INSTALL_TEMPLATE.format(
settings=",\n ".join(f"{k}={v!r}" for k, v in non_default_settings.items()),
enable_project_importer=repr(enable_project_importer),
enable_rs_file_importer=repr(enable_rs_file_importer),
uninstall_command=uninstall_command,
),
MANAGED_INSTALL_END,
])
Expand Down
30 changes: 21 additions & 9 deletions tests/test_import_hook/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_automatic_site_installation(tmp_path: Path) -> None:

insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=False,
args=None,
enable_project_importer=True,
Expand All @@ -36,6 +37,7 @@ def test_automatic_site_installation(tmp_path: Path) -> None:
with capture_logs() as cap:
insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=False,
args=None,
enable_project_importer=True,
Expand All @@ -56,16 +58,18 @@ def test_automatic_site_installation(tmp_path: Path) -> None:
try:
import maturin_import_hook
from maturin_import_hook.settings import MaturinSettings
except ImportError:
pass
else:
maturin_import_hook.install(
settings=MaturinSettings(
color=True
),
enable_project_importer=True,
enable_rs_file_importer=True,
)
except Exception as e:
raise RuntimeError(
f"{e}\\n>> ERROR in managed maturin_import_hook installation. "
"Remove with `<uninstall>`\\n",
)
# </maturin_import_hook>
""")

Expand Down Expand Up @@ -107,6 +111,7 @@ def test_automatic_site_installation_force_overwrite(tmp_path: Path) -> None:

insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=False,
args=None,
enable_project_importer=True,
Expand All @@ -119,6 +124,7 @@ def test_automatic_site_installation_force_overwrite(tmp_path: Path) -> None:
with capture_logs() as cap:
insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=True,
args="--release",
enable_project_importer=True,
Expand All @@ -142,9 +148,6 @@ def test_automatic_site_installation_force_overwrite(tmp_path: Path) -> None:
try:
import maturin_import_hook
from maturin_import_hook.settings import MaturinSettings
except ImportError:
pass
else:
maturin_import_hook.install(
settings=MaturinSettings(
release=True,
Expand All @@ -153,6 +156,11 @@ def test_automatic_site_installation_force_overwrite(tmp_path: Path) -> None:
enable_project_importer=True,
enable_rs_file_importer=True,
)
except Exception as e:
raise RuntimeError(
f"{e}\\n>> ERROR in managed maturin_import_hook installation. "
"Remove with `<uninstall>`\\n",
)
# </maturin_import_hook>
""")

Expand All @@ -165,6 +173,7 @@ def test_automatic_site_installation_invalid_args(tmp_path: Path) -> None:
with pytest.raises(ValueError, match="argument parser error"):
insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=False,
args="--foo",
enable_project_importer=True,
Expand All @@ -178,6 +187,7 @@ def test_automatic_site_installation_empty(tmp_path: Path) -> None:
sitecustomize = tmp_path / "sitecustomize.py"
insert_automatic_installation(
sitecustomize,
"<uninstall>",
force=False,
args=None,
enable_project_importer=True,
Expand All @@ -192,16 +202,18 @@ def test_automatic_site_installation_empty(tmp_path: Path) -> None:
try:
import maturin_import_hook
from maturin_import_hook.settings import MaturinSettings
except ImportError:
pass
else:
maturin_import_hook.install(
settings=MaturinSettings(
color=True
),
enable_project_importer=True,
enable_rs_file_importer=True,
)
except Exception as e:
raise RuntimeError(
f"{e}\\n>> ERROR in managed maturin_import_hook installation. "
"Remove with `<uninstall>`\\n",
)
# </maturin_import_hook>
""")

Expand Down
Loading