| 1 |
--- a/python/dolfin/__init__.py |
| 2 |
+++ b/python/dolfin/__init__.py |
| 3 |
@@ -30,6 +30,32 @@ |
| 4 |
# sys.setdlopenflags(stored_dlopen_flags) |
| 5 |
# del sys |
| 6 |
|
| 7 |
+import pusimp |
| 8 |
+ |
| 9 |
+def pip_uninstall_call(executable: str, dependency_pypi_name: str, dependency_actual_path: str) -> str: |
| 10 |
+ """Report to the user how to uninstall a dependency with pip.""" |
| 11 |
+ output = f"{executable} -m pip uninstall --break-system-packages {dependency_pypi_name}" |
| 12 |
+ if dependency_actual_path.startswith("/usr"): |
| 13 |
+ return f"sudo {output}" |
| 14 |
+ else: |
| 15 |
+ return output |
| 16 |
+ |
| 17 |
+ |
| 18 |
+pusimp.prevent_user_site_imports( |
| 19 |
+ "dolfin", "apt", "https://fenicsproject.discourse.group/", |
| 20 |
+ "/usr/lib/python3/dist-packages", |
| 21 |
+ ["dijitso", "ffc", "FIAT", "ufl_legacy", "ufl"], |
| 22 |
+ ["fenics-dijitso", "fenics-ffc", "fenics-fiat", "fenics-ufl-legacy", "fenics-ufl"], |
| 23 |
+ [False, False, False, False, True], |
| 24 |
+ ["", "", "", "", ( |
| 25 |
+ "Be aware that legacy dolfin codes must now import ufl_legacy instead of ufl, " |
| 26 |
+ "see https://fenicsproject.discourse.group/t/announcement-ufl-legacy-and-legacy-dolfin/11583 ." |
| 27 |
+ )], |
| 28 |
+ pip_uninstall_call |
| 29 |
+) |
| 30 |
+ |
| 31 |
+del pip_uninstall_call, pusimp |
| 32 |
+ |
| 33 |
# Import cpp modules |
| 34 |
from .cpp import __version__ |
| 35 |
|
| 36 |
--- /dev/null |
| 37 |
+++ b/python/test/debian-only/test_pusimp.py |
| 38 |
@@ -0,0 +1,101 @@ |
| 39 |
+# Copyright (C) 2024 by the pusimp authors |
| 40 |
+# |
| 41 |
+# This file is part of pusimp for FEniCS. |
| 42 |
+# |
| 43 |
+# SPDX-License-Identifier: LGPL-3.0-or-later |
| 44 |
+"""Test DOLFIN patches.""" |
| 45 |
+ |
| 46 |
+import typing |
| 47 |
+ |
| 48 |
+import pytest |
| 49 |
+import requests |
| 50 |
+ |
| 51 |
+from pusimp.utils import ( |
| 52 |
+ assert_package_import_errors_with_local_packages, |
| 53 |
+ assert_package_import_errors_with_broken_non_optional_packages, |
| 54 |
+ assert_package_import_success_with_allowed_local_packages, |
| 55 |
+ assert_package_import_success_with_broken_optional_packages, |
| 56 |
+ assert_package_import_success_without_local_packages |
| 57 |
+) |
| 58 |
+ |
| 59 |
+try: |
| 60 |
+ response = requests.get("https://www.debian.org", timeout=5) |
| 61 |
+except requests.ConnectionError: |
| 62 |
+ has_internet = False |
| 63 |
+else: |
| 64 |
+ has_internet = True |
| 65 |
+ |
| 66 |
+UFL_LEGACY_WARNING = "legacy dolfin codes must now import ufl_legacy instead of ufl" |
| 67 |
+ |
| 68 |
+ |
| 69 |
+def pip_install_call(executable: str, dependency_pypi_name: str) -> str: |
| 70 |
+ """Command to be run to install a dependency with pip.""" |
| 71 |
+ return ( |
| 72 |
+ f"{executable} -m pip install --ignore-installed --break-system-packages {dependency_pypi_name}" |
| 73 |
+ # debian does not ship numpy 2.0 yet, and a pip install-ed numpy 2.0 would break binary compatibility |
| 74 |
+ + " 'numpy<2.0'" |
| 75 |
+ ) |
| 76 |
+ |
| 77 |
+ |
| 78 |
+def pip_uninstall_call(executable: str, dependency_pypi_name: str, dependency_actual_path: str) -> str: |
| 79 |
+ """Command to be run to uninstall a dependency with pip.""" |
| 80 |
+ return f"{executable} -m pip uninstall --break-system-packages {dependency_pypi_name}" |
| 81 |
+ |
| 82 |
+ |
| 83 |
+def test_dolfin_import_success_without_local_packages() -> None: |
| 84 |
+ """Test that dolfin imports correctly without any extra local packages.""" |
| 85 |
+ assert_package_import_success_without_local_packages( |
| 86 |
+ "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py" |
| 87 |
+ ) |
| 88 |
+ |
| 89 |
+ |
| 90 |
[email protected](not has_internet, reason="Requires downloading from pypi.org") |
| 91 |
[email protected]("dependencies_import_name,dependencies_pypi_name,dependencies_extra_error_message", [ |
| 92 |
+ (["ufl_legacy"], ["fenics-ufl-legacy"], []), |
| 93 |
+ (["ufl"], ["fenics-ufl"], [UFL_LEGACY_WARNING]), |
| 94 |
+ (["FIAT", "ufl_legacy"], ["fenics-fiat", "fenics-ufl-legacy"], []), |
| 95 |
+ (["ufl", "ufl_legacy"], ["fenics-ufl", "fenics-ufl-legacy"], [UFL_LEGACY_WARNING]) |
| 96 |
+]) |
| 97 |
+def test_dolfin_import_errors_with_local_packages( |
| 98 |
+ dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str], |
| 99 |
+ dependencies_extra_error_message: typing.List[str] |
| 100 |
+) -> None: |
| 101 |
+ """Test that dolfin fails to import with extra local packages.""" |
| 102 |
+ assert_package_import_errors_with_local_packages( |
| 103 |
+ "dolfin", dependencies_import_name, dependencies_pypi_name, dependencies_extra_error_message, |
| 104 |
+ pip_install_call, pip_uninstall_call |
| 105 |
+ ) |
| 106 |
+ |
| 107 |
+ |
| 108 |
[email protected](not has_internet, reason="Requires downloading from pypi.org") |
| 109 |
[email protected]("dependencies_import_name,dependencies_pypi_name", [ |
| 110 |
+ (["ufl_legacy"], ["fenics-ufl-legacy"]), |
| 111 |
+ (["ufl"], ["fenics-ufl"]) |
| 112 |
+]) |
| 113 |
+def test_dolfin_import_success_with_allowed_local_packages( |
| 114 |
+ dependencies_import_name: typing.List[str], dependencies_pypi_name: typing.List[str] |
| 115 |
+) -> None: |
| 116 |
+ """Test that dolfin imports correctly even with extra local packages when asked to allow user-site imports.""" |
| 117 |
+ assert_package_import_success_with_allowed_local_packages( |
| 118 |
+ "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", |
| 119 |
+ dependencies_import_name, dependencies_pypi_name, pip_install_call |
| 120 |
+ ) |
| 121 |
+ |
| 122 |
+ |
| 123 |
[email protected]("dependencies_import_name", [ |
| 124 |
+ ["ufl_legacy"], # also breaks ffc/compiler.py (line 121) |
| 125 |
+ ["ffc"] |
| 126 |
+]) |
| 127 |
+def test_dolfin_import_errors_with_broken_non_optional_packages(dependencies_import_name: typing.List[str]) -> None: |
| 128 |
+ """Test that dolfin fails to import when non-optional packages are broken.""" |
| 129 |
+ assert_package_import_errors_with_broken_non_optional_packages("dolfin", dependencies_import_name, [False]) |
| 130 |
+ |
| 131 |
+ |
| 132 |
[email protected]("dependencies_import_name", [ |
| 133 |
+ ["ufl"] |
| 134 |
+]) |
| 135 |
+def test_dolfin_import_success_with_broken_optional_packages(dependencies_import_name: typing.List[str]) -> None: |
| 136 |
+ """Test that dolfin fails to import imports correctly when optional packages are broken.""" |
| 137 |
+ assert_package_import_success_with_broken_optional_packages( |
| 138 |
+ "dolfin", "/usr/lib/petsc/lib/python3/dist-packages/dolfin/__init__.py", dependencies_import_name, [True] |
| 139 |
+ ) |