Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/atrnet_star/download.py: 0%
25 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-26 05:19 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-26 05:19 +0000
1# MIT License
3# Copyright (c) 2025 Rodolphe Durand
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23import glob
24import os
25import pathlib
26import platform
27import shutil
28import subprocess
30import huggingface_hub
33def check_7z():
34 """
35 Checks if 7zip is installed and if the command is available. If not, raises an error asking the user to install it.
36 """
37 if shutil.which("7z") is None:
38 message = "7zip is needed to unpack the dataset files. Please install it and add command to path."
39 if platform.system() == "Linux":
40 message += " The following command can be used :\n\n sudo apt-get install p7zip-full\n"
42 raise RuntimeError(message)
45def download_benchmark(
46 benchmark: str,
47 root_dir: pathlib.Path,
48 hf_repo_id: str,
49 hf_benchmark_path: pathlib.Path,
50):
51 """
52 Download the specified benchmark in the given root directory.
54 The dowload uses Hugging Face. The user will be prompted to log in using an acess token
55 A Hugging Face account is thus required to download the dataset (even when doing so manually)
56 """
57 huggingface_hub.login(new_session=False)
59 # create root_dir if it does not exist
60 root_dir.mkdir(parents=True, exist_ok=True)
61 os.chdir(root_dir.absolute())
63 # gather all volumes of the split 7z archive (file.7z.001, file.7z.002, ...)
64 benchmark_glob = f"{str(hf_benchmark_path / benchmark)}.7z.*"
65 huggingface_hub.snapshot_download(
66 repo_id=hf_repo_id,
67 allow_patterns=benchmark_glob,
68 local_dir=root_dir.absolute(),
69 repo_type="dataset",
70 )
72 # downloaded files maintain their original file structure
73 # move them back to root_dir and delete empty directories
74 for zip_file in (root_dir / hf_benchmark_path).glob("*.*"):
75 shutil.move(zip_file, root_dir)
76 os.removedirs(hf_benchmark_path)
78 # unzip the first archive (7zip automatically joins all volumes)
79 subprocess.run(f"7z x {benchmark}.7z.001", shell=True)
81 # delete unpacked 7zip files
82 for f in glob.glob(f"{benchmark}.7z.*"):
83 os.remove(f)