Remove var type annotations in extract_metadata_from_bazel_xml (#29463)

* Remove var type annotations in extract_metadata_from_bazel_xml

* Use plain-old Python class to store data

* No more f-string

* Remove version check in generate_projects.py
pull/29482/head
Lidi Zheng 3 years ago committed by GitHub
parent eb2ae7a0cc
commit ec5b3b22c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      tools/buildgen/extract_metadata_from_bazel_xml.py
  2. 4
      tools/buildgen/generate_projects.py

@ -30,16 +30,13 @@
# format entirely or simplify it to a point where it becomes self-explanatory
# and doesn't need any detailed documentation.
from dataclasses import asdict
from dataclasses import dataclass
from dataclasses import field
import collections
import os
import subprocess
from typing import Any, Dict, Iterable, List, Optional
import xml.etree.ElementTree as ET
import build_cleaner
import yaml
BuildMetadata = Dict[str, Any]
BuildDict = Dict[str, BuildMetadata]
@ -50,19 +47,36 @@ BuildDict = Dict[str, BuildMetadata]
BuildYaml = Dict[str, Any]
# This is basically a Python dict with predefined fields and types
@dataclass()
class ExternalProtoLibrary:
# The relative path of this proto library should be. Preferably, it should
# match the submodule path.
destination: str
# The prefix to remove in order to insure the proto import is correct. For
# more info, see description of https://github.com/grpc/grpc/pull/25272.
proto_prefix: str
# Following 3 fields should be filled by build metadata from Bazel.
urls: List[str] = field(default_factory=list)
hash: str = ''
strip_prefix: str = ''
"""ExternalProtoLibrary is the struct about an external proto library.
Fields:
- destination(int): The relative path of this proto library should be.
Preferably, it should match the submodule path.
- proto_prefix(str): The prefix to remove in order to insure the proto import
is correct. For more info, see description of
https://github.com/grpc/grpc/pull/25272.
- urls(List[str]): Following 3 fields should be filled by build metadata from
Bazel.
- hash(str): The hash of the downloaded archive
- strip_prefix(str): The path to be stripped from the extracted directory, see
http_archive in Bazel.
"""
def __init__(self,
destination,
proto_prefix,
urls=None,
hash="",
strip_prefix=""):
self.destination = destination
self.proto_prefix = proto_prefix
if urls is None:
self.urls = []
else:
self.urls = urls
self.hash = hash
self.strip_prefix = strip_prefix
EXTERNAL_PROTO_LIBRARIES = {
@ -208,7 +222,7 @@ def _extract_sources(bazel_rule: BuildMetadata) -> List[str]:
if external_proto_library_name is not None:
result.append(
src.replace(
f'@{external_proto_library_name}//',
'@%s//' % external_proto_library_name,
EXTERNAL_PROTO_LIBRARIES[
external_proto_library_name].proto_prefix).
replace(':', '/'))
@ -790,7 +804,7 @@ def _generate_build_extra_metadata_for_tests(
return test_metadata
def _parse_http_archives(xml_tree: ET.Element) -> List[ExternalProtoLibrary]:
def _parse_http_archives(xml_tree: ET.Element) -> 'List[ExternalProtoLibrary]':
"""Parse Bazel http_archive rule into ExternalProtoLibrary objects."""
result = []
for xml_http_archive in xml_tree:
@ -829,7 +843,7 @@ def _generate_external_proto_libraries() -> List[Dict[str, Any]]:
xml_tree = _bazel_query_xml_tree('kind(http_archive, //external:*)')
libraries = _parse_http_archives(xml_tree)
libraries.sort(key=lambda x: x.destination)
return list(map(asdict, libraries))
return list(map(lambda x: x.__dict__, libraries))
def _detect_and_print_issues(build_yaml_like: BuildYaml) -> None:

@ -25,10 +25,6 @@ from typing import Dict, List, Union
import _utils
import yaml
if sys.version_info < (3, 6):
raise RuntimeError('Expected Python version > 3.6, but got %s at %s' %
(sys.version_info, sys.executable))
PROJECT_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..",
"..")
os.chdir(PROJECT_ROOT)

Loading…
Cancel
Save