|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import urllib.request |
| 6 | +from typing import List |
| 7 | + |
| 8 | +TEST_ROOT_PATH = "/e3sm_diags_downloaded_data/tests/integration/" |
| 9 | +TEST_DATA_DIR = "integration_test_data" |
| 10 | +TEST_IMAGES_DIR = "integration_test_images" |
| 11 | + |
| 12 | + |
| 13 | +# https://stackoverflow.com/questions/49113616/how-to-download-file-using-python |
| 14 | +def retrieve_file(url, file_path): |
| 15 | + dir_path = os.path.join(*os.path.split(file_path)[:-1]) |
| 16 | + # https://stackoverflow.com/questions/12517451/automatically-creating-directories-with-file-output |
| 17 | + if dir_path: |
| 18 | + os.makedirs(dir_path, exist_ok=True) |
| 19 | + print("Downloading {} to {}".format(url, file_path)) |
| 20 | + urllib.request.urlretrieve(url, file_path) |
| 21 | + return file_path |
| 22 | + |
| 23 | + |
| 24 | +def download_files(url_prefix, url_suffix, directory_prefix=None): |
| 25 | + print(f"Downloading {url_suffix}") |
| 26 | + print("url_prefix={}".format(url_prefix)) |
| 27 | + print("url_suffix={}".format(url_suffix)) |
| 28 | + print("(local) directory_prefix={}".format(directory_prefix)) |
| 29 | + |
| 30 | + url = os.path.join(url_prefix, url_suffix) |
| 31 | + |
| 32 | + if directory_prefix: |
| 33 | + links_file_path = os.path.join(directory_prefix, url_suffix) |
| 34 | + else: |
| 35 | + links_file_path = url_suffix |
| 36 | + |
| 37 | + links_file_path = "{}.html".format(links_file_path) |
| 38 | + print( |
| 39 | + "Downloading files from {}; checking for links on {}".format( |
| 40 | + url, links_file_path |
| 41 | + ) |
| 42 | + ) |
| 43 | + html_path = retrieve_file(url, links_file_path) |
| 44 | + links: List[str] = [] |
| 45 | + |
| 46 | + with open(html_path, "r") as html: |
| 47 | + for line in html: |
| 48 | + match_object = re.search(r'href=[\'"]?([^\'" >]+)', line) |
| 49 | + if match_object: |
| 50 | + link = match_object.group(1) |
| 51 | + # Ignore parent directory and sorting links |
| 52 | + if ( |
| 53 | + ("../" not in link) |
| 54 | + and (not link.startswith("/")) |
| 55 | + and ("?" not in link) |
| 56 | + ): |
| 57 | + print("Found a link: {}".format(link)) |
| 58 | + links.append(link) |
| 59 | + |
| 60 | + if os.path.exists(links_file_path): |
| 61 | + os.remove(links_file_path) |
| 62 | + |
| 63 | + files = [] |
| 64 | + directories = [] |
| 65 | + |
| 66 | + for link in links: |
| 67 | + if link.endswith("/"): |
| 68 | + # List directories to download. |
| 69 | + directories.append(link) |
| 70 | + else: |
| 71 | + # List '.csv', '.mat', '.nc', and '.png' files to download. |
| 72 | + files.append(link) |
| 73 | + |
| 74 | + print("\n###Downloading files") |
| 75 | + |
| 76 | + if directory_prefix: |
| 77 | + new_directory_prefix = os.path.join(directory_prefix, url_suffix) |
| 78 | + else: |
| 79 | + new_directory_prefix = url_suffix |
| 80 | + for f in files: |
| 81 | + url_to_download = os.path.join(url, f) |
| 82 | + file_path = os.path.join(new_directory_prefix, f) |
| 83 | + retrieve_file(url_to_download, file_path) |
| 84 | + |
| 85 | + print("\n###Downloading directories") |
| 86 | + for d in directories: |
| 87 | + new_directory = d.rstrip("/") |
| 88 | + download_files(url, new_directory, directory_prefix=new_directory_prefix) |
| 89 | + |
| 90 | + |
| 91 | +def download(): |
| 92 | + download_files( |
| 93 | + "https://web.lcrc.anl.gov/public/e3sm/e3sm_diags_test_data/integration", |
| 94 | + TEST_DATA_DIR, |
| 95 | + directory_prefix=TEST_ROOT_PATH, |
| 96 | + ) |
| 97 | + download_files( |
| 98 | + "https://web.lcrc.anl.gov/public/e3sm/e3sm_diags_test_data/integration/expected", |
| 99 | + TEST_IMAGES_DIR, |
| 100 | + directory_prefix=TEST_ROOT_PATH, |
| 101 | + ) |
| 102 | + print(f"Downloaded {TEST_DATA_DIR} and {TEST_ROOT_PATH}") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + download() |
0 commit comments