Skip to content

Commit

Permalink
feat(dataset): add existing file from current repo
Browse files Browse the repository at this point in the history
(addresses #99)
  • Loading branch information
jirikuncar committed Mar 7, 2019
1 parent ee35dce commit 575686b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
28 changes: 27 additions & 1 deletion renku/api/datasets.py
Expand Up @@ -141,7 +141,9 @@ def add_data_to_dataset(
ignored = self.find_ignored_paths(
*[
os.path.relpath(
str(self.renku_datasets_path / dataset.name / key),
str(
self.renku_datasets_path / dataset.identifier.hex / key
),
start=str(self.path),
) for key in files.keys()
]
Expand Down Expand Up @@ -252,6 +254,30 @@ def _add_from_git(self, dataset, path, url, target, **kwargs):
relative_to = kwargs.get('relative_to', None)

if u.scheme in ('', 'file'):
try:
relative_url = Path(url).resolve().relative_to(self.path)
except Exception:
relative_url = None

if relative_url:
result = str(
os.path.relpath(
str(relative_url),
start=str(
self.renku_datasets_path / dataset.identifier.hex
),
)
)
return {
result:
DatasetFile(
path=result,
url=url,
authors=dataset.authors,
dataset=dataset.name,
)
}

warnings.warn('Importing local git repository, use HTTPS')
# determine where is the base repo path
r = Repo(url, search_parent_directories=True)
Expand Down
25 changes: 25 additions & 0 deletions tests/cli/test_dataset.py → tests/cli/test_datasets.py
Expand Up @@ -123,6 +123,31 @@ def test_multiple_file_to_dataset(
assert result.exit_code == 0


def test_repository_file_to_dataset(runner, project, client):
"""Test adding a file from the repository into a dataset."""
# create a dataset
result = runner.invoke(cli.cli, ['dataset', 'create', 'dataset'])
assert result.exit_code == 0

with (client.path / 'a').open('w') as fp:
fp.write('a')

client.repo.git.add('a')
client.repo.git.commit(message='Added file a')

# add data
result = runner.invoke(
cli.cli,
['dataset', 'add', 'dataset', 'a'],
catch_exceptions=False,
)
assert result.exit_code == 0

with client.with_dataset('dataset') as dataset:
assert dataset.name == 'dataset'
assert '../../../a' in dataset.files


def test_relative_import_to_dataset(
tmpdir, data_repository, runner, project, client
):
Expand Down

0 comments on commit 575686b

Please sign in to comment.