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
56from datetime import datetime , timezone
6- from pathlib import Path
77
88
99def 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():
2691project = "PyAthena"
2792copyright = f"{ datetime .now (timezone .utc ).year } , laughingman7743"
2893author = "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
62128autodoc_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
75141intersphinx_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
81147templates_path = ["_templates" ]
0 commit comments