Skip to content
Merged
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
17 changes: 13 additions & 4 deletions HinetPy/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ def _extract_channel_sac(
The extracted SAC file name.
"""

filename = f"{channel.name}.{channel.component}.{suffix}"
if outdir != ".":
filename = os.path.join(outdir, filename)

cmd = [
"win2sac_32",
winfile,
Expand All @@ -376,12 +380,17 @@ def _extract_channel_sac(
channel.component,
channel.id,
)
if os.path.exists(filename):
logger.warning(
"No new data for %s.%s (%s), but %s already exists "
"on disk and may be stale.",
channel.name,
channel.component,
channel.id,
filename,
)
return None

filename = f"{channel.name}.{channel.component}.{suffix}"
if outdir != ".":
filename = os.path.join(outdir, filename)

if os.path.exists(filename): # some channels have no data
if suffix == "": # remove extra dot if suffix is empty
shutil.move(filename, filename[:-1])
Expand Down
74 changes: 74 additions & 0 deletions tests/test_stale_file_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
Regression tests for issue #152: stale/missing results reported silently.

Uses a fake Popen so no win2sac_32 binary, account, or network is needed.
"""

import logging
import os

from HinetPy import win32
from HinetPy.channel import Channel


def _make_channel():
return Channel(
id="3e81",
name="N.NGUH",
component="U",
latitude=35.0,
longitude=140.0,
unit="m/s",
gain=1000.0,
damping=0.7,
period=1.0,
preamplification=20.0,
lsb_value=0.001,
)


class _FakeStderr:
def __init__(self, text):
self._text = text

def read(self):
return self._text.encode()


class _FakeProc:
"""Mimics Popen as used in _extract_channel_sac (context manager)."""

def __init__(self, text):
self.stderr = _FakeStderr(text)

def __enter__(self):
return self

def __exit__(self, *args):
return False


def _fake_popen_no_data(*args, **kwargs):
return _FakeProc("Data for channel 3e81 not existed\n")


def test_no_data_warns_about_stale_file(tmp_path, monkeypatch, caplog):
channel = _make_channel()
stale = tmp_path / "N.NGUH.U.SAC"
stale.write_text("DATO VIEJO", encoding="utf8")
monkeypatch.setattr(win32, "Popen", _fake_popen_no_data)
with caplog.at_level(logging.WARNING, logger="HinetPy.win32"):
result = win32._extract_channel_sac("dummy.cnt", channel, outdir=str(tmp_path))
assert result is None
assert any("may be stale" in r.message for r in caplog.records)


def test_no_data_without_stale_file_warns_once(tmp_path, monkeypatch, caplog):
channel = _make_channel()
monkeypatch.setattr(win32, "Popen", _fake_popen_no_data)
with caplog.at_level(logging.WARNING, logger="HinetPy.win32"):
result = win32._extract_channel_sac("dummy.cnt", channel, outdir=str(tmp_path))
assert result is None
assert not any("may be stale" in r.message for r in caplog.records)
assert any("not exists" in r.message for r in caplog.records)
assert not os.path.exists(str(tmp_path / "N.NGUH.U.SAC"))
Loading