Skip to content
Open
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
46 changes: 46 additions & 0 deletions autotest/test_get_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
24 changes: 24 additions & 0 deletions flopy/utils/get_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
Loading