8
8
9
9
from ansys_sphinx_theme import ansys_favicon , get_version_match
10
10
import github
11
- import jinja2
12
11
from PIL import Image
13
12
import requests
14
13
import sphinx
89
88
90
89
metadata = Path (__file__ ).parent .parent .parent / "projects.yaml"
91
90
91
+
92
+ def read_dependencies_from_pyproject ():
93
+ """Read the dependencies declared in the project file."""
94
+ pyproject = Path (__file__ ).parent .parent .parent / "pyproject.toml"
95
+ if not pyproject .exists ():
96
+ raise ValueError (f"The file { pyproject } does not exist." )
97
+
98
+ pyproject_content = toml .loads (pyproject .read_text (encoding = "utf-8" ))
99
+ dependencies = pyproject_content ["project" ]["dependencies" ]
100
+ return {pkg .split ("==" )[0 ]: pkg .split ("==" )[1 ] for pkg in dependencies }
101
+
102
+
103
+ def read_optional_dependencies_from_pyproject ():
104
+ """Read the extra dependencies declared in the project file."""
105
+ pyproject = Path (__file__ ).parent .parent .parent / "pyproject.toml"
106
+ if not pyproject .exists ():
107
+ raise ValueError (f"The file { pyproject } does not exist." )
108
+
109
+ pyproject_content = toml .loads (pyproject .read_text (encoding = "utf-8" ))
110
+ exclude_targets = ["doc" ]
111
+ optional_dependencies = {
112
+ target : {pkg .split ("==" )[0 ]: pkg .split ("==" )[1 ] for pkg in deps }
113
+ for target , deps in pyproject_content ["project" ]["optional-dependencies" ].items ()
114
+ if target not in exclude_targets
115
+ }
116
+ return optional_dependencies
117
+
118
+
119
+ def get_last_metapackage_release ():
120
+ """Retrieve the last release of the metapackage."""
121
+ # Get the PyAnsys metapackage repository
122
+ g = github .Github (os .getenv ("GITHUB_TOKEN" , None ))
123
+ repository = g .get_repo ("ansys/pyansys" )
124
+
125
+ # Get the last release
126
+ last_release = repository .get_latest_release ()
127
+
128
+ return last_release .tag_name
129
+
130
+
131
+ jinja_globals = {
132
+ "LAST_RELEASE" : get_last_metapackage_release (),
133
+ "VERSION" : version ,
134
+ "SUPPORTED_PYTHON_VERSIONS" : ["3.10" , "3.11" , "3.12" ],
135
+ "SUPPORTED_PLATFORMS" : ["Windows" , "macOS" , "Linux" ],
136
+ }
92
137
jinja_contexts = {
93
- "project_context" : {"projects" : yaml .safe_load (metadata .read_text (encoding = "utf-8" ))}
138
+ "project_context" : {"projects" : yaml .safe_load (metadata .read_text (encoding = "utf-8" ))},
139
+ "dependencies" : {"dependencies" : read_dependencies_from_pyproject ()},
140
+ "optional_dependencies" : {"optional_dependencies" : read_optional_dependencies_from_pyproject ()},
141
+ "wheelhouse" : {
142
+ "wheelhouse" : {
143
+ platform : icon
144
+ for platform , icon in zip (["Windows" , "macOS" , "Linux" ], ["windows" , "apple" , "linux" ])
145
+ }
146
+ },
94
147
}
95
148
96
149
html_context = {
97
150
"github_user" : "ansys" ,
98
151
"github_repo" : "pyansys" ,
99
152
"github_version" : "main" ,
100
153
"doc_path" : "doc/source" ,
154
+ "page_assets" : {
155
+ "getting-started/prerequisites" : {
156
+ "needs_datatables" : True ,
157
+ },
158
+ "getting-started/install" : {
159
+ "needs_datatables" : True ,
160
+ },
161
+ },
101
162
}
102
163
103
164
html_theme_options = {
153
214
154
215
# Ignore certain URLs
155
216
linkcheck_ignore = [
156
- r"https://www.ansys.com/ .*" ,
217
+ r"https://www.ansys.com.*" ,
157
218
rf"https://pypi.org/project/pyansys/{ switcher_version } .*" ,
158
219
r"https://ansunits.docs.*" ,
220
+ r"https://download.ansys.com/.*" ,
221
+ r"https://github.com/ansys/pyansys/releases/download/*" ,
159
222
]
160
223
161
224
# User agent
179
242
# file for the minor versions of the PyAnsys metapackage release branches.
180
243
181
244
182
- def generate_rst_files (versions : list [str ], tables : dict [str , list [str ]]):
183
- """Generate the .rst files for the package versions."""
184
- # Create Jinja2 environment
185
- jinja_env = jinja2 .Environment (loader = jinja2 .BaseLoader ())
186
-
187
- # Compile the template
188
- template = jinja_env .from_string (VERSIONS_TEMPLATE )
189
-
190
- # Generate an .rst file for each version entry
191
- for version in versions :
192
- # Generate the content of the .rst file using the Jinja template
193
- rendered_content = template .render (
194
- version = version ,
195
- table = tables [version ],
196
- )
197
-
198
- # Define the output path for the generated file
199
- output_filename = GENERATED_DIR / f"version_{ version } .rst"
200
-
201
- # Write the rendered content to the file
202
- output_filename .write_text (rendered_content , encoding = "utf-8" )
203
-
204
- # Generate the index.rst file
205
- index_template = jinja_env .from_string (INDEX_TEMPLATE )
206
- rendered_index = index_template .render (versions = versions )
207
-
208
- # Write the rendered content to the file
209
- output_filename = GENERATED_DIR / "index.rst"
210
- output_filename .write_text (rendered_index , encoding = "utf-8" )
211
-
212
-
213
245
def get_documentation_link_from_pypi (library : str , version : str ) -> str :
214
246
"""Get the documentation link from PyPI for a specific library and version."""
215
247
# Get the PyPI metadata for the library
@@ -341,31 +373,6 @@ def build_versions_table(branch: str) -> list[str]:
341
373
return table
342
374
343
375
344
- def get_release_branches_in_metapackage ():
345
- """Retrieve the release branches in the PyAnsys metapackage."""
346
- # Get the PyAnsys metapackage repository
347
- g = github .Github (os .getenv ("GITHUB_TOKEN" , None ))
348
- github_repo = g .get_repo ("ansys/pyansys" )
349
-
350
- # Get the branches
351
- branches = github_repo .get_branches ()
352
-
353
- # Get the branches that are release branches + main
354
- release_branches = []
355
- versions = []
356
- for branch in branches :
357
- if branch .name .startswith ("release" ):
358
- release_branches .append (branch .name )
359
- versions .append (branch .name .split ("/" )[- 1 ])
360
-
361
- # Sort the release branches and versions: from newest to oldest
362
- release_branches .reverse ()
363
- versions .reverse ()
364
-
365
- # Return the dev/main branch and the release branches (and versions)
366
- return ["main" ] + release_branches , ["dev" ] + versions
367
-
368
-
369
376
###########################################################################
370
377
# Adapting thumbnails to the documentation
371
378
###########################################################################
@@ -440,15 +447,6 @@ def revert_thumbnails(app: sphinx.application.Sphinx, exception):
440
447
subprocess .run (["git" , "checkout" , "--" , thumbnail_dir ])
441
448
442
449
443
- def package_versions_table (app : sphinx .application .Sphinx ):
444
- """Generate the package_versions directory."""
445
- branches , versions = get_release_branches_in_metapackage ()
446
- generate_rst_files (
447
- versions ,
448
- {version : build_versions_table (branch ) for version , branch in zip (versions , branches )},
449
- )
450
-
451
-
452
450
def convert_yaml_to_json (app : sphinx .application .Sphinx ):
453
451
"""
454
452
Convert a YAML file to a JSON file.
@@ -475,6 +473,52 @@ def convert_yaml_to_json(app: sphinx.application.Sphinx):
475
473
print (f"JSON file successfully written to { json_path } " )
476
474
477
475
476
+ def fetch_release_branches_and_python_limits (app : sphinx .application .Sphinx ):
477
+ """Retrieve the release branches in the PyAnsys metapackage."""
478
+ # Get the PyAnsys metapackage repository
479
+ g = github .Github (os .getenv ("GITHUB_TOKEN" , None ))
480
+ repository = g .get_repo ("ansys/pyansys" )
481
+
482
+ # Get the branches
483
+ branches = repository .get_branches ()
484
+
485
+ supported_python_versions_by_metapackage_version = []
486
+ for branch in branches :
487
+ if not branch .name .startswith ("release" ) and not branch .name .startswith ("main" ):
488
+ continue
489
+
490
+ # Inspect the pyproject.toml file to get the Python limits
491
+ pyproject = repository .get_contents ("pyproject.toml" , ref = branch .name )
492
+ content = toml .loads (pyproject .decoded_content .decode ("utf-8" ))
493
+
494
+ # Extract the latest version and the Python limits
495
+ branch_name = branch .name
496
+ metapackage_version = branch_name .split ("/" )[- 1 ]
497
+ try :
498
+ pypi_version = content ["project" ]["version" ]
499
+ python_limits = content ["project" ]["requires-python" ].split ("," )
500
+ except KeyError :
501
+ pypi_version = content ["tool" ]["poetry" ]["version" ]
502
+ python_limits = content ["tool" ]["poetry" ]["dependencies" ]["python" ].split ("," )
503
+
504
+ if pypi_version .split ("." )[- 1 ].startswith ("dev" ):
505
+ link = repository .html_url
506
+ else :
507
+ link = f"https://pypi.org/project/pyansys/{ pypi_version } "
508
+
509
+ supported_python_versions_by_metapackage_version .append (
510
+ {
511
+ "version" : metapackage_version ,
512
+ "python" : {"lower" : python_limits [0 ], "upper" : python_limits [1 ]},
513
+ "link" : link ,
514
+ }
515
+ )
516
+
517
+ print (supported_python_versions_by_metapackage_version )
518
+
519
+ jinja_contexts ["releases" ] = {"table_data" : supported_python_versions_by_metapackage_version }
520
+
521
+
478
522
def setup (app : sphinx .application .Sphinx ):
479
523
"""Run different hook functions during the documentation build.
480
524
@@ -486,7 +530,7 @@ def setup(app: sphinx.application.Sphinx):
486
530
# At the beginning of the build process - update the version in cheatsheet
487
531
app .connect ("builder-inited" , convert_yaml_to_json )
488
532
app .connect ("builder-inited" , resize_thumbnails )
489
- app .connect ("builder-inited" , package_versions_table )
533
+ app .connect ("builder-inited" , fetch_release_branches_and_python_limits )
490
534
491
535
# Reverting the thumbnails - no local changes
492
536
app .connect ("build-finished" , revert_thumbnails )
0 commit comments