2Utility functions to download files
7from urllib.request
import urlretrieve
13 for u
in (
"B",
"KB",
"MB",
"GB",
"TB"):
19def reporthook(block_num, block_size, total_size):
20 if total_size <= 0
or block_size <= 0:
22 freq_report = int(int(total_size / block_size) / 10)
25 if block_num % freq_report == 0:
27 downloaded = block_num * block_size
28 percent = int((downloaded / total_size) * 100)
29 filled = int(BAR_WIDTH * percent / 100)
30 bar =
"#" * filled +
"-" * (BAR_WIDTH - filled)
31 sys.stdout.write(f
"[{bar}] {percent:3d}% | {fmt(downloaded)}/{fmt(total_size)}\n")
37 Download a file from an URL
40 if shamrock.sys.world_rank() == 0:
41 print(f
" - Downloading {filename} from {url}")
43 os.makedirs(os.path.dirname(filename), exist_ok=
True)
44 urlretrieve(url, filename, reporthook=reporthook)
46 shamrock.sys.mpi_barrier()
49 if not os.path.exists(filename):
50 raise FileNotFoundError(
51 f
"File {filename} should have been downloaded but is not present on rank {shamrock.sys.world_rank()}"
download_file(url, filename)