Skip to content

Commit

Permalink
Add script for migrating Dandiset metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Feb 8, 2021
1 parent fc56dfb commit ffe2fac
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tools/migrate-dandisets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
import click
import requests

from dandi.dandiapi import DandiAPIClient
from dandi.dandiset import APIDandiset


@click.command()
@click.option(
"-d", "--delete-extant", is_flag=True, help="Delete Dandisets that already exist"
)
@click.argument("api_url")
@click.argument("token")
@click.argument("dandiset_path", nargs=-1)
def main(api_url, token, dandiset_path, delete_extant):
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}/")
print("Creating Dandiset", dandiset.identifier)
client.create_dandiset(
name=dandiset.metadata.get("name", ""), metadata=dandiset.metadata
)


if __name__ == "__main__":
main()

0 comments on commit ffe2fac

Please sign in to comment.