Skip to content

Commit 575686b

Browse files
committed
feat(dataset): add existing file from current repo
(addresses #99)
1 parent ee35dce commit 575686b

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

renku/api/datasets.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ def add_data_to_dataset(
141141
ignored = self.find_ignored_paths(
142142
*[
143143
os.path.relpath(
144-
str(self.renku_datasets_path / dataset.name / key),
144+
str(
145+
self.renku_datasets_path / dataset.identifier.hex / key
146+
),
145147
start=str(self.path),
146148
) for key in files.keys()
147149
]
@@ -252,6 +254,30 @@ def _add_from_git(self, dataset, path, url, target, **kwargs):
252254
relative_to = kwargs.get('relative_to', None)
253255

254256
if u.scheme in ('', 'file'):
257+
try:
258+
relative_url = Path(url).resolve().relative_to(self.path)
259+
except Exception:
260+
relative_url = None
261+
262+
if relative_url:
263+
result = str(
264+
os.path.relpath(
265+
str(relative_url),
266+
start=str(
267+
self.renku_datasets_path / dataset.identifier.hex
268+
),
269+
)
270+
)
271+
return {
272+
result:
273+
DatasetFile(
274+
path=result,
275+
url=url,
276+
authors=dataset.authors,
277+
dataset=dataset.name,
278+
)
279+
}
280+
255281
warnings.warn('Importing local git repository, use HTTPS')
256282
# determine where is the base repo path
257283
r = Repo(url, search_parent_directories=True)

tests/cli/test_dataset.py renamed to tests/cli/test_datasets.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,31 @@ def test_multiple_file_to_dataset(
123123
assert result.exit_code == 0
124124

125125

126+
def test_repository_file_to_dataset(runner, project, client):
127+
"""Test adding a file from the repository into a dataset."""
128+
# create a dataset
129+
result = runner.invoke(cli.cli, ['dataset', 'create', 'dataset'])
130+
assert result.exit_code == 0
131+
132+
with (client.path / 'a').open('w') as fp:
133+
fp.write('a')
134+
135+
client.repo.git.add('a')
136+
client.repo.git.commit(message='Added file a')
137+
138+
# add data
139+
result = runner.invoke(
140+
cli.cli,
141+
['dataset', 'add', 'dataset', 'a'],
142+
catch_exceptions=False,
143+
)
144+
assert result.exit_code == 0
145+
146+
with client.with_dataset('dataset') as dataset:
147+
assert dataset.name == 'dataset'
148+
assert '../../../a' in dataset.files
149+
150+
126151
def test_relative_import_to_dataset(
127152
tmpdir, data_repository, runner, project, client
128153
):

0 commit comments

Comments
 (0)