Skip to content

Commit 78fac7e

Browse files
msyycCopilot
andauthored
[sdk generation pipeline] merge "sdk_packaging.toml" into "pyproject.toml" (#42300)
* replace sdk_packaging.toml with pyproject.toml * update * update * update * update * update * update * Update tools/azure-sdk-tools/packaging_tools/package_utils.py Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 29d2628 commit 78fac7e

File tree

5 files changed

+111
-66
lines changed

5 files changed

+111
-66
lines changed

scripts/auto_release/requirement.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ fastcore==1.3.25
88
tox==4.15.0
99
wheel==0.43.0
1010
setuptools==78.1.0
11-
build==1.0.3
11+
build==1.0.3
12+
tomli-w==1.0.0
13+
tomli==2.2.1

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
_LOGGER = logging.getLogger(__name__)
1414

15-
CONF_NAME = "sdk_packaging.toml"
15+
CONF_NAME = "pyproject.toml"
16+
OLD_CONF_NAME = "sdk_packaging.toml"
1617
_SECTION = "packaging"
1718

1819
# Default conf

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
import logging
33
import os
44
import re
5+
try:
6+
# py 311 adds this library natively
7+
import tomllib as toml
8+
except:
9+
# otherwise fall back to pypi package tomli
10+
import tomli as toml
11+
import tomli_w as tomlw
512
from functools import wraps
613
from typing import Optional
714

@@ -16,7 +23,7 @@
1623
from . import build_packaging
1724
from .swaggertosdk.autorest_tools import build_autorest_options, generate_code
1825
from .swaggertosdk.SwaggerToSdkCore import CONFIG_FILE_DPG, read_config
19-
from .conf import CONF_NAME
26+
from .conf import CONF_NAME, OLD_CONF_NAME
2027
from jinja2 import Environment, FileSystemLoader
2128

2229

@@ -113,11 +120,43 @@ def call_build_config(package_name: str, folder_name: str):
113120
# )
114121

115122

116-
def init_new_service(package_name, folder_name):
123+
def generate_packaging_files(package_name, folder_name):
124+
# replace sdk_packaging.toml with pyproject.toml
125+
output_path = Path(folder_name) / package_name
126+
pyproject_toml = output_path / CONF_NAME
127+
sdk_packaging_toml = output_path / OLD_CONF_NAME
128+
if sdk_packaging_toml.exists():
129+
if pyproject_toml.exists():
130+
# update related items in pyproject.toml then delete sdk_packaging.toml
131+
_LOGGER.info(f"update {pyproject_toml} with {sdk_packaging_toml}")
132+
133+
# Read the old sdk_packaging.toml content
134+
with open(sdk_packaging_toml, "rb") as f:
135+
sdk_packaging_content = toml.load(f)
136+
137+
# Read the existing pyproject.toml content
138+
with open(pyproject_toml, "rb") as f:
139+
pyproject_content = toml.load(f)
140+
141+
# Update pyproject.toml with sdk_packaging.toml content
142+
pyproject_content.update(sdk_packaging_content)
143+
144+
# Write updated content back to pyproject.toml
145+
with open(pyproject_toml, "wb") as f:
146+
tomlw.dump(pyproject_content, f)
147+
148+
# Delete the old sdk_packaging.toml file
149+
sdk_packaging_toml.unlink()
150+
_LOGGER.info(f"deleted {sdk_packaging_toml}")
151+
152+
else:
153+
# rename sdk_packaging.toml to pyproject.toml
154+
_LOGGER.info(f"rename {sdk_packaging_toml} to {pyproject_toml}")
155+
sdk_packaging_toml.rename(pyproject_toml)
156+
117157
if "azure-mgmt-" in package_name:
118158
call_build_config(package_name, folder_name)
119159
else:
120-
output_path = Path(folder_name) / package_name
121160
if not (output_path / CONF_NAME).exists():
122161
with open(output_path / CONF_NAME, "w") as file_out:
123162
file_out.write("[packaging]\nauto_update = false")

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

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
import ast
33
import time
44
import shutil
5+
6+
try:
7+
# py 311 adds this library natively
8+
import tomllib as toml
9+
except:
10+
# otherwise fall back to pypi package tomli
11+
import tomli as toml
12+
import tomli_w as tomlw
513
from typing import Optional, Tuple, Dict, Any, List
614
from pathlib import Path
715
import logging
@@ -223,28 +231,21 @@ def check_file_with_packaging_tool(self):
223231
if not title:
224232
_LOGGER.info(f"Can not find the title for {self.whole_package_name}")
225233

226-
# add `title` and update `is_stable` in sdk_packaging.toml
227-
toml = Path(self.whole_package_name) / "sdk_packaging.toml"
228-
stable_config = f'is_stable = {"true" if self.tag_is_stable and self.next_version != "1.0.0b1" else "false"}\n'
229-
if toml.exists():
230-
231-
def edit_toml(content: List[str]):
232-
has_title = False
233-
has_isstable = False
234-
for idx in range(len(content)):
235-
if "title" in content[idx]:
236-
has_title = True
237-
if "is_stable" in content[idx]:
238-
has_isstable = True
239-
content[idx] = stable_config
240-
if not has_title:
241-
content.append(f'title = "{title}"\n')
242-
if not has_isstable:
243-
content.append(stable_config)
244-
245-
modify_file(str(toml), edit_toml)
234+
# add `title` and update `is_stable` in pyproject.toml
235+
pyproject_toml = Path(self.whole_package_name) / "pyproject.toml"
236+
if pyproject_toml.exists():
237+
with open(pyproject_toml, "rb") as fd:
238+
toml_data = toml.load(fd)
239+
if "packaging" not in toml_data:
240+
toml_data["packaging"] = {}
241+
if title and not toml_data["packaging"].get("title"):
242+
toml_data["packaging"]["title"] = title
243+
toml_data["packaging"]["is_stable"] = self.tag_is_stable and self.next_version != "1.0.0b1"
244+
with open(pyproject_toml, "wb") as fd:
245+
tomlw.dump(toml_data, fd)
246+
_LOGGER.info(f"Update {pyproject_toml} successfully")
246247
else:
247-
_LOGGER.info(f"{os.getcwd()}/{toml} does not exist")
248+
_LOGGER.info(f"{os.getcwd()}/{pyproject_toml} does not exist")
248249

249250
build_packaging(output_folder=".", packages=[self.whole_package_name], build_conf=True)
250251
_LOGGER.info("packaging_tools --build-conf successfully")
@@ -381,42 +382,44 @@ def check_pyproject_toml(self):
381382
"mypy": "false",
382383
}
383384

