Skip to content

Commit

Permalink
Merge pull request #1125 from dandi/gh-1119
Browse files Browse the repository at this point in the history
Update client-side publication workflow
  • Loading branch information
yarikoptic committed Oct 6, 2022
2 parents e021829 + e24d324 commit fd0dd09
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
55 changes: 46 additions & 9 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class AssetType(Enum):
ZARR = 2


class VersionStatus(Enum):
PENDING = "Pending"
VALIDATING = "Validating"
VALID = "Valid"
INVALID = "Invalid"
PUBLISHING = "Publishing"
PUBLISHED = "Published"


# Following class is loosely based on GirderClient, with authentication etc
# being stripped.
# TODO: add copyright/license info
Expand Down Expand Up @@ -636,6 +645,7 @@ class Version(APIBase):
created: datetime
#: The timestamp at which the version was last modified
modified: datetime
status: VersionStatus

def __str__(self) -> str:
return self.identifier
Expand Down Expand Up @@ -850,10 +860,19 @@ def refresh(self) -> None:
# Clear _version so it will be refetched the next time it is accessed
self._version = None

def get_versions(self) -> Iterator[Version]:
"""Returns an iterator of all available `Version`\\s for the Dandiset"""
def get_versions(self, order: Optional[str] = None) -> Iterator[Version]:
"""
Returns an iterator of all available `Version`\\s for the Dandiset
Versions can be sorted by a given field by passing the name of that
field as the ``order`` parameter. Currently, the only accepted field
name is ``"created"``. Prepend a hyphen to the field name to reverse
the sort order.
"""
try:
for v in self.client.paginate(f"{self.api_path}versions/"):
for v in self.client.paginate(
f"{self.api_path}versions/", params={"order": order}
):
yield Version.parse_obj(v)
except HTTP404Error:
raise NotFoundError(f"No such Dandiset: {self.identifier!r}")
Expand Down Expand Up @@ -962,14 +981,32 @@ def wait_until_valid(self, max_time: float = 120) -> None:
f"Dandiset {self.identifier} is {r['status']}: {json.dumps(about, indent=4)}"
)

def publish(self) -> "RemoteDandiset":
def publish(self, max_time: float = 120) -> "RemoteDandiset":
"""
Publish this version of the Dandiset. Returns a copy of the
`RemoteDandiset` with the `version` attribute set to the new published
`Version`.
Publish the draft version of the Dandiset and wait at most ``max_time``
seconds for the publication operation to complete. If the operation
does not complete in time, a `ValueError` is raised.
Returns a copy of the `RemoteDandiset` with the `version` attribute set
to the new published `Version`.
"""
return self.for_version(
Version.parse_obj(self.client.post(f"{self.version_api_path}publish/"))
draft_api_path = f"/dandisets/{self.identifier}/versions/draft/"
self.client.post(f"{draft_api_path}publish/")
lgr.debug(
"Waiting for Dandiset %s to complete publication ...", self.identifier
)
start = time()
while time() - start < max_time:
v = Version.parse_obj(self.client.get(f"{draft_api_path}info/"))
if v.status is VersionStatus.PUBLISHED:
break
sleep(0.5)
else:
raise ValueError(f"Dandiset {self.identifier} did not publish in time")
for v in self.get_versions(order="-created"):
return self.for_version(v)
raise AssertionError(
f"No published versions found for Dandiset {self.identifier}"
)

def get_assets(self, order: Optional[str] = None) -> Iterator["RemoteAsset"]:
Expand Down
2 changes: 2 additions & 0 deletions dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_publish_and_manipulate(new_dandiset: SampleDandiset, tmp_path: Path) ->
d.wait_until_valid()
v = d.publish().version
version_id = v.identifier
assert version_id != "draft"
assert str(v) == version_id
dv = d.for_version(v)
assert str(dv) == f"DANDI-API-LOCAL-DOCKER-TESTS:{dandiset_id}/{version_id}"
Expand Down Expand Up @@ -486,6 +487,7 @@ def test_remote_dandiset_json_dict(text_dandiset: SampleDandiset) -> None:
"size": anys.ANY_INT,
"created": anys.ANY_AWARE_DATETIME_STR,
"modified": anys.ANY_AWARE_DATETIME_STR,
"status": anys.ANY_STR,
},
"version": anys.ANY_DICT,
}
Expand Down

0 comments on commit fd0dd09

Please sign in to comment.