|
26 | 26 | from datetime import datetime |
27 | 27 | from pathlib import Path |
28 | 28 | from typing import Dict, List |
| 29 | +from distutils.core import run_setup |
| 30 | + |
| 31 | +import tomli |
29 | 32 |
|
30 | 33 | try: |
31 | 34 | import synthtool |
@@ -546,12 +549,48 @@ def _verify_library_namespace(library_id: str, repo: str): |
546 | 549 | ) |
547 | 550 |
|
548 | 551 |
|
| 552 | +def _get_setup_dist_name(library_id: str, repo: str): |
| 553 | + try: |
| 554 | + dist = run_setup(f"{repo}/packages/{library_id}/setup.py") |
| 555 | + return dist.get_name() |
| 556 | + except Exception as e: |
| 557 | + return None |
| 558 | + |
| 559 | + |
| 560 | +def _get_toml_dist_name(library_id: str, repo: str): |
| 561 | + try: |
| 562 | + pyproject_toml_file = Path(f"{repo}/packages/{library_id}/pyproject.toml") |
| 563 | + with open(pyproject_toml_file, "rb") as f: |
| 564 | + data = tomli.load(f) |
| 565 | + return data.get("project", {}).get("name") |
| 566 | + except Exception as e: |
| 567 | + return None |
| 568 | + |
| 569 | + |
| 570 | +def _verify_library_dist_name(library_id: str, repo: str): |
| 571 | + setup_dist_name = _get_setup_dist_name(library_id, repo) |
| 572 | + toml_dist_name = _get_toml_dist_name(library_id, repo) |
| 573 | + if setup_dist_name is None and toml_dist_name is None: |
| 574 | + raise ValueError( |
| 575 | + f"No valid `setup.py` or `pyproject.toml found for `{library_id}`." |
| 576 | + ) |
| 577 | + if setup_dist_name is not None and setup_dist_name != library_id: |
| 578 | + raise ValueError( |
| 579 | + f"The distribution name `{setup_dist_name} in `setup.py` does not match folder `{library_id}`" |
| 580 | + ) |
| 581 | + if toml_dist_name is not None and toml_dist_name != library_id: |
| 582 | + raise ValueError( |
| 583 | + f"The distribution name `{toml_dist_name} in `pyproject.toml` does not match folder `{library_id}`" |
| 584 | + ) |
| 585 | + |
| 586 | + |
549 | 587 | def handle_build(librarian: str = LIBRARIAN_DIR, repo: str = REPO_DIR): |
550 | 588 | """The main coordinator for validating client library generation.""" |
551 | 589 | try: |
552 | 590 | request_data = _read_json_file(f"{librarian}/{BUILD_REQUEST_FILE}") |
553 | 591 | library_id = _get_library_id(request_data) |
554 | 592 | _verify_library_namespace(library_id, repo) |
| 593 | + _verify_library_dist_name(library_id, repo) |
555 | 594 | _run_nox_sessions(library_id, repo) |
556 | 595 | except Exception as e: |
557 | 596 | raise ValueError("Build failed.") from e |
|
0 commit comments