-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_switcher.py
More file actions
351 lines (282 loc) · 10.9 KB
/
generate_switcher.py
File metadata and controls
351 lines (282 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025 Autodesk, Inc.
# SPDX-License-Identifier: Apache-2.0
"""
Generate switcher.json for documentation version switcher.
Usage:
generate_switcher.py [--include-current]
Options:
--include-current Include the current version from version.json if it is
newer than the latest tag.
"""
import json
import logging
import os
import re
import sys
import docopt
import git
from packaging.version import InvalidVersion, Version
# Must match smv_tag_whitelist in docs/source/conf.py
SMV_TAG_PATTERN = re.compile(r'^v\d+\.\d+\.\d+$')
# Paths
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
DOCS_STATIC_DIR = os.path.join(ROOT_DIR, 'docs', 'source', '_static')
SWITCHER_JSON = os.path.join(DOCS_STATIC_DIR, 'switcher.json')
VERSION_JSON = os.path.join(ROOT_DIR, 'version.json')
def get_git_tags():
"""Fetch all git tags from the repository."""
try:
repo = git.Repo(ROOT_DIR)
return [tag.name for tag in repo.tags]
except git.InvalidGitRepositoryError as err:
logging.error("Not a valid git repository: %s", err)
raise
except git.GitCommandError as err:
logging.error("Failed to fetch git tags: %s", err)
raise
def parse_version_tags(tags):
"""
Filter tags to those matching the required vX.Y.Z format.
Uses the same pattern as smv_tag_whitelist in docs/source/conf.py
so that switcher.json stays in sync with the versions that
sphinx-multiversion actually builds.
Returns the matching tag strings.
"""
version_tags = []
for tag in tags:
if not SMV_TAG_PATTERN.match(tag):
continue
version_str = tag.lstrip('v')
try:
Version(version_str)
version_tags.append(tag)
except InvalidVersion:
logging.warning("Skipping invalid version tag: %s", tag)
continue
return version_tags
def get_version_from_json():
"""
Read version from version.json file.
Returns version string in format 'vX.Y.Z', or None if the file does not
exist. Raises ValueError if the file exists but is invalid (malformed
JSON, missing fields, non-numeric values, etc.) so that a broken
version.json is always a hard failure rather than a silent skip.
"""
try:
with open(VERSION_JSON, 'r', encoding='utf-8') as f:
version_data = json.load(f)
except FileNotFoundError:
logging.warning("version.json file not found at: %s", VERSION_JSON)
return None
except json.JSONDecodeError as err:
raise ValueError(f"version.json is not valid JSON: {err}") from err
required_fields = ['major', 'minor', 'patch']
for field in required_fields:
if field not in version_data:
raise ValueError(f"version.json is missing required field: {field}")
try:
major = int(version_data['major'])
minor = int(version_data['minor'])
patch = int(version_data['patch'])
except (ValueError, TypeError) as err:
raise ValueError(
f"version.json contains non-numeric version values: "
f"major={version_data.get('major')}, "
f"minor={version_data.get('minor')}, "
f"patch={version_data.get('patch')}"
) from err
if major < 0 or minor < 0 or patch < 0:
raise ValueError(
f"version.json contains negative version numbers: " f"{major}.{minor}.{patch}"
)
version_str = f"v{major}.{minor}.{patch}"
try:
Version(version_str.lstrip('v'))
except InvalidVersion as err:
raise ValueError(f"Invalid semantic version in version.json: {version_str}") from err
return version_str
def sort_versions(version_tags):
"""Sort version tags in descending order (latest first)."""
def version_key(tag):
try:
return Version(tag.lstrip('v'))
except InvalidVersion:
return Version("0.0.0")
return sorted(version_tags, key=version_key, reverse=True)
def _validate_version_progression(json_version, latest_tag_version):
"""
Validate that version.json is a legal next version after the latest tag.
Raises ValueError if the progression skips versions or violates reset
rules (e.g. patch must be 0 when bumping minor).
"""
json_ver = Version(json_version.lstrip('v'))
tag_ver = Version(latest_tag_version.lstrip('v'))
if json_ver <= tag_ver:
return
j_major, j_minor, j_patch = json_ver.release[:3]
t_major, t_minor, t_patch = tag_ver.release[:3]
if j_major == t_major and j_minor == t_minor:
if j_patch > t_patch + 1:
logging.error(
"version.json (%s) skips patch versions from latest "
"tag (%s). Expected v%d.%d.%d",
json_version,
latest_tag_version,
t_major,
t_minor,
t_patch + 1,
)
raise ValueError(
f"Invalid version progression: "
f"{latest_tag_version} -> {json_version}. "
f"Cannot skip versions. "
f"Expected v{t_major}.{t_minor}.{t_patch + 1}"
)
elif j_major == t_major and j_minor == t_minor + 1:
if j_patch != 0:
logging.error(
"version.json (%s) has non-zero patch when bumping "
"minor version. Expected v%d.%d.0",
json_version,
j_major,
j_minor,
)
raise ValueError(
f"Invalid version: {json_version}. "
f"When bumping minor version from "
f"{latest_tag_version}, patch must be 0. "
f"Expected v{j_major}.{j_minor}.0"
)
elif j_major == t_major and j_minor > t_minor + 1:
logging.error(
"version.json (%s) skips minor versions from latest " "tag (%s). Expected v%d.%d.0",
json_version,
latest_tag_version,
t_major,
t_minor + 1,
)
raise ValueError(
f"Invalid version progression: "
f"{latest_tag_version} -> {json_version}. "
f"Cannot skip versions. "
f"Expected v{t_major}.{t_minor + 1}.0"
)
elif j_major == t_major + 1:
if j_minor != 0 or j_patch != 0:
logging.error(
"version.json (%s) has non-zero minor/patch when "
"bumping major version. Expected v%d.0.0",
json_version,
j_major,
)
raise ValueError(
f"Invalid version: {json_version}. "
f"When bumping major version from "
f"{latest_tag_version}, minor and patch must be 0. "
f"Expected v{j_major}.0.0"
)
elif j_major > t_major + 1:
logging.error(
"version.json (%s) skips major versions from latest " "tag (%s). Expected v%d.0.0",
json_version,
latest_tag_version,
t_major + 1,
)
raise ValueError(
f"Invalid version progression: "
f"{latest_tag_version} -> {json_version}. "
f"Cannot skip versions. "
f"Expected v{t_major + 1}.0.0"
)
def generate_switcher_json(version_tags, include_current=False):
"""
Generate the switcher.json structure.
Returns a list of version entries with the format expected by
pydata-sphinx-theme's version switcher.
Always reads version.json and validates its progression against the
latest git tag (to catch skipped versions early, even in CI).
When include_current is True and version.json is newer than the latest
tag, that version is also added to the switcher output.
"""
if not version_tags:
logging.warning("No version tags found!")
return []
sorted_tags = sort_versions(version_tags)
switcher_data = []
latest_tag_version = sorted_tags[0]
json_version = get_version_from_json()
add_json_version = False
if json_version and latest_tag_version:
try:
json_ver = Version(json_version.lstrip('v'))
tag_ver = Version(latest_tag_version.lstrip('v'))
version_json_is_newer = json_ver > tag_ver
if version_json_is_newer:
_validate_version_progression(json_version, latest_tag_version)
if include_current:
add_json_version = True
except InvalidVersion:
pass
if add_json_version:
logging.info(
"version.json (%s) is newer than latest tag (%s), " "adding as latest",
json_version,
latest_tag_version,
)
switcher_data.append(
{
"version": json_version,
"name": f"{json_version} (latest)",
"url": f"../{json_version}/",
"is_latest": True,
}
)
for i, tag in enumerate(sorted_tags):
is_latest = (i == 0) and not add_json_version
switcher_data.append(
{
"version": tag,
"name": f"{tag} (latest)" if is_latest else tag,
"url": f"../{tag}/",
"is_latest": is_latest,
}
)
return switcher_data
def write_switcher_json(switcher_data):
"""Write the switcher data to switcher.json file."""
# Ensure the directory exists
os.makedirs(DOCS_STATIC_DIR, exist_ok=True)
with open(SWITCHER_JSON, 'w', encoding='utf-8') as f:
json.dump(switcher_data, f, indent=2)
logging.info("Generated switcher.json with %d versions", len(switcher_data))
logging.info("Latest version: %s", switcher_data[0]['version'] if switcher_data else 'None')
def main():
"""Main entry point."""
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
args = docopt.docopt(__doc__)
include_current = args.get('--include-current')
try:
logging.info("Fetching git tags...")
tags = get_git_tags()
if not tags:
logging.error("No git tags found in the repository!")
return 1
logging.info("Found %d total tags", len(tags))
logging.info("Parsing version tags...")
version_tags = parse_version_tags(tags)
if not version_tags:
logging.error("No valid version tags found!")
return 1
logging.info("Found %d version tags", len(version_tags))
logging.info("Generating switcher.json...")
switcher_data = generate_switcher_json(version_tags, include_current=include_current)
logging.info("Writing switcher.json to %s", SWITCHER_JSON)
write_switcher_json(switcher_data)
logging.info("Successfully generated switcher.json!")
return 0
except Exception as err:
logging.error("Failed to generate switcher.json: %s", err, exc_info=True)
return 1
if __name__ == '__main__':
sys.exit(main())