Skip to content

Commit

Permalink
Merge pull request #302 from ocefpaf/fix_301
Browse files Browse the repository at this point in the history
Manually delete the file to avoid Windows aggressive file lock
  • Loading branch information
ocefpaf authored Apr 20, 2023
2 parents f4bece4 + 9daddbb commit 99c39de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion erddapy/core/netcdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Handles netCDF responses."""

import platform
from contextlib import contextmanager
from pathlib import Path
from typing import BinaryIO, Dict, Generator, Optional
Expand Down Expand Up @@ -27,9 +28,14 @@ def _tempnc(data: BinaryIO) -> Generator[str, None, None]:
"""Create a temporary netcdf file."""
from tempfile import NamedTemporaryFile

# Let windows handle the file cleanup to avoid its aggressive file lock.
delete = True
if platform.system().lower() == "windows":
delete = False

tmp = None
try:
tmp = NamedTemporaryFile(suffix=".nc", prefix="erddapy_")
tmp = NamedTemporaryFile(suffix=".nc", prefix="erddapy_", delete=delete)
tmp.write(data.read())
tmp.flush()
yield tmp.name
Expand Down
5 changes: 5 additions & 0 deletions tests/test_netcdf_handling.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test netCDF loading."""

import os
import platform

import pytest

Expand All @@ -23,6 +24,10 @@ def test__nc_dataset_in_memory_https():

@pytest.mark.web
@pytest.mark.vcr()
@pytest.mark.skipif(
platform.system().lower() == "windows",
reason="does not remove the file on windows",
)
def test__tempnc():
"""Test temporary netcdf file."""
url = "http://erddap.ioos.us/erddap/tabledap/allDatasets.nc" # noqa
Expand Down

0 comments on commit 99c39de

Please sign in to comment.