Skip to content

Commit fe57ef1

Browse files
authored
[sdk generation pipeline] find client name in __all__ from __init__.py (#41687)
* find client in __all__ of __init__.py * update * update * update log
1 parent 9a5ad7c commit fe57ef1

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

tools/azure-sdk-tools/packaging_tools/package_utils.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import ast
23
import time
34
import importlib
45
from 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]:

tools/azure-sdk-tools/packaging_tools/sdk_package.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def main(generate_input, generate_output):
5151
try:
5252
md_output = execute_func_with_timeout(change_log_func)
5353
except multiprocessing.TimeoutError:
54-
md_output = "change log generation was timeout!!!"
54+
md_output = "change log generation was timeout!!! You need to write it manually!!!"
5555
except:
56-
md_output = "change log generation failed!!!"
56+
md_output = "change log generation failed!!! You need to write it manually!!!"
5757
finally:
5858
for file in ["stable.json", "current.json"]:
5959
file_path = Path(sdk_folder, prefolder, package_name, file)

0 commit comments

Comments
 (0)