5.13.2-dev0
mHM
The mesoscale Hydrological Model
Loading...
Searching...
No Matches
download.py
Go to the documentation of this file.
1"""!
2Download routines to get test domains for mHM.
3
4@copyright Copyright 2005-@today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
5 mHM is released under the LGPLv3+ license @license_note
6@ingroup mhm
7"""
8
9import argparse
10import os
11import shutil
12import tarfile
13from pathlib import Path
14from tempfile import TemporaryDirectory
15from urllib.error import HTTPError
16from urllib.request import urlretrieve
17
18# query = "?path={folder}"
19MHM_URL = "https://git.ufz.de/mhm/mhm/-/archive/{branch}/mhm-{branch}.{format}{query}"
20# when renaming develop to main, we need to be ready
21BRANCH_MAP = {"main": "develop", "develop": "main"}
22VALID_DOMAINS = [1, 2]
23
24
25def _dl(branch, format, folder, filename):
26 url = MHM_URL.format(branch=branch, format=format, query=f"?path={folder}")
27 urlretrieve(url, filename)
28
29
30def download_test(branch=None, domain=1, path=None, verbose=False):
31 """
32 Download a test domain for mHM.
33
34 @param branch (str, optional): Branch, tag, or commit of the mHM repository to
35 take the test domain from, by default tag determined from the mHM version
36 @param domain (int, optional): Test domain 1 or 2, by default 1
37 @param path (pathlike, optional): Destination path for the downloaded folder,
38 by default original name of the test domain folder
39 """
40 # format fixed to tar.gz
41 format = "tar.gz"
42 # determine branch from mhm version
43 if branch is None:
44 from . import __version__
45
46 branch = "main" if "dev" in __version__ else f"v{__version__}"
47 # check test domain
48 if domain not in VALID_DOMAINS:
49 msg = f"mhm-download: 'domain' needs to be 1 or 2. Got: '{domain}'"
50 raise ValueError(msg)
51 folder = "test_domain" if domain == 1 else "test_domain_2"
52 path = Path(path or folder)
53 if verbose:
54 print(f"Downloading mHM test domain '{domain}'")
55 print(f" branch: '{branch}'")
56 print(f" path: '{Path(path).resolve()}'")
57 # download to temporary directory
58 with TemporaryDirectory() as tmp_dir:
59 tar_file = Path(tmp_dir) / f"test.{format}"
60 tar_dir = Path(tmp_dir) / "test"
61 # redirect 'main' to 'develop' branch to be future proof
62 try:
63 _dl(branch, format, folder, tar_file)
64 except HTTPError as err:
65 if branch not in BRANCH_MAP:
66 raise err
67 _dl(BRANCH_MAP[branch], format, folder, tar_file)
68 # extract
69 with tarfile.open(tar_file, "r:gz") as tar:
70 tar.extractall(path=tar_dir)
71 # move sub-folder content to top level
72 folder_path = tar_dir / os.listdir(tar_dir)[0] / folder
73 shutil.copytree(folder_path, path, ignore_dangling_symlinks=True)
74
75
76def cli(argv=None): # pragma: no cover
77 """Command line interface to download test domains for mHM."""
78 from . import __version__
79
80 parser = argparse.ArgumentParser(
81 description="Download tool to retrieve the test domains for mHM.",
82 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
83 )
84 parser.add_argument(
85 "-V",
86 "--version",
87 action="version",
88 version=__version__,
89 help="display version information",
90 )
91 parser.add_argument("-v", "--verbose", action="store_true", help="be verbose")
92 parser.add_argument(
93 "-b",
94 "--branch",
95 help=(
96 "branch, tag, or commit of the mHM repository to take the "
97 "test domain from, by default tag determined from the mHM version"
98 ),
99 )
100 parser.add_argument(
101 "-d",
102 "--domain",
103 type=int,
104 default=1,
105 choices=VALID_DOMAINS,
106 help="test domain '1' or '2'",
107 )
108 parser.add_argument(
109 "-p",
110 "--path",
111 help=(
112 "destination path for the downloaded folder, "
113 "by default the original folder name in the current directory"
114 ),
115 )
116 # parse arguments
117 args = parser.parse_args(argv)
118 download_test(
119 branch=args.branch, domain=args.domain, path=args.path, verbose=args.verbose
120 )
_dl(branch, format, folder, filename)
Definition download.py:25