11import os
2+ import ast
23import time
34import importlib
45from typing import Optional , Tuple , Dict , Any , List
@@ -174,20 +175,47 @@ def next_version(self) -> str:
174175 self ._next_version = self .calculate_next_version ()
175176 return self ._next_version
176177
178+ @property
179+ def extract_client_title_from_init (self ) -> str :
180+ """
181+ Extract the client title from a package's __init__.py file.
182+
183+ Args:
184+ package_name (str): The package name (e.g., "azure-mgmt-compute")
185+
186+ Returns:
187+ str: The client title if found, empty string otherwise
188+ """
189+ init_file = Path (self .whole_package_name ) / self .whole_package_name .replace ("-" , "/" ) / "__init__.py"
190+ try :
191+ with open (init_file , "r" ) as f :
192+ content = f .read ()
193+ tree = ast .parse (content )
194+ # Look for __all__ assignment
195+ for node in ast .walk (tree ):
196+ if isinstance (node , ast .Assign ):
197+ # Check if any target is __all__
198+ for target in node .targets :
199+ if isinstance (target , ast .Name ) and target .id == "__all__" and isinstance (node .value , ast .List ):
200+ # Extract the value
201+ for elt in node .value .elts :
202+ if isinstance (elt , ast .Constant ) and elt .value .endswith ("Client" ):
203+ return elt .value
204+ except Exception as e :
205+ _LOGGER .info (f"Failed to extract title from { init_file } : { e } " )
206+
207+ return ""
208+
177209 # Use the template to update readme and setup by packaging_tools
178210 @return_origin_path
179211 def check_file_with_packaging_tool (self ):
180- module = importlib .import_module (self .whole_package_name .replace ("-" , "." ))
181- title = ""
182- for item in getattr (module , "__all__" , []):
183- if item .endswith ("Client" ):
184- title = item
185- break
212+
213+ os .chdir (Path (f"sdk/{ self .sdk_folder } " ))
214+ title = self .extract_client_title_from_init
186215
187216 if not title :
188- _LOGGER .info (f"Can not find the title in { self .whole_package_name } " )
217+ _LOGGER .info (f"Can not find the title for { self .whole_package_name } " )
189218
190- os .chdir (Path (f"sdk/{ self .sdk_folder } " ))
191219 # add `title` and update `is_stable` in sdk_packaging.toml
192220 toml = Path (self .whole_package_name ) / "sdk_packaging.toml"
193221 stable_config = f'is_stable = { "true" if self .tag_is_stable and self .next_version != "1.0.0b1" else "false" } \n '
@@ -338,14 +366,14 @@ def check_pyproject_toml(self):
338366 os .chdir (Path ("sdk" ) / self .sdk_folder / self .whole_package_name )
339367 # Configure and ensure pyproject.toml exists with required settings
340368 toml_path = Path ("pyproject.toml" )
341-
369+
342370 # Default configurations to enforce
343371 default_configs = {
344372 "breaking" : "false" ,
345373 "pyright" : "false" ,
346374 "mypy" : "false" ,
347375 }
348-
376+
349377 # Create new pyproject.toml if it doesn't exist
350378 if not toml_path .exists ():
351379 with open (toml_path , "w" ) as file :
@@ -354,27 +382,27 @@ def check_pyproject_toml(self):
354382 file .write (f"{ key } = { value } \n " )
355383 _LOGGER .info ("Created pyproject.toml with default configurations" )
356384 return
357-
385+
358386 # If file exists, ensure all required configurations are present
359387 def edit_toml (content : List [str ]):
360388 # Track if we have the [tool.azure-sdk-build] section
361389 has_section = False
362390 config_exists = {key : False for key in default_configs }
363-
391+
364392 # Check for existing configurations and section
365393 for i , line in enumerate (content ):
366394 if "[tool.azure-sdk-build]" in line :
367395 has_section = True
368-
396+
369397 # Check for each configuration
370398 for key in default_configs :
371399 if f"{ key } = " in line :
372400 config_exists [key ] = True
373-
401+
374402 # Add section if it doesn't exist
375403 if not has_section :
376404 content .append ("\n [tool.azure-sdk-build]\n " )
377-
405+
378406 # Add missing configurations
379407 for key , value in default_configs .items ():
380408 if not config_exists [key ]:
0 commit comments