diff options
author | Hind-M <hind.montassif@gmail.com> | 2021-11-15 11:12:27 +0100 |
---|---|---|
committer | Hind-M <hind.montassif@gmail.com> | 2021-11-15 11:12:27 +0100 |
commit | aa600c433e1f756bec4323e29e86786b937d9443 (patch) | |
tree | e2b1c2ff9c2b310e013d110dab4a6a0f06300df0 /src/python | |
parent | 5db7ab2b55262a88c0ceecbb9c7ea004d9ed087e (diff) |
Print files licenses when available
Wrap bunny fetching
Add corresponding tests
Diffstat (limited to 'src/python')
-rw-r--r-- | src/python/gudhi/datasets/remote.py | 38 | ||||
-rw-r--r-- | src/python/test/test_remote_datasets.py | 55 |
2 files changed, 81 insertions, 12 deletions
diff --git a/src/python/gudhi/datasets/remote.py b/src/python/gudhi/datasets/remote.py index aef4b277..7e8f9ce7 100644 --- a/src/python/gudhi/datasets/remote.py +++ b/src/python/gudhi/datasets/remote.py @@ -39,7 +39,7 @@ def _checksum_sha256(file_path): sha256_hash.update(buffer) return sha256_hash.hexdigest() -def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): +def fetch(url, filename, dirname = "remote_datasets", file_checksum = None, accept_license = False): """ Fetch the wanted dataset from the given url and save it in file_path @@ -54,6 +54,9 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): file_checksum : string The file checksum using sha256 to check against the one computed on the downloaded file. Default is 'None'. + accept_license : boolean + Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. + Default is False Returns ------- @@ -69,6 +72,7 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): if not exists(dirname): makedirs(dirname) + # Get the file urlretrieve(url, file_path) if file_checksum is not None: @@ -78,6 +82,13 @@ def fetch(url, filename, dirname = "remote_datasets", file_checksum = None): "different from expected : {}." "The file may be corrupted or the given url may be wrong !".format(file_path, checksum, file_checksum)) + # Print license terms unless accept_license is set to True + if not accept_license: + license_file = join(dirname, "LICENSE") + if exists(license_file) and (file_path != license_file): + with open(license_file, 'r') as f: + print(f.read()) + return file_path def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): @@ -98,3 +109,28 @@ def fetch_spiral_2d(filename = "spiral_2d.csv", dirname = "remote_datasets"): """ return fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", filename, dirname, '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') + +def fetch_bunny(filename = "bunny.off", dirname = "remote_datasets/bunny", accept_license = False): + """ + Fetch bunny.off remotely and its LICENSE file + + Parameters + ---------- + filename : string + The name to give to downloaded file. Default is "bunny.off" + dirname : string + The directory to save the file to. Default is "remote_datasets/bunny". + accept_license : boolean + Flag to specify if user accepts the file LICENSE and prevents from printing the corresponding license terms. + Default is False + + Returns + ------- + files_paths: list of strings + Full paths of the created file and its LICENSE. + """ + + return [fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", dirname, + 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956'), + fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", filename, dirname, + '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license)] diff --git a/src/python/test/test_remote_datasets.py b/src/python/test/test_remote_datasets.py index e252980d..e777abc6 100644 --- a/src/python/test/test_remote_datasets.py +++ b/src/python/test/test_remote_datasets.py @@ -11,36 +11,69 @@ from gudhi.datasets import remote import re import os.path +import io +import sys import pytest -def check_dir_file_names(path_file_dw, filename, dirname): +def _check_dir_file_names(path_file_dw, filename, dirname): assert os.path.isfile(path_file_dw) names_dw = re.split(r' |/|\\', path_file_dw) - assert dirname == names_dw[0] - assert filename == names_dw[1] + # Case where inner directories are created in "remote_datasets/"; e.g: "remote_datasets/bunny" + if len(names_dw) >= 3: + for i in range(len(names_dw)-1): + assert re.split(r' |/|\\', dirname)[i] == names_dw[i] + assert filename == names_dw[i+1] + else: + assert dirname == names_dw[0] + assert filename == names_dw[1] -def check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): +def _check_fetch_output(url, filename, dirname = "remote_datasets", file_checksum = None): path_file_dw = remote.fetch(url, filename, dirname, file_checksum) - check_dir_file_names(path_file_dw, filename, dirname) + _check_dir_file_names(path_file_dw, filename, dirname) + +def _get_bunny_license_print(accept_license = False): + capturedOutput = io.StringIO() + # Redirect stdout + sys.stdout = capturedOutput + remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/bunny.off", "bunny.off", "remote_datasets/bunny", + '11852d5e73e2d4bd7b86a2c5cc8a5884d0fbb72539493e8cec100ea922b19f5b', accept_license) + # Reset redirect + sys.stdout = sys.__stdout__ + return capturedOutput def test_fetch_remote_datasets(): # Test fetch with a wrong checksum with pytest.raises(OSError): - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = 'XXXXXXXXXX') # Test files download from given urls with checksums provided - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv", file_checksum = '37530355d980d957c4ec06b18c775f90a91e446107d06c6201c9b4000b077f38') - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off", file_checksum = '32f96d2cafb1177f0dd5e0a019b6ff5658e14a619a7815ae55ad0fc5e8bd3f88') # Test files download from given urls without checksums - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/spiral_2d.csv", "spiral_2d.csv") - check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") + _check_fetch_output("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points/sphere3D_pts_on_grid.off", "sphere3D_pts_on_grid.off") # Test spiral_2d.csv wrapping function path_file_dw = remote.fetch_spiral_2d() - check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') + _check_dir_file_names(path_file_dw, 'spiral_2d.csv', 'remote_datasets') + + # Test printing existing LICENSE file when fetching bunny.off with accept_license = False (default) + # Fetch LICENSE file + remote.fetch("https://raw.githubusercontent.com/GUDHI/gudhi-data/main/points//bunny/LICENSE", "LICENSE", "remote_datasets/bunny", + 'aeb1bad319b7d74fa0b8076358182f9c6b1284c67cc07dc67cbc9bc73025d956') + with open("remote_datasets/bunny/LICENSE") as f: + assert f.read() == _get_bunny_license_print().getvalue().rstrip("\n") + + # Test not printing bunny.off LICENSE when accept_license = True + assert "" == _get_bunny_license_print(accept_license = True).getvalue() + + # Test fetch_bunny wrapping function + path_file_dw = remote.fetch_bunny() + _check_dir_file_names(path_file_dw[0], 'LICENSE', 'remote_datasets/bunny') + _check_dir_file_names(path_file_dw[1], 'bunny.off', 'remote_datasets/bunny') |