|
2 | 2 | import ast |
3 | 3 | import time |
4 | 4 | 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 |
5 | 13 | from typing import Optional, Tuple, Dict, Any, List |
6 | 14 | from pathlib import Path |
7 | 15 | import logging |
@@ -223,28 +231,21 @@ def check_file_with_packaging_tool(self): |
223 | 231 | if not title: |
224 | 232 | _LOGGER.info(f"Can not find the title for {self.whole_package_name}") |
225 | 233 |
|
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") |
246 | 247 | else: |
247 | | - _LOGGER.info(f"{os.getcwd()}/{toml} does not exist") |
| 248 | + _LOGGER.info(f"{os.getcwd()}/{pyproject_toml} does not exist") |
248 | 249 |
|
249 | 250 | build_packaging(output_folder=".", packages=[self.whole_package_name], build_conf=True) |
250 | 251 | _LOGGER.info("packaging_tools --build-conf successfully") |
@@ -381,42 +382,44 @@ def check_pyproject_toml(self): |
381 | 382 | "mypy": "false", |
382 | 383 | } |
383 | 384 |
|
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") |
420 | 423 |
|
421 | 424 | def run(self): |
422 | 425 | self.check_file_with_packaging_tool() |
|
0 commit comments