diff --git a/renku/api/datasets.py b/renku/api/datasets.py index 54fc10ae19..8a3c68ed1d 100644 --- a/renku/api/datasets.py +++ b/renku/api/datasets.py @@ -50,7 +50,7 @@ class DatasetsApiMixin(object): @property def renku_datasets_path(self): """Return a ``Path`` instance of Renku dataset metadata folder.""" - return self.renku_path.joinpath(self.DATASETS) + return Path(self.renku_home).joinpath(self.DATASETS) def datasets_from_commit(self, commit=None): """Return datasets defined in a commit.""" @@ -80,11 +80,11 @@ def datasets(self): result[path] = self.get_dataset(path) return result - def get_dataset(self, path): + def get_dataset(self, path, commit=None): """Return a dataset from a given path.""" if not path.is_absolute(): path = self.path / path - return Dataset.from_yaml(path, client=self) + return Dataset.from_yaml(path, client=self, commit=commit) def dataset_path(self, name): """Get dataset path from name.""" diff --git a/renku/models/_jsonld.py b/renku/models/_jsonld.py index dbb71a8589..b7cf384c47 100644 --- a/renku/models/_jsonld.py +++ b/renku/models/_jsonld.py @@ -314,6 +314,7 @@ def from_jsonld( cls, data, client=None, + commit=None, __reference__=None, __source__=None, ): @@ -331,7 +332,9 @@ def from_jsonld( ) != type_: new_cls = cls.__type_registry__[type_] if cls != new_cls: - return new_cls.from_jsonld(data, client=client) + return new_cls.from_jsonld( + data, client=client, commit=commit + ) if cls._jsonld_translate: data = ld.compact(data, {'@context': cls._jsonld_translate}) @@ -355,6 +358,7 @@ def from_jsonld( data_ = {} if client: data_['client'] = client + data_['commit'] = commit for k, v in compacted.items(): if k in fields: @@ -372,7 +376,7 @@ def from_jsonld( return self @classmethod - def from_yaml(cls, path, client=None): + def from_yaml(cls, path, client=None, commit=None): """Return an instance from a YAML file.""" import yaml @@ -381,10 +385,10 @@ def from_yaml(cls, path, client=None): self = cls.from_jsonld( source, client=client, + commit=commit, __reference__=path, - __source__=deepcopy(source), + __source__=deepcopy(source) ) - return self def asjsonld(self): diff --git a/renku/models/datasets.py b/renku/models/datasets.py index b3a05b01b9..6cd6e19440 100644 --- a/renku/models/datasets.py +++ b/renku/models/datasets.py @@ -467,7 +467,7 @@ def unlink_file(self, file_path): return self.files.pop(index) def __attrs_post_init__(self): - """Post-Init hook to set _id field.""" + """Post-Init hook.""" self._id = self.identifier if not self._label: @@ -487,3 +487,11 @@ def __attrs_post_init__(self): ) datasetfile.client = client + + try: + self.commit = self.client.find_previous_commit( + self.path, revision=self.commit or 'HEAD' + ) + except KeyError: + # if with_dataset is used, the dataset is not committed yet + pass diff --git a/renku/models/provenance/entities.py b/renku/models/provenance/entities.py index 7a8f0a6985..99f6fa994e 100644 --- a/renku/models/provenance/entities.py +++ b/renku/models/provenance/entities.py @@ -18,6 +18,7 @@ """Represent provenance entities.""" import weakref +from pathlib import Path import attr @@ -77,6 +78,13 @@ def default_project(self): if self.client: return self.client.project + def __attrs_post_init__(self): + """Post-init hook.""" + if self.path: + path = Path(self.path) + if path.is_absolute(): + self.path = str(path.relative_to(self.client.path)) + @jsonld.s( type=[ diff --git a/tests/test_dataset.py b/tests/test_dataset.py index d1c9115a6d..88cf074145 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -88,7 +88,6 @@ def test_data_add( assert not os.access( 'data/dataset/file', stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH ) - # assert os.stat('data/dataset/file/metadata.yml') # check the linking if scheme in ('', 'file://'):