From 94a8376c3d5ac9b6e28cbe257e4af351ab7e7655 Mon Sep 17 00:00:00 2001 From: Annmary Justine K Date: Thu, 23 Sep 2021 00:56:59 -0500 Subject: [PATCH] Support hdf5 files external links --- dvc/repo/add.py | 3 +++ dvc/utils/hdf5.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 dvc/utils/hdf5.py diff --git a/dvc/repo/add.py b/dvc/repo/add.py index 7318cb320e..ecd74e8938 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -18,6 +18,7 @@ from ..repo.scm_context import scm_context from ..utils import LARGE_DIR_SIZE, glob_targets, resolve_output, resolve_paths from ..utils.collections import ensure_list, validate +from ..utils.hdf5 import get_external_links from . import locked if TYPE_CHECKING: @@ -237,6 +238,8 @@ def _find_all_targets( if not repo.scm.belongs_to_scm(path) if not repo.scm.is_tracked(path) ) + elif (target.find(".h5") != -1): + yield from get_external_links(target) else: yield target diff --git a/dvc/utils/hdf5.py b/dvc/utils/hdf5.py new file mode 100644 index 0000000000..73bec5f20c --- /dev/null +++ b/dvc/utils/hdf5.py @@ -0,0 +1,21 @@ + +import h5py + +class H5ls: + def __init__(self): + # Store an empty list for link names + self.names = [] + + def __call__(self, name, obj): + for key, val in obj.items(): + p = obj.get(name=key,default=None, getclass=False, getlink=True) + if (isinstance(p, h5py.ExternalLink)): + self.names.append(p.filename) + +def get_external_links(target): + h5ls = H5ls() + h5ls.names.append(target) + with h5py.File(target,'r') as hdf: + hdf.visititems(h5ls) + return set(h5ls.names) +