|
| 1 | +""" |
| 2 | +Python DPF plugins utilities |
| 3 | +============================ |
| 4 | +
|
| 5 | +Contains the utilities specific to installing and using Python DPF plugins. |
| 6 | +""" |
| 7 | +import os.path |
| 8 | +try: |
| 9 | + import importlib.metadata as importlib_metadata |
| 10 | +except ImportError: # Python < 3.10 (backport) |
| 11 | + import importlib_metadata as importlib_metadata |
| 12 | + |
| 13 | +import ansys.dpf.core as dpf |
| 14 | +from ansys.dpf.core import server as server_module |
| 15 | + |
| 16 | + |
| 17 | +def load_plugin_on_server(plugin, server=None, symbol="load_operators"): |
| 18 | + """Load a DPF Python plugin on the global or given DPF server. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + plugin: |
| 23 | + DPF Python plugin to load. |
| 24 | + server: |
| 25 | + DPF server to load the plugin onto. |
| 26 | + symbol: |
| 27 | + Name of the function recording the operators in the plugin. |
| 28 | + """ |
| 29 | + server = server_module.get_or_create_server(server) |
| 30 | + plugin_name = plugin.split("-")[-1] |
| 31 | + tmp_folder = dpf.make_tmp_dir_server(server=server) |
| 32 | + |
| 33 | + # Get the path to the plugin from the package installation |
| 34 | + if len([p for p in importlib_metadata.files(plugin) if "__init__.py" in str(p)]) > 0: |
| 35 | + file_path = [p for p in importlib_metadata.files(plugin) if "__init__.py" in str(p)][0] |
| 36 | + plugin_path = str(os.path.dirname(file_path.locate())) |
| 37 | + # For some reason the "locate()" function returns a path with src doubled |
| 38 | + plugin_path = plugin_path.replace("src"+os.path.sep+"src", "src") |
| 39 | + elif len([p for p in importlib_metadata.files(plugin) if ".pth" in str(p)]) > 0: |
| 40 | + path_file = [p for p in importlib_metadata.files(plugin) if ".pth" in str(p)][0].locate() |
| 41 | + with open(path_file, "r") as file: |
| 42 | + plugin_path = file.readline()[:-1] |
| 43 | + plugin_path = os.path.join(plugin_path, "ansys", "dpf", "plugins", plugin_name) |
| 44 | + else: |
| 45 | + raise ModuleNotFoundError(f"Could not locate files for plugin {plugin}") |
| 46 | + |
| 47 | + target_plugin_path = dpf.path_utilities.join(tmp_folder, "ansys", "dpf", "plugins", plugin_name) |
| 48 | + target_xml_path = dpf.path_utilities.join(tmp_folder, "ansys", "dpf", "plugins") |
| 49 | + |
| 50 | + # Upload python files |
| 51 | + _ = dpf.upload_files_in_folder( |
| 52 | + target_plugin_path, |
| 53 | + plugin_path, |
| 54 | + specific_extension=".py", |
| 55 | + server=server, |
| 56 | + ) |
| 57 | + |
| 58 | + # upload zip files (site-packages) |
| 59 | + _ = dpf.upload_files_in_folder( |
| 60 | + target_plugin_path, |
| 61 | + plugin_path, |
| 62 | + specific_extension=".zip", |
| 63 | + server=server, |
| 64 | + ) |
| 65 | + |
| 66 | + # Upload xml file for the plugin |
| 67 | + _ = dpf.upload_files_in_folder( |
| 68 | + target_xml_path, |
| 69 | + os.path.join(plugin_path, os.pardir), |
| 70 | + specific_extension=".xml", |
| 71 | + server=server, |
| 72 | + ) |
| 73 | + |
| 74 | + # Load the plugin on the server |
| 75 | + dpf.load_library( |
| 76 | + filename=target_plugin_path, |
| 77 | + name=f"py_{plugin_name}", |
| 78 | + symbol=symbol, |
| 79 | + server=server, |
| 80 | + ) |
0 commit comments