Skip to content

Commit 5cd9f9d

Browse files
committed
Convert to CLI and click app
1 parent 89d3363 commit 5cd9f9d

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

python/sphinx_docs/src/opm_python_docs/download_files.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import subprocess
66
from pathlib import Path
77

8+
import click
9+
810
URL_SIMULATORS = "https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json"
911
URL_COMMON = "https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json"
1012
URL_DUNE_MODULE = "https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module"
@@ -25,30 +27,23 @@ def get_git_root() -> Path:
2527
raise RuntimeError("Not in the opm-python-documentation repository.")
2628
return Path(root)
2729

28-
def convert_pr_to_commit_hash(repo: str, pr_number: str) -> str:
30+
def convert_pr_to_commit_hash(repo: str, pr_number: int) -> str:
2931
"""Convert a PR number to a commit hash."""
3032
url = f"https://api.github.com/repos/OPM/{repo}/pulls/{pr_number}"
3133
response = requests.get(url)
3234
response.raise_for_status()
3335
commit_hash = response.json()["head"]["sha"]
3436
return commit_hash
3537

36-
def download_docstring_file(url: str) -> None:
38+
def download_docstring_file(url: str, pr_number: int|None) -> None:
3739
"""Download a docstrings file from a URL (either opm-simulators or opm-common)."""
3840
if "opm-simulators" in url:
3941
repo = "opm-simulators"
4042
filename = "docstrings_simulators.json"
4143
else:
4244
repo = "opm-common"
4345
filename = "docstrings_common.json"
44-
print(f"Downloading docstrings file from {repo} repository. "
45-
"Should we use the master branch or a PR branch?")
46-
branch = input("Enter 'master' or a PR number: ")
47-
if branch != "master":
48-
pr_number = branch
49-
if not pr_number.isdigit():
50-
print("Invalid PR number.")
51-
return
46+
if pr_number is not None:
5247
commit_hash = convert_pr_to_commit_hash(repo, pr_number)
5348
url = url.replace("/master/", f"/{commit_hash}/")
5449
logging.info(f"Downloading docstrings file from {url}")
@@ -71,10 +66,35 @@ def download_dune_module() -> None:
7166
file.write(response.content)
7267
logging.info(f"Saved dune.module file to {save_path}")
7368

74-
def main() -> None:
69+
# CLI command: opmdoc-download-files
70+
#
71+
# SHELL USAGE:
72+
#
73+
# opmdoc-download-files --opm-simulators <pr-number> --opm-common <pr-number>
74+
#
75+
# DESCRIPTION:
76+
#
77+
# Downloads the docstring JSON files from opm-simulators and opm-common. Also downloads
78+
# the dune.module from opm-simulators. By default, the files are downloaded from the
79+
# master branches. If a PR number is provided, the files are downloaded from the corresponding
80+
# PR branch.
81+
#
82+
# EXAMPLES:
83+
#
84+
# opmdoc-download-files # Downloads the docstrings files and dune.module file from master branches
85+
#
86+
# opmdoc-download-files \
87+
# --opm-simulators 1234 \
88+
# --opm-common 5678 # Downloads the docstrings files from PR 1234 and 5678 and dune.module from master
89+
#
90+
#
91+
@click.command()
92+
@click.option("--opm-simulators", type=int, help="PR number for opm-simulators")
93+
@click.option("--opm-common", type=int, help="PR number for opm-common")
94+
def main(opm_simulators: int|None, opm_common: int|None) -> None:
7595
logging.basicConfig(level=logging.INFO)
76-
download_docstring_file(URL_SIMULATORS)
77-
download_docstring_file(URL_COMMON)
96+
download_docstring_file(URL_SIMULATORS, pr_number=opm_simulators)
97+
download_docstring_file(URL_COMMON, pr_number=opm_common)
7898
download_dune_module()
7999

80100
if __name__ == '__main__':

0 commit comments

Comments
 (0)