Skip to content

Commit 4026f3c

Browse files
Merge pull request #618 from laughingman7743/fix-version-detection-in-multiversion-docs
fix: Fix version detection in sphinx-multiversion documentation builds
2 parents f05ff4d + 453c719 commit 4026f3c

3 files changed

Lines changed: 84 additions & 20 deletions

File tree

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ tox:
2727

2828
.PHONY: docs
2929
docs:
30-
uv build
3130
uv run sphinx-multiversion docs docs/_build/html
3231
echo '<meta http-equiv="refresh" content="0; url=./master/index.html">' > docs/_build/html/index.html
3332
touch docs/_build/html/.nojekyll

docs/conf.py

Lines changed: 84 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,87 @@
22
#
33
# For the full list of built-in configuration values, see the documentation:
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
import subprocess
56
from datetime import datetime, timezone
6-
from pathlib import Path
77

88

99
def get_version():
10+
# Use git commands to get version from the checked-out ref.
11+
# sphinx-multiversion checks out each ref to a temp directory and runs Sphinx there.
12+
# Use current working directory to get the version of the checked-out ref.
13+
try:
14+
# Try to get exact tag (for tagged commits)
15+
result = subprocess.run(
16+
["git", "describe", "--tags", "--exact-match"],
17+
capture_output=True,
18+
text=True,
19+
check=True,
20+
)
21+
tag = result.stdout.strip()
22+
if tag and tag.startswith("v"):
23+
return tag[1:] # Remove 'v' prefix
24+
return tag
25+
except (subprocess.CalledProcessError, FileNotFoundError):
26+
pass
27+
28+
# Try to get version using git describe (for non-tagged commits)
29+
try:
30+
result = subprocess.run(
31+
["git", "describe", "--tags", "--always"],
32+
capture_output=True,
33+
text=True,
34+
check=True,
35+
)
36+
version_str = result.stdout.strip()
37+
if version_str and version_str.startswith("v"):
38+
return version_str[1:] # Remove 'v' prefix
39+
return version_str
40+
except (subprocess.CalledProcessError, FileNotFoundError):
41+
pass
42+
43+
# Fallback to _version.py (for local development builds)
1044
try:
11-
# Try to import from _version.py (generated by setuptools-scm)
1245
from pyathena._version import __version__
46+
1347
return __version__
1448
except ImportError:
15-
try:
16-
# Fallback to importlib.metadata
17-
from importlib.metadata import version
18-
return version("PyAthena")
19-
except Exception:
20-
return "unknown"
49+
pass
50+
51+
# Final fallback to importlib.metadata
52+
try:
53+
from importlib.metadata import version
54+
55+
return version("PyAthena")
56+
except Exception:
57+
return "unknown"
58+
59+
60+
# -- Setup function ----------------------------------------------------------
61+
62+
63+
def config_inited(app, config):
64+
"""Handler for config-inited event to set version dynamically."""
65+
smv_current_version = getattr(config, "smv_current_version", None)
66+
67+
if smv_current_version:
68+
# sphinx-multiversion sets this to the ref name (e.g., "v3.19.0" or "master")
69+
if smv_current_version.startswith("v") and smv_current_version[1:2].isdigit():
70+
# It's a version tag like "v3.19.0"
71+
ver = smv_current_version[1:] # Remove 'v' prefix
72+
else:
73+
# It's a branch name like "master", use git to get version
74+
ver = get_version()
75+
else:
76+
# Not running under sphinx-multiversion, use git
77+
ver = get_version()
78+
79+
config.version = f"v{ver}"
80+
config.release = f"v{ver}"
81+
82+
83+
def setup(app):
84+
"""Sphinx setup hook."""
85+
app.connect("config-inited", config_inited)
2186

2287

2388
# -- Project information -----------------------------------------------------
@@ -26,8 +91,9 @@ def get_version():
2691
project = "PyAthena"
2792
copyright = f"{datetime.now(timezone.utc).year}, laughingman7743"
2893
author = "laughingman7743"
29-
version = f"v{get_version()}"
30-
release = f"v{get_version()}"
94+
# Version will be set dynamically in setup() function
95+
version = ""
96+
release = ""
3197

3298
# -- General configuration ---------------------------------------------------
3399
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
@@ -60,11 +126,11 @@ def get_version():
60126

61127
# Autodoc settings
62128
autodoc_default_options = {
63-
'members': True,
64-
'member-order': 'bysource',
65-
'special-members': '__init__',
66-
'undoc-members': True,
67-
'exclude-members': '__weakref__'
129+
"members": True,
130+
"member-order": "bysource",
131+
"special-members": "__init__",
132+
"undoc-members": True,
133+
"exclude-members": "__weakref__",
68134
}
69135

70136
# Autosummary settings
@@ -73,9 +139,9 @@ def get_version():
73139

74140
# Intersphinx mapping
75141
intersphinx_mapping = {
76-
'python': ('https://docs.python.org/3', None),
77-
'pandas': ('https://pandas.pydata.org/pandas-docs/stable', None),
78-
'pyarrow': ('https://arrow.apache.org/docs/', None),
142+
"python": ("https://docs.python.org/3", None),
143+
"pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
144+
"pyarrow": ("https://arrow.apache.org/docs/", None),
79145
}
80146

81147
templates_path = ["_templates"]

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ line-length = 100
106106
exclude = [
107107
".venv",
108108
".tox",
109-
"docs",
110109
]
111110
target-version = "py39"
112111

0 commit comments

Comments
 (0)