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

1# MIT License 

2 

3# Copyright (c) 2025 Rodolphe Durand 

4 

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: 

11 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14 

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. 

22 

23import glob 

24import os 

25import pathlib 

26import platform 

27import shutil 

28import subprocess 

29 

30import huggingface_hub 

31 

32 

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" 

41 

42 raise RuntimeError(message) 

43 

44 

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. 

53 

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) 

58 

59 # create root_dir if it does not exist 

60 root_dir.mkdir(parents=True, exist_ok=True) 

61 os.chdir(root_dir.absolute()) 

62 

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 ) 

71 

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) 

77 

78 # unzip the first archive (7zip automatically joins all volumes) 

79 subprocess.run(f"7z x {benchmark}.7z.001", shell=True) 

80 

81 # delete unpacked 7zip files 

82 for f in glob.glob(f"{benchmark}.7z.*"): 

83 os.remove(f)