From ecc63822b20a782b69a808457010712b8a610cb2 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Tue, 24 Nov 2026 12:55:56 -0800 Subject: [PATCH] feat(get-modflow): add option to install shared libraries in a lib directory On Linux and macOS, --libdir (libdir in run_main) installs shared libraries in a given directory, or in lib beside the bindir when given no value. The default is unchanged. Windows keeps them in the bindir. Closes #2895 Co-Authored-By: Claude Opus 5.5 --- autotest/test_get_modflow.py | 46 ++++++++++++++++++++++++++++++++++++ flopy/utils/get_modflow.py | 24 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/autotest/test_get_modflow.py b/autotest/test_get_modflow.py index e7b325c1d..d370345db 100644 --- a/autotest/test_get_modflow.py +++ b/autotest/test_get_modflow.py @@ -4,6 +4,7 @@ import os import sys import urllib.request +import zipfile from os.path import expandvars from pathlib import Path from platform import system @@ -330,6 +331,51 @@ def test_run_main_ostag_matches_asset_exactly(tmp_path, monkeypatch, asset): run_main(tmp_path, owner="me", repo="my-build", ostag="mac", quiet=True) +@pytest.mark.parametrize("libdir", [None, True, "custom"]) +def test_run_main_libdir(tmp_path, monkeypatch, libdir): + # shared libraries go to libdir, executables to bindir + release = { + "tag_name": "v1", + "html_url": "https://example.com", + "assets": [ + { + "name": "linux.zip", + "browser_download_url": "https://example.com/linux.zip", + "updated_at": "", + } + ], + } + monkeypatch.setattr(get_modflow, "get_release", lambda *args, **kwargs: release) + downloads = tmp_path / "downloads" + downloads.mkdir() + with zipfile.ZipFile(downloads / "my-build-v1-linux.zip", "w") as zipf: + zipf.writestr("bin/mf6", "") + zipf.writestr("bin/libmf6.so", "") + bindir = tmp_path / "bin" + bindir.mkdir() + if libdir == "custom": + libdir = tmp_path / "custom" + expected_libdir = libdir + elif libdir: + expected_libdir = tmp_path / "lib" + else: + expected_libdir = bindir + + run_main( + bindir, + owner="me", + repo="my-build", + ostag="linux", + downloads_dir=downloads, + libdir=libdir, + quiet=True, + ) + + assert (bindir / "mf6").is_file() + assert (expected_libdir / "libmf6.so").is_file() + assert not (bindir / "bin").exists() + + @flaky @requires_github @pytest.mark.parametrize("repo", repo_options.keys()) diff --git a/flopy/utils/get_modflow.py b/flopy/utils/get_modflow.py index d76c8474d..f135fedbc 100755 --- a/flopy/utils/get_modflow.py +++ b/flopy/utils/get_modflow.py @@ -395,6 +395,7 @@ def run_main( quiet=False, tries=None, retry_delay=None, + libdir=None, _is_cli=False, ): """Run main method to get MODFLOW and related programs. @@ -433,6 +434,10 @@ def run_main( retry_delay : float, optional Base seconds between retries, growing exponentially. Defaults to the ``GET_MODFLOW_RETRY_DELAY`` environment variable, or 2.0. + libdir : str, Path or bool, optional + Directory to install shared libraries on Linux and macOS, created if + needed. True selects ``lib`` beside ``bindir``. Default is ``bindir``. + On Windows, shared libraries are always installed in ``bindir``. _is_cli : bool, default False Control behavior of method if this is run as a command-line interface or as a Python function. @@ -704,6 +709,18 @@ def add_item(key, fname, do_chmod): pth = bindir / fname pth.chmod(pth.stat().st_mode | 0o111) + if libdir and "win" not in ostag: + libdir = bindir.parent / "lib" if libdir is True else Path(libdir).resolve() + libdir.mkdir(parents=True, exist_ok=True) + libs = [Path(f).name for f in extract if f.endswith(lib_suffix)] + if libs and not quiet: + print( + f"moving {len(libs)} shared " + f"librar{'ies' if len(libs) != 1 else 'y'} to '{libdir}'" + ) + for fname in libs: + shutil.move(bindir / fname, libdir / fname) + # Show listing if not quiet: if any(items): @@ -845,6 +862,13 @@ def cli_main(): help="Base seconds between retries, grows exponentially; default is " f"{http_retry_delay} (env: GET_MODFLOW_RETRY_DELAY).", ) + parser.add_argument( + "--libdir", + nargs="?", + const=True, + help="Directory to install shared libraries on Linux and macOS; " + "default is the bindir. Without a value, 'lib' beside the bindir.", + ) args = vars(parser.parse_args()) try: run_main(**args, _is_cli=True)