diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py index a22a89cb1ec584af1b8fac28c70ee98f0aa0c97b..49febfd9f5ca0b2bdb7fa75bfa5aedfe5fad756d 100644 --- a/cal_tools/cal_tools/tools.py +++ b/cal_tools/cal_tools/tools.py @@ -12,6 +12,7 @@ from typing import Optional from urllib.parse import urljoin import dateutil.parser +import h5py import ipykernel from metadata_client.metadata_client import MetadataClient from notebook.notebookapp import list_running_servers @@ -264,12 +265,12 @@ def get_dir_creation_date(directory: str, run: int, try: dates = [] for f in directory.glob('*.h5'): - with h5py.File(f) as fin: + with h5py.File(f, 'r') as fin: cdate = fin['METADATA/creationDate'][0].decode() cdate = datetime.datetime.strptime(cdate, "%Y%m%dT%H%M%SZ") dates.append(cdate) return min(dates) - except IOError: + except (IOError, ValueError): ntries -= 1 msg = 'Could not get the creation time from the directory' diff --git a/tests/test_cal_tools.py b/tests/test_cal_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..e93a75174d18b4e46c0b9739db8977499f307119 --- /dev/null +++ b/tests/test_cal_tools.py @@ -0,0 +1,17 @@ +from datetime import datetime +from pathlib import Path + +import pytest +from cal_tools.tools import get_dir_creation_date + + +def test_dir_creation_date(): + folder = "/gpfs/exfel/exp/DETLAB/202031/p900172/raw" + + date = get_dir_creation_date(folder, 10) + assert isinstance(date, datetime) + assert str(date) == '2020-07-20 10:39:03' + + with pytest.raises(ValueError) as e: + assert get_dir_creation_date(folder, 4) + assert e.value.args[1] == Path(folder) / 'r0004'