Skip to content

Commit 1ff3c0f

Browse files
authored
fix(models): include files in nested folders (#201)
Some of the model directories have nested folders containing direct comparison files, input files for comparison models, or other supporting data.
1 parent 642c2d1 commit 1ff3c0f

7 files changed

Lines changed: 10700 additions & 117 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,6 @@ autotest/temp/
149149

150150
# uv lockfile
151151
uv.lock
152+
153+
# filelock files
154+
**/*.lock

autotest/test_models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ def test_get_examples(example_name, model_names):
4444

4545
@pytest.mark.parametrize("model_name, files", list(islice(MODELMAP.items(), TAKE)))
4646
def test_copy_to(model_name, files, tmp_path):
47-
workspace = models.copy_to(tmp_path, model_name)
47+
workspace = models.copy_to(tmp_path, model_name, verbose=True)
4848
assert workspace.exists(), f"Model {model_name} was not copied to {tmp_path}"
4949
assert workspace.is_dir(), f"Model {model_name} is not a directory"
50-
assert len(list(workspace.iterdir())) == len(files), (
51-
f"Model {model_name} does not have the correct number of files"
50+
found = [p for p in workspace.rglob("*") if p.is_file()]
51+
assert len(found) == len(files), (
52+
f"Model {model_name} does not have the correct number of files, "
53+
f"expected {len(files)}, got {len(found)}"
5254
)
5355
if "mf6" in model_name:
5456
assert any(Path(f).name == "mfsim.nam" for f in files)

modflow_devtools/make_registry.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,30 @@ def write_registry(
4545
if is_zip := url.endswith((".zip", ".tar")):
4646
registry[url.rpartition("/")[2]] = {"hash": None, "url": url}
4747

48-
def _find_examples_dir(p):
49-
while p.name != "examples":
50-
p = p.parent
51-
return p
52-
5348
model_paths = get_model_paths(path)
5449
for model_path in model_paths:
5550
# TODO: the renaming is only necessary because we're attaching auto-
5651
# generated functions to the models module. If we can live without,
5752
# and get by with a single function that takes model name as an arg,
5853
# then the model names could correspond directly to directory names.
5954
model_path = model_path.expanduser().absolute()
60-
base_path = _find_examples_dir(model_path) if is_zip else path
61-
rel_path = model_path.relative_to(base_path)
55+
rel_path = model_path.relative_to(path)
6256
model_name = "/".join(rel_path.parts)
6357
modelmap[model_name] = []
6458
if is_zip:
6559
if rel_path.parts[0] not in examples:
6660
examples[rel_path.parts[0]] = []
6761
examples[rel_path.parts[0]].append(model_name)
68-
for p in model_path.glob("*"):
62+
for p in model_path.rglob("*"):
6963
if not p.is_file() or any(e in p.name for e in exclude):
7064
continue
7165
if is_zip:
72-
relpath = p.expanduser().absolute().relative_to(base_path)
66+
relpath = p.expanduser().absolute().relative_to(path)
7367
name = "/".join(relpath.parts)
7468
url_ = url
7569
hash = None
7670
else:
77-
relpath = p.expanduser().absolute().relative_to(base_path)
71+
relpath = p.expanduser().absolute().relative_to(path)
7872
name = "/".join(relpath.parts)
7973
url_ = f"{url}/{relpath!s}"
8074
hash = _sha256(p)

modflow_devtools/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,19 @@ def copy_to(
120120
) -> Path | None:
121121
if not any(files := FETCHERS[model_name]()):
122122
return None
123+
# create the workspace if needed
123124
workspace = Path(workspace).expanduser().absolute()
124125
if verbose:
125126
print(f"Creating workspace {workspace}")
126127
workspace.mkdir(parents=True, exist_ok=True)
128+
# copy the files. some might be in nested folders,
129+
# but the first is guaranteed not to be, so use it
130+
# to determine relative path in the new workspace.
131+
base = Path(files[0]).parent
127132
for file in files:
128133
if verbose:
129134
print(f"Copying {file} to workspace")
130-
copy(file, workspace)
135+
path = workspace / file.relative_to(base)
136+
path.parent.mkdir(parents=True, exist_ok=True)
137+
copy(file, workspace / file.relative_to(base))
131138
return workspace

modflow_devtools/registry/examples.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ ex-gwf-csub-p02b = [
5656
]
5757
ex-gwf-csub-p02c = [
5858
"ex-gwf-csub-p02c/es-001",
59-
"ex-gwf-csub-p02c/es-020",
59+
"ex-gwf-csub-p02c/hb-001",
6060
"ex-gwf-csub-p02c/hb-002",
61-
"ex-gwf-csub-p02c/hb-100",
62-
"ex-gwf-csub-p02c/hb-050",
61+
"ex-gwf-csub-p02c/es-100",
6362
"ex-gwf-csub-p02c/hb-005",
63+
"ex-gwf-csub-p02c/hb-100",
64+
"ex-gwf-csub-p02c/es-020",
65+
"ex-gwf-csub-p02c/es-010",
66+
"ex-gwf-csub-p02c/hb-020",
6467
"ex-gwf-csub-p02c/hb-010",
6568
"ex-gwf-csub-p02c/es-050",
66-
"ex-gwf-csub-p02c/hb-001",
6769
"ex-gwf-csub-p02c/es-005",
70+
"ex-gwf-csub-p02c/hb-050",
6871
"ex-gwf-csub-p02c/es-002",
69-
"ex-gwf-csub-p02c/es-100",
70-
"ex-gwf-csub-p02c/hb-020",
71-
"ex-gwf-csub-p02c/es-010",
7272
]
7373
ex-gwf-csub-p03a = [
7474
"ex-gwf-csub-p03a",

0 commit comments

Comments
 (0)