11#!/usr/bin/env python3
2- """Download auto_examples folder for document except Exception as e:
3- if requests and hasattr(e, 'r except Exception as e:
4- if requests and hasattr(e, 'response') and e.response is not None:
5- if e.response.status_code == 404:
6- log_message("No GitHub releases found or no auto_examples assets available")
7- else:
8- log_message(f"Could not check GitHub releases: HTTP {e.response.status_code}")
9- elif isinstance(e, urllib.error.HTTPError): # urllib error
10- if e.code == 404:
11- log_message("No GitHub releases found or no auto_examples assets available")
12- else:
13- log_message(f"Could not check GitHub releases: HTTP {e.code} - {e.reason}") # type: ignore[attr-defined]
14- else:
15- log_message(f"Could not check GitHub releases: {e}") e.response is not None:
16- if e.response.status_code == 404:
17- log_message("No GitHub artifacts found or repository not accessible")
18- elif e.response.status_code == 403:
19- log_message("GitHub artifacts require authentication (GITHUB_TOKEN)")
20- else:
21- log_message(f"Could not check GitHub artifacts: HTTP {e.response.status_code}")
22- elif isinstance(e, urllib.error.HTTPError): # urllib error
23- if e.code == 404:
24- log_message("No GitHub artifacts found or repository not accessible")
25- elif e.code == 403:
26- log_message("GitHub artifacts require authentication (GITHUB_TOKEN)")
27- else:
28- log_message(f"Could not check GitHub artifacts: HTTP {e.code} - {e.reason}") # type: ignore[attr-defined]
29- else:
30- log_message(f"Could not check GitHub artifacts: {e}")is script downloads the auto_examples folder from a remote source (such as GitHub releases or
31- artifacts) before documentation builds. It's designed to work both locally and on ReadTheDocs.
2+ """Download auto_examples folder from a remote source (such as GitHub releases or artifacts) before
3+ documentation builds.
4+
5+ It's designed to work both locally and on ReadTheDocs.
326"""
337
34- import configparser
358import json
369import os
3710import shutil
@@ -87,9 +60,17 @@ def check_github_artifacts_for_examples(repo_owner: str, repo_name: str, token:
8760 artifacts_data = json .loads (urllib_response .read ().decode ())
8861
8962 # Look for recent auto_examples artifacts
90- for artifact in artifacts_data .get ("artifacts" , []):
91- if "auto_examples" in artifact ["name" ].lower () and not artifact ["expired" ]:
92- log_message (f"Found auto_examples artifact: { artifact ['name' ]} " )
63+ # Sort by creation date to get the most recent first
64+ artifacts = sorted (
65+ artifacts_data .get ("artifacts" , []),
66+ key = lambda x : x .get ("created_at" , "" ),
67+ reverse = True ,
68+ )
69+
70+ for artifact in artifacts :
71+ name_lower = artifact ["name" ].lower ()
72+ if ("auto_examples" in name_lower or "auto-examples" in name_lower ) and not artifact ["expired" ]:
73+ log_message (f"Found auto_examples artifact: { artifact ['name' ]} (created: { artifact .get ('created_at' , 'unknown' )} )" )
9374 return artifact ["archive_download_url" ]
9475
9576 except Exception as e :
@@ -282,13 +263,9 @@ def generate_placeholder_examples(target_dir: Path) -> None:
282263 log_message ("Placeholder auto_examples created" )
283264
284265
285- def load_config (script_dir : Path ) -> dict [str , bool | int | str ]:
286- """Load configuration from config file."""
287- config_file = script_dir / "auto_examples_config.ini"
288- config = configparser .ConfigParser ()
289-
290- # Set defaults with their expected types
291- defaults = {
266+ def get_default_config () -> dict [str , bool | int | str ]:
267+ """Get default configuration for auto_examples download."""
268+ return {
292269 "github_owner" : "ipc-lab" ,
293270 "github_repo" : "kaira" ,
294271 "use_github_releases" : True ,
@@ -299,44 +276,6 @@ def load_config(script_dir: Path) -> dict[str, bool | int | str]:
299276 "skip_if_exists" : True ,
300277 }
301278
302- if config_file .exists ():
303- config .read (config_file )
304-
305- # Merge with defaults
306- result : dict [str , bool | int | str ] = {}
307- for key , default_value in defaults .items ():
308- if "." in key :
309- section , option = key .split ("." , 1 )
310- else :
311- section = "sources" if key .startswith (("github_" , "use_" )) else "settings"
312- option = key
313-
314- try :
315- if section in config and option in config [section ]:
316- raw_value = config [section ][option ]
317-
318- # Convert based on the type of the default value
319- if isinstance (default_value , bool ):
320- result [key ] = raw_value .lower () in ("true" , "1" , "yes" , "on" )
321- elif isinstance (default_value , int ):
322- result [key ] = int (raw_value )
323- else :
324- result [key ] = str (raw_value )
325- else :
326- # Ensure we assign the correct type based on default_value type
327- if isinstance (default_value , (bool , int , str )):
328- result [key ] = default_value
329- else :
330- result [key ] = str (default_value )
331- except Exception :
332- # Ensure we assign the correct type based on default_value type
333- if isinstance (default_value , (bool , int , str )):
334- result [key ] = default_value
335- else :
336- result [key ] = str (default_value )
337-
338- return result
339-
340279
341280def main ():
342281 """Main function to download auto_examples."""
@@ -350,7 +289,7 @@ def main():
350289 log_message (f"Docs directory: { docs_dir } " )
351290
352291 # Load configuration
353- config = load_config ( script_dir )
292+ config = get_default_config ( )
354293
355294 # Check if auto_examples already exists and is not empty
356295 auto_examples_dir = docs_dir / "auto_examples"
0 commit comments