Skip to content

Commit a15cca6

Browse files
authored
Merge pull request #7 from hakonhagland/download_script
Script to download docstring files
2 parents d6132b0 + 5cd9f9d commit a15cca6

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Repository to host the Python documentation for OPM Flow
22

33
## Building the documentation locally
4-
Follow the commands in .github/workflows/python_sphinx_docs.yml for your local setup!
4+
Follow the commands in `.github/workflows/python_sphinx_docs.yml` for your local setup!
5+
6+
See also the script [opmdoc-download-files](https://github.com/OPM/opm-python-documentation/blob/master/python/sphinx_docs/README.md) for more information.
57

68
## Building the documentation online on your fork
79
- Turn on github actions at `https://github.com/<your-github-username>/opm-python-documentation/actions`

Diff for: python/sphinx_docs/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python scripts for building opm-simulators sphinx documentation
1+
# Python scripts for building opm-simulators and opm-common sphinx documentation
22

33
## Installation of the python scripts
44
- Requires python3 >= 3.10
@@ -21,3 +21,15 @@ $ python -m venv .venv
2121
$ source .venv/bin/activate
2222
$ pip install .
2323
```
24+
25+
### Scripts
26+
27+
After installation, you can run the following scripts:
28+
29+
```
30+
# Downloads docstrings JSON files and dune.module file before building the documentation locally
31+
$ opmdoc-download-files
32+
# Generate the documentation
33+
$ make
34+
# View the generated documentation in the browser
35+
```

Diff for: python/sphinx_docs/docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def extract_opm_simulators_release():
3434
# opm.simulators.BlackOilSimulator Python module will also have access to the docstrings.)
3535
sys.path.insert(0, os.path.abspath("../src"))
3636
# Our sphinx extension that will use the docstrings.json file to generate documentation
37-
extensions = ["opm_simulators_docs.sphinx_ext_docstrings"]
37+
extensions = ["opm_python_docs.sphinx_ext_docstrings"]
3838
# Path to docstrings.json
3939
opm_simulators_docstrings_path = os.path.abspath('../../docstrings_simulators.json')
4040
opm_common_docstrings_path = os.path.abspath('../../docstrings_common.json')
@@ -50,7 +50,7 @@ def extract_opm_simulators_release():
5050
html_context = {
5151
"display_github": True,
5252
"github_user": "OPM",
53-
"github_repo": "opm-simulators",
53+
"github_repo": "opm-python-documentation",
5454
"github_version": "master",
5555
"conf_py_path": "/python/docs/",
5656
}

Diff for: python/sphinx_docs/pyproject.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[tool.poetry]
2-
name = "opm-simulators-docs"
2+
name = "opm-python-docs"
33
version = "0.1.0"
44
description = """Helper scripts for generating sphinx documentation for the \
5-
opm-simulators Python bindings"""
5+
opm-simulators and opm-common Python bindings"""
66
authors = ["Håkon Hægland <[email protected]>"]
77
readme = "README.md"
8-
packages = [{ include = "opm_simulators_docs", from = "src"}]
8+
packages = [{ include = "opm_python_docs", from = "src"}]
99
license = "GPL3"
1010
repository = "https://github.com/OPM/opm-simulators"
1111

@@ -16,6 +16,9 @@ sphinx-rtd-theme = "^1.3.0"
1616
click = "^8.1.7"
1717
sphinx-versioned-docs = "^1.3.1"
1818

19+
[tool.poetry.scripts]
20+
opmdoc-download-files = "opm_python_docs.download_files:main"
21+
1922
[build-system]
2023
requires = ["poetry-core"]
2124
build-backend = "poetry.core.masonry.api"
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)