384-
# Create new pyproject.toml if it doesn't exist
385-
if not toml_path.exists():
386-
with open(toml_path, "w") as file:
387-
file.write("[tool.azure-sdk-build]\n")
388-
for key, value in default_configs.items():
389-
file.write(f"{key} = {value}\n")
390-
_LOGGER.info("Created pyproject.toml with default configurations")
391-
return
392-
393-
# If file exists, ensure all required configurations are present
394-
def edit_toml(content: List[str]):
395-
# Track if we have the [tool.azure-sdk-build] section
396-
has_section = False
397-
config_exists = {key: False for key in default_configs}
398-
399-
# Check for existing configurations and section
400-
for i, line in enumerate(content):
401-
if "[tool.azure-sdk-build]" in line:
402-
has_section = True
403-
404-
# Check for each configuration
405-
for key in default_configs:
406-
if f"{key} = " in line:
407-
config_exists[key] = True
408-
409-
# Add section if it doesn't exist
410-
if not has_section:
411-
content.append("\n[tool.azure-sdk-build]\n")
412-
413-
# Add missing configurations
414-
for key, value in default_configs.items():
415-
if not config_exists[key]:
416-
_LOGGER.info(f"Adding {key} = {value} to pyproject.toml")
417-
content.append(f"{key} = {value}\n")
418-
419-
modify_file(str(toml_path), edit_toml)
385+
# Load existing TOML or create new structure
386+
if toml_path.exists():
387+
try:
388+
with open(toml_path, "rb") as file:
389+
toml_data = toml.load(file)
390+
except Exception as e:
391+
_LOGGER.warning(f"Error parsing pyproject.toml: {e}, creating new one")
392+
toml_data = {}
393+
else:
394+
toml_data = {}
395+
396+
# Ensure [tool.azure-sdk-build] section exists
397+
if "tool" not in toml_data:
398+
toml_data["tool"] = {}
399+
400+
if "azure-sdk-build" not in toml_data["tool"]:
401+
toml_data["tool"]["azure-sdk-build"] = {}
402+
403+
# Update configurations
404+
azure_sdk_build = toml_data["tool"]["azure-sdk-build"]
405+
406+
for key, value in default_configs.items():
407+
if key not in azure_sdk_build:
408+
_LOGGER.info(f"Adding {key} = {value} to pyproject.toml")
409+
if isinstance(value, str):
410+
if value.lower() == "true":
411+
azure_sdk_build[key] = True
412+
elif value.lower() == "false":
413+
azure_sdk_build[key] = False
414+
else:
415+
azure_sdk_build[key] = value
416+
else:
417+
azure_sdk_build[key] = value
418+
# Write back to file
419+
with open(toml_path, "wb") as file:
420+
tomlw.dump(toml_data, file)
421+
422+
_LOGGER.info("Updated pyproject.toml with required azure-sdk-build configurations")
420423

421424
def run(self):
422425
self.check_file_with_packaging_tool()

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .generate_sdk import generate
2727
from .generate_utils import (
2828
get_package_names,
29-
init_new_service,
29+
generate_packaging_files,
3030
update_servicemetadata,
3131
judge_tag_preview,
3232
format_samples_and_tests,
@@ -309,11 +309,11 @@ def main(generate_input, generate_output):
309309
_LOGGER.error(f"Fail to process package {package_name} in {readme_or_tsp}: {str(e)}")
310310
continue
311311

312-
# Generate some necessary file for new service
312+
# Generate packaging files
313313
try:
314-
init_new_service(package_name, folder_name)
314+
generate_packaging_files(package_name, folder_name)
315315
except Exception as e:
316-
_LOGGER.warning(f"Fail to init new service {package_name} in {readme_or_tsp}: {str(e)}")
316+
_LOGGER.warning(f"Fail to generate packaging files for {package_name} in {readme_or_tsp}: {str(e)}")
317317

318318
# format samples and tests
319319
try:

0 commit comments

Comments
 (0)