Skip to content

Commit 642c2d1

Browse files
authored
fix: filelock when fetching zips (#200)
https://github.com/fatiando/pooch/pull/415/files
1 parent af441d1 commit 642c2d1

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

autotest/mf6examples.zip.lock

Whitespace-only changes.

modflow_devtools/models.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pooch
1414
import tomli
15+
from filelock import FileLock
1516

1617
import modflow_devtools
1718

@@ -26,9 +27,9 @@
2627

2728
# set up the pooch
2829
FETCHERS = {}
29-
REGISTRY: dict[str, str] = {}
30-
MODELMAP: dict[str, list[str]] = {}
31-
EXAMPLES: dict[str, list[str]] = {}
30+
REGISTRY: dict = {}
31+
MODELMAP: dict = {}
32+
EXAMPLES: dict = {}
3233
POOCH = pooch.create(
3334
path=pooch.os_cache(modflow_devtools.__name__.replace("_", "-")),
3435
base_url=BASE_URL,
@@ -42,12 +43,13 @@ def _fetch_files():
4243
return [Path(POOCH.fetch(fname)) for fname in file_names]
4344

4445
def _fetch_zip(zip_name):
45-
return [
46-
Path(f)
47-
for f in POOCH.fetch(
48-
zip_name, processor=pooch.Unzip(members=MODELMAP[model_name])
49-
)
50-
]
46+
with FileLock(f"{zip_name}.lock"):
47+
return [
48+
Path(f)
49+
for f in POOCH.fetch(
50+
zip_name, processor=pooch.Unzip(members=MODELMAP[model_name])
51+
)
52+
]
5153

5254
urls = [POOCH.registry[fname] for fname in file_names]
5355
if not any(url for url in urls) or set(urls) == {f"{BASE_URL}/{ZIP_NAME}"}:
@@ -62,10 +64,11 @@ def _fetch_zip(zip_name):
6264
with pkg_resources.open_binary(
6365
REGISTRY_ANCHOR, REGISTRY_FILE_NAME
6466
) as registry_file:
65-
registry = tomli.load(registry_file)
66-
urls = {k: v["url"] for k, v in registry.items() if v.get("url", None)}
67-
registry = {k: v.get("hash", None) for k, v in registry.items()}
68-
POOCH.registry = registry
67+
REGISTRY = tomli.load(registry_file)
68+
# extract urls then drop them, leaving a direct map of name to hash
69+
urls = {k: v["url"] for k, v in REGISTRY.items() if v.get("url", None)}
70+
REGISTRY = {k: v.get("hash", None) for k, v in REGISTRY.items()}
71+
POOCH.registry = REGISTRY
6972
POOCH.urls = urls
7073
except: # noqa: E722
7174
warn(
@@ -105,7 +108,7 @@ def get_examples() -> dict[str, list[str]]:
105108

106109

107110
def get_registry() -> dict[str, str]:
108-
return POOCH.registry
111+
return REGISTRY
109112

110113

111114
def list_models() -> list[str]:

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ dfn = [
7676
]
7777
models = [
7878
"boltons",
79+
"filelock",
7980
"pooch",
8081
"tomli",
8182
"tomli-w"
@@ -120,6 +121,7 @@ dfn = [
120121
]
121122
models = [
122123
"boltons",
124+
"filelock",
123125
"pooch",
124126
"tomli",
125127
"tomli-w"

0 commit comments

Comments
 (0)