1
1
import os
2
+ import ast
2
3
import time
3
4
import importlib
4
5
from typing import Optional , Tuple , Dict , Any , List
@@ -174,20 +175,47 @@ def next_version(self) -> str:
174
175
self ._next_version = self .calculate_next_version ()
175
176
return self ._next_version
176
177
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
+
177
209
# Use the template to update readme and setup by packaging_tools
178
210
@return_origin_path
179
211
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
186
215
187
216
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 } " )
189
218
190
- os .chdir (Path (f"sdk/{ self .sdk_folder } " ))
191
219
# add `title` and update `is_stable` in sdk_packaging.toml
192
220
toml = Path (self .whole_package_name ) / "sdk_packaging.toml"
193
221
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):
338
366
os .chdir (Path ("sdk" ) / self .sdk_folder / self .whole_package_name )
339
367
# Configure and ensure pyproject.toml exists with required settings
340
368
toml_path = Path ("pyproject.toml" )
341
-
369
+
342
370
# Default configurations to enforce
343
371
default_configs = {
344
372
"breaking" : "false" ,
345
373
"pyright" : "false" ,
346
374
"mypy" : "false" ,
347
375
}
348
-
376
+
349
377
# Create new pyproject.toml if it doesn't exist
350
378
if not toml_path .exists ():
351
379
with open (toml_path , "w" ) as file :
@@ -354,27 +382,27 @@ def check_pyproject_toml(self):
354
382
file .write (f"{ key } = { value } \n " )
355
383
_LOGGER .info ("Created pyproject.toml with default configurations" )
356
384
return
357
-
385
+
358
386
# If file exists, ensure all required configurations are present
359
387
def edit_toml (content : List [str ]):
360
388
# Track if we have the [tool.azure-sdk-build] section
361
389
has_section = False
362
390
config_exists = {key : False for key in default_configs }
363
-
391
+
364
392
# Check for existing configurations and section
365
393
for i , line in enumerate (content ):
366
394
if "[tool.azure-sdk-build]" in line :
367
395
has_section = True
368
-
396
+
369
397
# Check for each configuration
370
398
for key in default_configs :
371
399
if f"{ key } = " in line :
372
400
config_exists [key ] = True
373
-
401
+
374
402
# Add section if it doesn't exist
375
403
if not has_section :
376
404
content .append ("\n [tool.azure-sdk-build]\n " )
377
-
405
+
378
406
# Add missing configurations
379
407
for key , value in default_configs .items ():
380
408
if not config_exists [key ]:
0 commit comments