Skip to content

Commit 7a8c7e3

Browse files
authored
Merge pull request #10 from hakonhagland/view_docs
Commands for viewing the documentation
2 parents 12db73a + fea8cf3 commit 7a8c7e3

File tree

6 files changed

+85
-23
lines changed

6 files changed

+85
-23
lines changed

python/sphinx_docs/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
.PHONY: docs
1+
.PHONY: docs view-docs
2+
3+
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
4+
GIT_ROOT := $(shell git rev-parse --show-toplevel)
25

36
# Build the documentation locally for the current branch
47
# NOTE: You need to commit your changes before running this command
5-
CURRENT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
68
docs:
79
sphinx-versioned -m ${CURRENT_BRANCH} -b ${CURRENT_BRANCH} --git-root ../../
10+
11+
view-docs:
12+
@xdg-open "file://$(GIT_ROOT)/python/sphinx_docs/docs/_build/$(CURRENT_BRANCH)/index.html"

python/sphinx_docs/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ After installation, you can run the following scripts:
3030
# Downloads docstrings JSON files and dune.module file before building the documentation locally
3131
$ opmdoc-download-files
3232
# Generate the documentation
33-
$ make
34-
# View the generated documentation in the browser
33+
$ make docs
34+
# View the generated documentation for the current branch in the browser
35+
$ make view-docs
36+
# Or for a specific branch
37+
$ opmdoc-view-doc --branch=master
3538
```

python/sphinx_docs/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sphinx-versioned-docs = "^1.3.1"
1818

1919
[tool.poetry.scripts]
2020
opmdoc-download-files = "opm_python_docs.download_files:main"
21+
opmdoc-view-doc = "opm_python_docs.view_docs:main"
2122

2223
[build-system]
2324
requires = ["poetry-core"]

python/sphinx_docs/src/opm_python_docs/download_files.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
import logging
44
import requests
5-
import subprocess
6-
from pathlib import Path
75

86
import click
97

8+
from opm_python_docs import helpers
9+
1010
URL_SIMULATORS = "https://raw.githubusercontent.com/OPM/opm-simulators/master/python/docstrings_simulators.json"
1111
URL_COMMON = "https://raw.githubusercontent.com/OPM/opm-common/master/python/docstrings_common.json"
1212
URL_DUNE_MODULE = "https://raw.githubusercontent.com/OPM/opm-simulators/master/dune.module"
1313

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)
2914

3015
def convert_pr_to_commit_hash(repo: str, pr_number: int) -> str:
3116
"""Convert a PR number to a commit hash."""
@@ -49,7 +34,7 @@ def download_docstring_file(url: str, pr_number: int|None) -> None:
4934
logging.info(f"Downloading docstrings file from {url}")
5035
response = requests.get(url)
5136
response.raise_for_status() # Raises 404 if the file is not found
52-
git_root_dir = get_git_root()
37+
git_root_dir = helpers.get_git_root()
5338
save_path = git_root_dir / "python" / filename
5439
with open(str(save_path), "wb") as file:
5540
file.write(response.content)
@@ -60,7 +45,7 @@ def download_dune_module() -> None:
6045
logging.info("Downloading dune.module file")
6146
response = requests.get(URL_DUNE_MODULE)
6247
response.raise_for_status()
63-
git_root_dir = get_git_root()
48+
git_root_dir = helpers.get_git_root()
6449
save_path = git_root_dir / "dune.module"
6550
with open(save_path, "wb") as file:
6651
file.write(response.content)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import subprocess
2+
from pathlib import Path
3+
4+
def get_current_branch() -> str:
5+
"""Return the current Git branch."""
6+
try:
7+
output = subprocess.check_output(
8+
["git", "branch", "--show-current"],
9+
stderr=subprocess.STDOUT
10+
)
11+
except subprocess.CalledProcessError:
12+
# Handle the error if we're not inside a Git repo, etc.
13+
raise RuntimeError("Not a valid Git repository or other error occurred.")
14+
return output.decode("utf-8").strip()
15+
16+
def get_git_root() -> Path:
17+
"""Return the absolute path of the opm-python-documentation repository's root."""
18+
try:
19+
output = subprocess.check_output(
20+
["git", "rev-parse", "--show-toplevel"],
21+
stderr=subprocess.STDOUT
22+
)
23+
except subprocess.CalledProcessError:
24+
# Handle the error if we're not inside a Git repo, etc.
25+
raise RuntimeError("Not a valid Git repository or other error occurred.")
26+
# Check that the parent directory is the opm-python-documentation repository
27+
root = output.decode("utf-8").strip()
28+
if not root.endswith("opm-python-documentation"):
29+
raise RuntimeError("Not in the opm-python-documentation repository.")
30+
return Path(root)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/env python3
2+
3+
import logging
4+
5+
import click
6+
7+
from opm_python_docs import helpers
8+
9+
10+
# CLI command: opmdoc-view-doc
11+
#
12+
# SHELL USAGE:
13+
#
14+
# opmdoc-view-doc --branch <branch>
15+
#
16+
# DESCRIPTION:
17+
#
18+
# View the generated sphinx documentation for the given branch in the default browser.
19+
# If the branch is not provided, the current git branch is used.
20+
#
21+
# EXAMPLES:
22+
#
23+
# opmdoc-view-doc # Opens index.html for the current git branch in the default browser
24+
#
25+
#
26+
#
27+
@click.command()
28+
@click.option("--branch", type=str, help="Branch to view documentation for. If not provided, the current git branch is used.")
29+
def main(branch: str|None) -> None:
30+
logging.basicConfig(level=logging.INFO)
31+
git_root_dir = helpers.get_git_root()
32+
branch = branch or helpers.get_current_branch()
33+
logging.info(f"Opening documentation for branch {branch}")
34+
url = f"file://{git_root_dir}/python/sphinx_docs/docs/_build/{branch}/index.html"
35+
click.launch(url)
36+
37+
if __name__ == '__main__':
38+
main()

0 commit comments

Comments
 (0)