Skip to content

Commit b2090ae

Browse files
committed
chore: verify library distribution name
1 parent daf5ab8 commit b2090ae

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

.generator/cli.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
from datetime import datetime
2727
from pathlib import Path
2828
from typing import Dict, List
29+
from distutils.core import run_setup
30+
31+
import tomli
2932

3033
try:
3134
import synthtool
@@ -546,12 +549,48 @@ def _verify_library_namespace(library_id: str, repo: str):
546549
)
547550

548551

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+
549587
def handle_build(librarian: str = LIBRARIAN_DIR, repo: str = REPO_DIR):
550588
"""The main coordinator for validating client library generation."""
551589
try:
552590
request_data = _read_json_file(f"{librarian}/{BUILD_REQUEST_FILE}")
553591
library_id = _get_library_id(request_data)
554592
_verify_library_namespace(library_id, repo)
593+
_verify_library_dist_name(library_id, repo)
555594
_run_nox_sessions(library_id, repo)
556595
except Exception as e:
557596
raise ValueError("Build failed.") from e

0 commit comments

Comments
 (0)