diff --git a/datalad/plugin/addurls.py b/datalad/plugin/addurls.py index 3da450dcb9..f5f49141e7 100644 --- a/datalad/plugin/addurls.py +++ b/datalad/plugin/addurls.py @@ -556,16 +556,24 @@ def add_meta(rows): """ from unittest.mock import patch + # OPT: group by dataset first so to not patch/unpatch always_commit + # per each file of which we could have thousands + from collections import defaultdict + dss_rows = defaultdict(list) for row in rows: - ds, filename = row["ds"], row["ds_filename"] + dss_rows[row["ds"]].append(row) + + for ds, ds_rows in dss_rows.items(): with patch.object(ds.repo, "always_commit", False): - lgr.debug("Adding metadata to %s in %s", filename, ds.path) - for a in ds.repo.set_metadata_(filename, add=row["meta_args"]): - res = annexjson2result(a, ds, type="file", logger=lgr) - # Don't show all added metadata for the file because that - # could quickly flood the output. - del res["message"] - yield res + for row in ds_rows: + filename = row["ds_filename"] + lgr.debug("Adding metadata to %s in %s", filename, ds.path) + for a in ds.repo.set_metadata_(filename, add=row["meta_args"]): + res = annexjson2result(a, ds, type="file", logger=lgr) + # Don't show all added metadata for the file because that + # could quickly flood the output. + del res["message"] + yield res @build_doc