|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import requests |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import click |
| 9 | + |
| 10 | +URL_SIMULATORS = "https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json" |
| 11 | +URL_COMMON = "https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json" |
| 12 | +URL_DUNE_MODULE = "https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module" |
| 13 | + |
| 14 | +def get_git_root() -> Path: |
| 15 | + """Return the absolute path of the opm-python-documentation repository's root.""" |
| 16 | + try: |
| 17 | + output = subprocess.check_output( |
| 18 | + ["git", "rev-parse", "--show-toplevel"], |
| 19 | + stderr=subprocess.STDOUT |
| 20 | + ) |
| 21 | + except subprocess.CalledProcessError: |
| 22 | + # Handle the error if we're not inside a Git repo, etc. |
| 23 | + raise RuntimeError("Not a valid Git repository or other error occurred.") |
| 24 | + # Check that the parent directory is the opm-python-documentation repository |
| 25 | + root = output.decode("utf-8").strip() |
| 26 | + if not root.endswith("opm-python-documentation"): |
| 27 | + raise RuntimeError("Not in the opm-python-documentation repository.") |
| 28 | + return Path(root) |
| 29 | + |
| 30 | +def convert_pr_to_commit_hash(repo: str, pr_number: int) -> str: |
| 31 | + """Convert a PR number to a commit hash.""" |
| 32 | + url = f"https://api.github.com/repos/OPM/{repo}/pulls/{pr_number}" |
| 33 | + response = requests.get(url) |
| 34 | + response.raise_for_status() |
| 35 | + commit_hash = response.json()["head"]["sha"] |
| 36 | + return commit_hash |
| 37 | + |
| 38 | +def download_docstring_file(url: str, pr_number: int|None) -> None: |
| 39 | + """Download a docstrings file from a URL (either opm-simulators or opm-common).""" |
| 40 | + if "opm-simulators" in url: |
| 41 | + repo = "opm-simulators" |
| 42 | + filename = "docstrings_simulators.json" |
| 43 | + else: |
| 44 | + repo = "opm-common" |
| 45 | + filename = "docstrings_common.json" |
| 46 | + if pr_number is not None: |
| 47 | + commit_hash = convert_pr_to_commit_hash(repo, pr_number) |
| 48 | + url = url.replace("/master/", f"/{commit_hash}/") |
| 49 | + logging.info(f"Downloading docstrings file from {url}") |
| 50 | + response = requests.get(url) |
| 51 | + response.raise_for_status() # Raises 404 if the file is not found |
| 52 | + git_root_dir = get_git_root() |
| 53 | + save_path = git_root_dir / "python" / filename |
| 54 | + with open(str(save_path), "wb") as file: |
| 55 | + file.write(response.content) |
| 56 | + logging.info(f"Saved docstrings file to {save_path}") |
| 57 | + |
| 58 | +def download_dune_module() -> None: |
| 59 | + """Download the dune.module file from the opm-simulators repository.""" |
| 60 | + logging.info("Downloading dune.module file") |
| 61 | + response = requests.get(URL_DUNE_MODULE) |
| 62 | + response.raise_for_status() |
| 63 | + git_root_dir = get_git_root() |
| 64 | + save_path = git_root_dir / "dune.module" |
| 65 | + with open(save_path, "wb") as file: |
| 66 | + file.write(response.content) |
| 67 | + logging.info(f"Saved dune.module file to {save_path}") |
| 68 | + |
| 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: |
| 95 | + logging.basicConfig(level=logging.INFO) |
| 96 | + download_docstring_file(URL_SIMULATORS, pr_number=opm_simulators) |
| 97 | + download_docstring_file(URL_COMMON, pr_number=opm_common) |
| 98 | + download_dune_module() |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments