def text_dandiset( dandi_client: DandiAPIClient, tmp_path_factory: pytest.TempPathFactory) -> Iterator[Dict[str, Any]]: d = dandi_client.create_dandiset( "Dandiset for testing backups2datalad", { "schemaKey": "Dandiset", "name": "Dandiset for testing backups2datalad", "description": "A test text Dandiset", "contributor": [{ "schemaKey": "Person", "name": "Wodder, John", "roleName": ["dcite:Author", "dcite:ContactPerson"], }], "license": ["spdx:CC0-1.0"], "manifestLocation": ["https://github.com/dandi/dandi-cli"], }, ) dandiset_id = d.identifier dspath = tmp_path_factory.mktemp("text_dandiset") (dspath / dandiset_metadata_file).write_text(f"identifier: '{dandiset_id}'\n") (dspath / "file.txt").write_text("This is test text.\n") (dspath / "v0.txt").write_text("Version 0\n") (dspath / "subdir1").mkdir() (dspath / "subdir1" / "apple.txt").write_text("Apple\n") (dspath / "subdir2").mkdir() (dspath / "subdir2" / "banana.txt").write_text("Banana\n") (dspath / "subdir2" / "coconut.txt").write_text("Coconut\n") def upload_dandiset(paths: Optional[List[str]] = None, **kwargs: Any) -> None: upload( paths=paths or [dspath], dandi_instance="dandi-staging", devel_debug=True, allow_any_path=True, validation="skip", **kwargs, ) try: upload_dandiset() yield { "client": dandi_client, "dspath": dspath, "dandiset": d, "dandiset_id": dandiset_id, "reupload": upload_dandiset, } finally: for v in d.get_versions(): if v.identifier != "draft": dandi_client.delete(f"{d.api_path}versions/{v.identifier}/") d.delete()
def main(api_url, token, dandiset_path, delete_extant, only_metadata): client = DandiAPIClient(api_url=api_url, token=token) with client.session(): for dpath in dandiset_path: dandiset = APIDandiset(dpath) if delete_extant: try: client.get_dandiset(dandiset.identifier, "draft") except requests.HTTPError as e: if e.response.status_code != 404: raise else: print("Dandiset", dandiset.identifier, "already exists; deleting") client.delete(f"/dandisets/{dandiset.identifier}/") if only_metadata: print("Setting metadata for Dandiset", dandiset.identifier) client.set_dandiset_metadata( dandiset.identifier, metadata=dandiset.metadata ) else: print("Creating Dandiset", dandiset.identifier) client.create_dandiset( name=dandiset.metadata.get("name", ""), metadata=dandiset.metadata )