Skip to content

Commit 22c80d0

Browse files
committed
Move script to python folder
1 parent 0535933 commit 22c80d0

5 files changed

Lines changed: 43 additions & 18 deletions

File tree

python/sphinx_docs/README.md

Lines changed: 13 additions & 1 deletion
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+
```

python/sphinx_docs/docs/conf.py

Lines changed: 2 additions & 2 deletions
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
}

python/sphinx_docs/pyproject.toml

Lines changed: 6 additions & 3 deletions
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 <hakon.hagland@gmail.com>"]
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"

scripts/download_files.py renamed to python/sphinx_docs/src/opm_python_docs/download_files.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@
22

33
import logging
44
import requests
5+
import subprocess
56
from pathlib import Path
67

78
URL_SIMULATORS = "https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json"
89
URL_COMMON = "https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json"
910
URL_DUNE_MODULE = "https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module"
1011

11-
def get_script_dir():
12-
"""Return the directory of the script."""
13-
script_path = Path(__file__).resolve()
14-
script_dir = script_path.parent
15-
return script_dir
12+
def get_git_root() -> Path:
13+
"""Return the absolute path of the opm-python-documentation repository's root."""
14+
try:
15+
output = subprocess.check_output(
16+
["git", "rev-parse", "--show-toplevel"],
17+
stderr=subprocess.STDOUT
18+
)
19+
except subprocess.CalledProcessError:
20+
# Handle the error if we're not inside a Git repo, etc.
21+
raise RuntimeError("Not a valid Git repository or other error occurred.")
22+
# Check that the parent directory is the opm-python-documentation repository
23+
root = output.decode("utf-8").strip()
24+
if not root.endswith("opm-python-documentation"):
25+
raise RuntimeError("Not in the opm-python-documentation repository.")
26+
return Path(root)
1627

1728
def convert_pr_to_commit_hash(repo: str, pr_number: str) -> str:
1829
"""Convert a PR number to a commit hash."""
@@ -24,7 +35,6 @@ def convert_pr_to_commit_hash(repo: str, pr_number: str) -> str:
2435

2536
def download_docstring_file(url: str) -> None:
2637
"""Download a docstrings file from a URL (either opm-simulators or opm-common)."""
27-
# Ask command line user question if to use master or PR branch
2838
if "opm-simulators" in url:
2939
repo = "opm-simulators"
3040
filename = "docstrings_simulators.json"
@@ -44,9 +54,9 @@ def download_docstring_file(url: str) -> None:
4454
logging.info(f"Downloading docstrings file from {url}")
4555
response = requests.get(url)
4656
response.raise_for_status() # Raises 404 if the file is not found
47-
script_dir = get_script_dir()
48-
save_path = script_dir.parent / "python" / filename
49-
with open(save_path, "wb") as file:
57+
git_root_dir = get_git_root()
58+
save_path = git_root_dir / "python" / filename
59+
with open(str(save_path), "wb") as file:
5060
file.write(response.content)
5161
logging.info(f"Saved docstrings file to {save_path}")
5262

@@ -55,17 +65,17 @@ def download_dune_module() -> None:
5565
logging.info("Downloading dune.module file")
5666
response = requests.get(URL_DUNE_MODULE)
5767
response.raise_for_status()
58-
script_dir = get_script_dir()
59-
save_path = script_dir.parent / "dune.module"
68+
git_root_dir = get_git_root()
69+
save_path = git_root_dir / "dune.module"
6070
with open(save_path, "wb") as file:
6171
file.write(response.content)
6272
logging.info(f"Saved dune.module file to {save_path}")
6373

6474
def main() -> None:
75+
logging.basicConfig(level=logging.INFO)
6576
download_docstring_file(URL_SIMULATORS)
6677
download_docstring_file(URL_COMMON)
6778
download_dune_module()
6879

6980
if __name__ == '__main__':
70-
logging.basicConfig(level=logging.INFO)
7181
main()

python/sphinx_docs/src/opm_simulators_docs/sphinx_ext_docstrings.py renamed to python/sphinx_docs/src/opm_python_docs/sphinx_ext_docstrings.py

File renamed without changes.

0 commit comments

Comments
 (0)