2Download routines to get test domains for mHM.
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
13from pathlib import Path
14from tempfile import TemporaryDirectory
15from urllib.error import HTTPError
16from urllib.request import urlretrieve
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"}
25def _dl(branch, format, folder, filename):
26 url = MHM_URL.format(branch=branch, format=format, query=f"?path={folder}")
27 urlretrieve(url, filename)
30def download_test(branch=None, domain=1, path=None, verbose=False):
32 Download a test domain for mHM.
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
44 from .
import __version__
46 branch =
"main" if "dev" in __version__
else f
"v{__version__}"
48 if domain
not in VALID_DOMAINS:
49 msg = f
"mhm-download: 'domain' needs to be 1 or 2. Got: '{domain}'"
51 folder =
"test_domain" if domain == 1
else "test_domain_2"
52 path = Path(path
or folder)
54 print(f
"Downloading mHM test domain '{domain}'")
55 print(f
" branch: '{branch}'")
56 print(f
" path: '{Path(path).resolve()}'")
58 with TemporaryDirectory()
as tmp_dir:
59 tar_file = Path(tmp_dir) / f
"test.{format}"
60 tar_dir = Path(tmp_dir) /
"test"
63 _dl(branch, format, folder, tar_file)
64 except HTTPError
as err:
65 if branch
not in BRANCH_MAP:
67 _dl(BRANCH_MAP[branch], format, folder, tar_file)
69 with tarfile.open(tar_file,
"r:gz")
as tar:
70 tar.extractall(path=tar_dir)
72 folder_path = tar_dir / os.listdir(tar_dir)[0] / folder
73 shutil.copytree(folder_path, path, ignore_dangling_symlinks=
True)
77 """Command line interface to download test domains for mHM."""
78 from .
import __version__
80 parser = argparse.ArgumentParser(
81 description=
"Download tool to retrieve the test domains for mHM.",
82 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
89 help=
"display version information",
91 parser.add_argument(
"-v",
"--verbose", action=
"store_true", help=
"be verbose")
96 "branch, tag, or commit of the mHM repository to take the "
97 "test domain from, by default tag determined from the mHM version"
105 choices=VALID_DOMAINS,
106 help=
"test domain '1' or '2'",
112 "destination path for the downloaded folder, "
113 "by default the original folder name in the current directory"
117 args = parser.parse_args(argv)
119 branch=args.branch, domain=args.domain, path=args.path, verbose=args.verbose
_dl(branch, format, folder, filename)