Skip to content

Commit ed6d158

Browse files
committed
add update-hashes command
1 parent 503c04b commit ed6d158

File tree

2 files changed

+66
-26
lines changed

2 files changed

+66
-26
lines changed

bioimageio/core/cli.py

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import subprocess
1010
import sys
11+
from abc import ABC
1112
from argparse import RawTextHelpFormatter
1213
from difflib import SequenceMatcher
1314
from functools import cached_property
@@ -42,7 +43,6 @@
4243
SettingsConfigDict,
4344
YamlConfigSettingsSource,
4445
)
45-
from ruyaml import YAML
4646
from tqdm import tqdm
4747
from typing_extensions import assert_never
4848

@@ -53,8 +53,11 @@
5353
load_description,
5454
save_bioimageio_yaml_only,
5555
settings,
56+
update_format,
57+
update_hashes,
5658
)
5759
from bioimageio.spec._internal.io_basics import ZipPath
60+
from bioimageio.spec._internal.io_utils import yaml
5861
from bioimageio.spec._internal.types import NotEmpty
5962
from bioimageio.spec.dataset import DatasetDescr
6063
from bioimageio.spec.model import ModelDescr, v0_4, v0_5
@@ -66,7 +69,6 @@
6669
WeightFormatArgAny,
6770
package,
6871
test,
69-
update_format,
7072
)
7173
from .common import MemberId, SampleId, SupportedWeightsFormat
7274
from .digest_spec import get_member_ids, load_sample_for_model
@@ -84,8 +86,6 @@
8486
from .utils import VERSION
8587
from .weight_converters._add_weights import add_weights
8688

87-
yaml = YAML(typ="safe")
88-
8989

9090
class CmdBase(BaseModel, use_attribute_docstrings=True, cli_implicit_flags=True):
9191
pass
@@ -254,31 +254,68 @@ def _get_stat(
254254
return stat
255255

256256

257-
class UpdateFormatCmd(CmdBase, WithSource):
258-
"""Update the metadata format"""
257+
class UpdateCmdBase(CmdBase, WithSource, ABC):
258+
output: Union[Literal["render", "stdout"], Path] = "render"
259+
"""Output updated bioimageio.yaml to the terminal or write to a file."""
259260

260-
output: Optional[Path] = None
261-
"""Save updated bioimageio.yaml to this file.
261+
exclude_unset: bool = Field(True, alias="exclude-unset")
262+
"""Exclude fields that have not explicitly be set."""
262263

263-
Updated bioimageio.yaml is rendered to the terminal if the output is None.
264-
"""
265-
266-
exclude_defaults: bool = Field(True, alias="exclude-defaults")
264+
exclude_defaults: bool = Field(False, alias="exclude-defaults")
267265
"""Exclude fields that have the default value (even if set explicitly)."""
268266

267+
@cached_property
268+
def updated(self) -> Union[ResourceDescr, InvalidDescr]:
269+
raise NotImplementedError
270+
269271
def run(self):
270-
updated = update_format(
271-
self.source, output=self.output, exclude_defaults=self.exclude_defaults
272-
)
273-
updated_stream = StringIO()
272+
if self.output == "render":
273+
out = StringIO()
274+
elif self.output == "stdout":
275+
out = sys.stdout
276+
else:
277+
out = self.output
278+
274279
save_bioimageio_yaml_only(
275-
updated, updated_stream, exclude_defaults=self.exclude_defaults
280+
self.updated,
281+
out,
282+
exclude_unset=self.exclude_unset,
283+
exclude_defaults=self.exclude_defaults,
284+
)
285+
286+
if self.output == "render":
287+
assert isinstance(out, StringIO)
288+
updated_md = f"```yaml\n{out.getvalue()}\n```"
289+
290+
rich_markdown = rich.markdown.Markdown(updated_md)
291+
console = rich.console.Console()
292+
console.print(rich_markdown)
293+
294+
295+
class UpdateFormatCmd(UpdateCmdBase):
296+
"""Update the metadata format to the latest format version."""
297+
298+
perform_io_checks: bool = Field(
299+
settings.perform_io_checks, alias="perform-io-checks"
300+
)
301+
"""Wether or not to attempt validation that may require file download.
302+
If `True` file hash values are added if not present."""
303+
304+
@cached_property
305+
def updated(self):
306+
return update_format(
307+
self.source,
308+
exclude_defaults=self.exclude_defaults,
309+
perform_io_checks=self.perform_io_checks,
276310
)
277-
updated_md = f"```yaml\n{updated_stream.getvalue()}\n```"
278311

279-
rich_markdown = rich.markdown.Markdown(updated_md)
280-
console = rich.console.Console()
281-
console.print(rich_markdown)
312+
313+
class UpdateHashesCmd(UpdateCmdBase):
314+
"""Create a bioimageio.yaml description with updated file hashes."""
315+
316+
@cached_property
317+
def updated(self):
318+
return update_hashes(self.source)
282319

283320

284321
class PredictCmd(CmdBase, WithSource):
@@ -690,6 +727,9 @@ class Bioimageio(
690727
update_format: CliSubCommand[UpdateFormatCmd] = Field(alias="update-format")
691728
"""Update the metadata format"""
692729

730+
update_hashes: CliSubCommand[UpdateHashesCmd] = Field(alias="update-hashes")
731+
"""Create a bioimageio.yaml description with updated file hashes."""
732+
693733
add_weights: CliSubCommand[ConvertWeightsCmd] = Field(alias="add-weights")
694734
"""Add additional weights to the model descriptions converted from available
695735
formats to improve deployability."""
@@ -732,12 +772,13 @@ def run(self):
732772
pformat({k: v for k, v in self.model_dump().items() if v is not None}),
733773
)
734774
cmd = (
735-
self.validate_format
736-
or self.test
775+
self.add_weights
737776
or self.package
738777
or self.predict
778+
or self.test
739779
or self.update_format
740-
or self.add_weights
780+
or self.update_hashes
781+
or self.validate_format
741782
)
742783
assert cmd is not None
743784
cmd.run()

bioimageio/core/commands.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""These functions implement the logic of the bioimageio command line interface
1+
"""These functions are used in the bioimageio command line interface
22
defined in `bioimageio.core.cli`."""
33

44
from pathlib import Path
@@ -13,7 +13,6 @@
1313
save_bioimageio_package,
1414
save_bioimageio_package_as_folder,
1515
)
16-
from bioimageio.spec import update_format as update_format
1716

1817
from ._resource_tests import test_description
1918

0 commit comments

Comments
 (0)