From 7385eae3f144fd47108301afa0bc76583bb5769b Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Fri, 10 May 2024 09:52:13 -0400 Subject: [PATCH 1/4] Add calculate_sha256 management command to trigger (re)computation for a blob Added ability to specify either blob id (where it applies) or asset id since that is the id we typically know for which a blob lacks sha256. Refs: - https://github.com/dandi/dandi-archive/issues/471 - https://github.com/dandi/dandi-archive/issues/1578 --- .../management/commands/calculate_sha256.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dandiapi/api/management/commands/calculate_sha256.py diff --git a/dandiapi/api/management/commands/calculate_sha256.py b/dandiapi/api/management/commands/calculate_sha256.py new file mode 100644 index 000000000..d7f0507e8 --- /dev/null +++ b/dandiapi/api/management/commands/calculate_sha256.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +import djclick as click + +from dandiapi.api.models import Asset +from dandiapi.api.tasks import calculate_sha256 as do_calculate_sha256 + + +@click.command() +@click.option('--blob-id', 'blob_id', help='Blob ID') +@click.option('--asset-id', 'asset_id', help='Asset ID') +def calculate_sha256(asset_id: str | None = None, blob_id: str | None = None): + """Trigger computation of sha256 for a blob + + Either blob-id or asset-id should be provided. + """ + if not asset_id and not blob_id: + raise ValueError('Provide either asset_id or blob_id') + + if asset_id and blob_id: + raise ValueError('Provide only asset_id or blob_id, not both') + + if not blob_id: + assert asset_id + asset = Asset.objects.get(asset_id=asset_id) + blob_id = asset.blob_id + + do_calculate_sha256(blob_id=blob_id) From 6707d931fb3728f7145a362e1c9734c0ef827728 Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Fri, 10 May 2024 10:28:39 -0400 Subject: [PATCH 2/4] Add a few more ignores for codespell so I could run tox -e lint locally --- .codespellrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.codespellrc b/.codespellrc index 87d72897c..71599816e 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,5 +1,5 @@ [codespell] -skip = .git,node_modules,dist,venv,.venv,.mypy_cache,.tox,yarn.lock,CHANGELOG.md,./htmlcov,*.pyc,*.log +skip = .git,node_modules,dist,venv,.venv,.mypy_cache,.tox,yarn.lock,CHANGELOG.md,./htmlcov,*.pyc,*.log,build,venvs # Disabled until https://github.com/codespell-project/codespell/issues/2727 # got answer/re-solution # dictionary = .codespell_dict From b39cfb5276d2ee6c71103aaa93c8d924fd182ede Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Fri, 10 May 2024 10:31:21 -0400 Subject: [PATCH 3/4] Refactor code slightly to avoid assert + fix D400 ruff check for final period --- dandiapi/api/management/commands/calculate_sha256.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dandiapi/api/management/commands/calculate_sha256.py b/dandiapi/api/management/commands/calculate_sha256.py index d7f0507e8..f5c7e2722 100644 --- a/dandiapi/api/management/commands/calculate_sha256.py +++ b/dandiapi/api/management/commands/calculate_sha256.py @@ -10,18 +10,16 @@ @click.option('--blob-id', 'blob_id', help='Blob ID') @click.option('--asset-id', 'asset_id', help='Asset ID') def calculate_sha256(asset_id: str | None = None, blob_id: str | None = None): - """Trigger computation of sha256 for a blob + """Trigger computation of sha256 for a blob. Either blob-id or asset-id should be provided. """ if not asset_id and not blob_id: raise ValueError('Provide either asset_id or blob_id') - if asset_id and blob_id: - raise ValueError('Provide only asset_id or blob_id, not both') - - if not blob_id: - assert asset_id + if asset_id: + if blob_id: + raise ValueError('Provide only asset_id or blob_id, not both') asset = Asset.objects.get(asset_id=asset_id) blob_id = asset.blob_id From ccf08bff1a501912d85f0576bf8d74fd6cf060a3 Mon Sep 17 00:00:00 2001 From: Roni Choudhury Date: Thu, 3 Oct 2024 12:50:20 -0400 Subject: [PATCH 4/4] Simplify argument handling logic for readability --- dandiapi/api/management/commands/calculate_sha256.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dandiapi/api/management/commands/calculate_sha256.py b/dandiapi/api/management/commands/calculate_sha256.py index f5c7e2722..eb9d8148c 100644 --- a/dandiapi/api/management/commands/calculate_sha256.py +++ b/dandiapi/api/management/commands/calculate_sha256.py @@ -14,12 +14,14 @@ def calculate_sha256(asset_id: str | None = None, blob_id: str | None = None): Either blob-id or asset-id should be provided. """ + # Handle mutually exclusive option failure cases. if not asset_id and not blob_id: raise ValueError('Provide either asset_id or blob_id') + if asset_id and blob_id: + raise ValueError('Provide only asset_id or blob_id, not both') + # Make sure we have a good blob_id to work with. if asset_id: - if blob_id: - raise ValueError('Provide only asset_id or blob_id, not both') asset = Asset.objects.get(asset_id=asset_id) blob_id = asset.blob_id