Use lib/ts_utils for scripts/sync_protobuf (#12913)

- Simplified `download_file` error handling (400+ return codes already raised errors!)
- Moved `update_metadata` from `scripts/sync_protobuf/_utils.py` to `lib/ts_utils/metadata.py`
- Improved `update_metadata` to support any key (values unvalidated atm) and return the modified dictionary
- Updated `scripts/stubsabot.py` to use `update_metadata`
- Updated `scripts/sync_protobuf/*` to use  `lib/ts_utils`
- Updated `scripts/sync_protobuf/tensorflow.py` and `scripts/sync_protobuf/google_protobuf.py` to use the version directly from the `METADATA.toml` file
This commit is contained in:
Avasam
2024-10-28 06:47:52 -04:00
committed by GitHub
parent c225ac7587
commit bff5547abc
9 changed files with 54 additions and 60 deletions

View File

@@ -14,6 +14,7 @@ from typing import Final, NamedTuple, final
from typing_extensions import Annotated, TypeGuard
import tomli
import tomlkit
from packaging.requirements import Requirement
from packaging.specifiers import Specifier
@@ -300,6 +301,22 @@ def read_metadata(distribution: str) -> StubMetadata:
)
def update_metadata(distribution: str, **new_values: object) -> tomlkit.TOMLDocument:
"""Updates a distribution's METADATA.toml.
Return the updated TOML dictionary for use without having to open the file separately."""
path = metadata_path(distribution)
try:
with path.open("rb") as file:
data = tomlkit.load(file)
except FileNotFoundError:
raise NoSuchStubError(f"Typeshed has no stubs for {distribution!r}!") from None
data.update(new_values) # pyright: ignore[reportUnknownMemberType] # tomlkit.TOMLDocument.update is partially typed
with path.open("w", encoding="UTF-8") as file:
tomlkit.dump(data, file) # pyright: ignore[reportUnknownMemberType] # tomlkit.dump has partially unknown Mapping type
return data
def parse_requires(distribution: str, req: object) -> Requirement:
assert isinstance(req, str), f"Invalid requirement {req!r} for {distribution!r}"
return Requirement(req)