Skip to content

Commit 04a9fe9

Browse files
authored
feat(api): list datasets from a commit
1 parent fa31dd7 commit 04a9fe9

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

renku/api/datasets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ def renku_datasets_path(self):
5151
"""Return a ``Path`` instance of Renku dataset metadata folder."""
5252
return self.renku_path.joinpath(self.DATASETS)
5353

54+
def datasets_from_commit(self, commit=None):
55+
"""Return datasets defined in a commit."""
56+
commit = commit or self.repo.head.commit
57+
58+
try:
59+
datasets = commit.tree / self.renku_home / self.DATASETS
60+
except KeyError:
61+
return
62+
63+
for tree in datasets:
64+
try:
65+
blob = tree / self.METADATA
66+
except KeyError:
67+
continue
68+
69+
yield Dataset.from_jsonld(
70+
yaml.safe_load(blob.data_stream.read()),
71+
__reference__=Path(blob.path),
72+
)
73+
5474
@property
5575
def datasets(self):
5676
"""Return mapping from path to dataset."""
@@ -90,6 +110,7 @@ def with_dataset(self, name=None):
90110
self.METADATA
91111
)
92112
path.parent.mkdir(parents=True, exist_ok=True)
113+
setattr(dataset, '__reference__', path)
93114

94115
if name:
95116
LinkReference.create(

renku/cli/_format/datasets.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
import click
2424

2525

26-
def tabular(client, datasets=None):
26+
def tabular(client, datasets):
2727
"""Format datasets with a tabular output."""
2828
from renku.models._tabulate import tabulate
2929

30-
datasets = datasets or client.datasets
31-
3230
click.echo(
3331
tabulate(
34-
datasets.values(),
32+
datasets,
3533
headers=OrderedDict((
3634
('short_id', 'id'),
3735
('name', None),
@@ -42,16 +40,18 @@ def tabular(client, datasets=None):
4240
)
4341

4442

45-
def jsonld(client, datasets=None):
43+
def jsonld(client, datasets):
4644
"""Format datasets as JSON-LD."""
4745
from renku.models._json import dumps
4846
from renku.models._jsonld import asjsonld
4947

50-
datasets = datasets or client.datasets
5148
data = [
5249
asjsonld(
53-
dataset, basedir=os.path.relpath('.', start=str(path.parent))
54-
) for path, dataset in datasets.items()
50+
dataset,
51+
basedir=os.path.relpath(
52+
'.', start=str(dataset.__reference__.parent)
53+
)
54+
) for dataset in datasets
5555
]
5656
click.echo(dumps(data, indent=2))
5757

renku/cli/dataset.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989

9090
@click.group(invoke_without_command=True)
91+
@click.option('--revision', default=None)
9192
@click.option('--datadir', default='data', type=click.Path(dir_okay=True))
9293
@click.option(
9394
'--format',
@@ -97,14 +98,19 @@
9798
)
9899
@pass_local_client(clean=False, commit=False)
99100
@click.pass_context
100-
def dataset(ctx, client, datadir, format):
101+
def dataset(ctx, client, revision, datadir, format):
101102
"""Handle datasets."""
102103
ctx.meta['renku.datasets.datadir'] = datadir
103104

104105
if ctx.invoked_subcommand is not None:
105106
return
106107

107-
DATASETS_FORMATS[format](client)
108+
if revision is None:
109+
datasets = client.datasets.values()
110+
else:
111+
datasets = client.datasets_from_commit(client.repo.commit(revision))
112+
113+
DATASETS_FORMATS[format](client, datasets)
108114

109115

110116
@dataset.command()

tests/cli/test_datasets.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ def test_datasets_list_non_empty(output_format, runner, project):
9393
assert 0 == result.exit_code
9494
assert 'dataset' in result.output
9595

96+
result = runner.invoke(
97+
cli.cli, ['dataset', '--revision=HEAD~1', format_option]
98+
)
99+
assert result.exit_code == 0
100+
assert 'dataset' not in result.output
101+
96102

97103
def test_multiple_file_to_dataset(
98104
tmpdir, data_repository, runner, project, client

0 commit comments

Comments
 (0)