Skip to content

Commit 7659bc8

Browse files
rokroskarjsam
authored andcommitted
fix: always add commit to dataset if possible (#648)
* fix: always add commit to dataset if possible addresses #646 * Update renku/models/_jsonld.py Co-Authored-By: Sam <contact@justsam.io> * expand kwargs
1 parent 04eba66 commit 7659bc8

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

renku/api/datasets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class DatasetsApiMixin(object):
5050
@property
5151
def renku_datasets_path(self):
5252
"""Return a ``Path`` instance of Renku dataset metadata folder."""
53-
return self.renku_path.joinpath(self.DATASETS)
53+
return Path(self.renku_home).joinpath(self.DATASETS)
5454

5555
def datasets_from_commit(self, commit=None):
5656
"""Return datasets defined in a commit."""
@@ -80,11 +80,11 @@ def datasets(self):
8080
result[path] = self.get_dataset(path)
8181
return result
8282

83-
def get_dataset(self, path):
83+
def get_dataset(self, path, commit=None):
8484
"""Return a dataset from a given path."""
8585
if not path.is_absolute():
8686
path = self.path / path
87-
return Dataset.from_yaml(path, client=self)
87+
return Dataset.from_yaml(path, client=self, commit=commit)
8888

8989
def dataset_path(self, name):
9090
"""Get dataset path from name."""

renku/models/_jsonld.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def from_jsonld(
314314
cls,
315315
data,
316316
client=None,
317+
commit=None,
317318
__reference__=None,
318319
__source__=None,
319320
):
@@ -331,7 +332,9 @@ def from_jsonld(
331332
) != type_:
332333
new_cls = cls.__type_registry__[type_]
333334
if cls != new_cls:
334-
return new_cls.from_jsonld(data, client=client)
335+
return new_cls.from_jsonld(
336+
data, client=client, commit=commit
337+
)
335338

336339
if cls._jsonld_translate:
337340
data = ld.compact(data, {'@context': cls._jsonld_translate})
@@ -355,6 +358,7 @@ def from_jsonld(
355358
data_ = {}
356359
if client:
357360
data_['client'] = client
361+
data_['commit'] = commit
358362

359363
for k, v in compacted.items():
360364
if k in fields:
@@ -372,7 +376,7 @@ def from_jsonld(
372376
return self
373377

374378
@classmethod
375-
def from_yaml(cls, path, client=None):
379+
def from_yaml(cls, path, client=None, commit=None):
376380
"""Return an instance from a YAML file."""
377381
import yaml
378382

@@ -381,10 +385,10 @@ def from_yaml(cls, path, client=None):
381385
self = cls.from_jsonld(
382386
source,
383387
client=client,
388+
commit=commit,
384389
__reference__=path,
385-
__source__=deepcopy(source),
390+
__source__=deepcopy(source)
386391
)
387-
388392
return self
389393

390394
def asjsonld(self):

renku/models/datasets.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def unlink_file(self, file_path):
467467
return self.files.pop(index)
468468

469469
def __attrs_post_init__(self):
470-
"""Post-Init hook to set _id field."""
470+
"""Post-Init hook."""
471471
self._id = self.identifier
472472

473473
if not self._label:
@@ -487,3 +487,11 @@ def __attrs_post_init__(self):
487487
)
488488

489489
datasetfile.client = client
490+
491+
try:
492+
self.commit = self.client.find_previous_commit(
493+
self.path, revision=self.commit or 'HEAD'
494+
)
495+
except KeyError:
496+
# if with_dataset is used, the dataset is not committed yet
497+
pass

renku/models/provenance/entities.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"""Represent provenance entities."""
1919

2020
import weakref
21+
from pathlib import Path
2122

2223
import attr
2324

@@ -77,6 +78,13 @@ def default_project(self):
7778
if self.client:
7879
return self.client.project
7980

81+
def __attrs_post_init__(self):
82+
"""Post-init hook."""
83+
if self.path:
84+
path = Path(self.path)
85+
if path.is_absolute():
86+
self.path = str(path.relative_to(self.client.path))
87+
8088

8189
@jsonld.s(
8290
type=[

tests/test_dataset.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def test_data_add(
8888
assert not os.access(
8989
'data/dataset/file', stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
9090
)
91-
# assert os.stat('data/dataset/file/metadata.yml')
9291

9392
# check the linking
9493
if scheme in ('', 'file://'):

0 commit comments

Comments
 (0)