Skip to content

Commit 7e7b4fd

Browse files
gnossencopybara-github
authored andcommitted
Fix breakage when built against protobuf head (grpc#34945)
This PR is required in order to upgrade to the `master` branch version of protobuf. To do this, we upgrade to the latest version of `rules_python` by: - Adding in explicit dependencies on all PyPi Bazel test dependencies - Resolving the circular dependency this creates for `xds-protos` by giving it a Bazel build - Generating the Python code for `xds-protos` as part of `generate-projects.sh` since doing so directly in Bazel would be highly fragily, dependent on many other projects' Bazel builds Closes grpc#34945 PiperOrigin-RevId: 599875020
1 parent 24f8963 commit 7e7b4fd

File tree

1,538 files changed

+34473
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,538 files changed

+34473
-146
lines changed

WORKSPACE

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,27 @@ local_repository(
5959
path = "third_party/utf8_range",
6060
)
6161

62-
load("@io_bazel_rules_python//python:pip.bzl", "pip_install")
62+
load("@rules_python//python:pip.bzl", "pip_parse")
6363

64-
pip_install(
64+
pip_parse(
6565
name = "grpc_python_dependencies",
66-
requirements = "@com_github_grpc_grpc//:requirements.bazel.txt",
66+
requirements_lock = "@com_github_grpc_grpc//:requirements.bazel.txt",
6767
)
6868

69+
load("@grpc_python_dependencies//:requirements.bzl", "install_deps")
70+
71+
install_deps()
72+
6973
load("@com_google_protobuf//bazel:system_python.bzl", "system_python")
7074

7175
system_python(
7276
name = "system_python",
7377
minimum_python_version = "3.7",
7478
)
7579

76-
load("@system_python//:pip.bzl", "pip_parse")
80+
load("@system_python//:pip.bzl", system_pip_parse = "pip_parse")
7781

78-
pip_parse(
82+
system_pip_parse(
7983
name = "pip_deps",
8084
requirements = "@com_google_protobuf//python:requirements.txt",
8185
requirements_overrides = {

bazel/grpc_extra_deps.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
2222
load("@envoy_api//bazel:repositories.bzl", "api_dependencies")
2323
load("@google_cloud_cpp//bazel:google_cloud_cpp_deps.bzl", "google_cloud_cpp_deps")
2424
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
25+
load("@rules_python//python:repositories.bzl", "py_repositories")
2526

2627
def grpc_extra_deps(ignore_version_differences = False):
2728
"""Loads the extra dependencies.
@@ -71,3 +72,5 @@ def grpc_extra_deps(ignore_version_differences = False):
7172
)
7273

7374
google_cloud_cpp_deps()
75+
76+
py_repositories()

bazel/grpc_python_deps.bzl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ load("@com_github_grpc_grpc//third_party/py:python_configure.bzl", "python_confi
1919
# buildifier: disable=unnamed-macro
2020
def grpc_python_deps():
2121
"""Loads dependencies for gRPC Python."""
22-
if "io_bazel_rules_python" not in native.existing_rules():
22+
if "rules_python" not in native.existing_rules():
2323
http_archive(
24-
name = "io_bazel_rules_python",
25-
url = "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
26-
sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
27-
patches = ["@com_github_grpc_grpc//third_party:rules_python.patch"],
28-
patch_args = ["-p1"],
24+
name = "rules_python",
25+
sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
26+
strip_prefix = "rules_python-0.26.0",
27+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz",
2928
)
3029

3130
python_configure(name = "local_config_python")

black.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extend-exclude = '''
2121
| tools/distrib/python/grpcio_tools/protoc_lib_deps.py
2222
| .*_pb2.py # autogenerated Protocol Buffer files
2323
| .*_pb2_grpc.py # autogenerated Protocol Buffer gRPC files
24+
# AUTO-GENERATED By tools/distrib/python/xds_protos/build.py
25+
| tools/distrib/python/xds_protos/.*
2426
)
2527
'''
2628

@@ -44,6 +46,7 @@ skip_glob = [
4446
"*pb2*.py",
4547
"*pb2*.pyi",
4648
"**/site-packages/**/*",
49+
"tools/distrib/python/xds_protos/*",
4750
]
4851
single_line_exclusions = ["typing"]
4952
force_single_line = true

requirements.bazel.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ zope.event==4.5.0
1515
setuptools==44.1.1
1616
xds-protos==0.0.11
1717
absl-py==1.4.0
18+
google-cloud-trace==1.11.3
19+
opencensus-context==0.1.3
20+
google-cloud-monitoring==2.16.0
21+
google-api-core==2.14.0
22+
proto-plus==1.22.3
23+
google-auth==2.23.4
1824
googleapis-common-protos==1.61.0
25+
cachetools==5.3.2
26+
charset-normalizer==3.3.2
27+
pyasn1==0.5.0
28+
rsa==4.9
29+
greenlet==1.1.3.post0
30+
zope.interface==6.1
1931
opentelemetry-sdk==1.21.0
2032
opentelemetry-api==1.21.0
33+
importlib-metadata==7.0.1
34+
Deprecated==1.2.14
35+
opentelemetry-semantic-conventions==0.43b0
36+
typing-extensions==4.9.0
37+
pyasn1-modules==0.3.0
38+
zipp==3.17.0
39+
wrapt==1.16.0

src/python/grpcio_csds/grpc_csds/BUILD.bazel

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@grpc_python_dependencies//:requirements.bzl", "requirement")
16-
1715
package(default_visibility = ["//visibility:public"])
1816

1917
py_library(
2018
name = "grpc_csds",
2119
srcs = glob(["*.py"]),
2220
imports = ["../"],
2321
deps = [
24-
requirement("xds-protos"),
2522
"//src/python/grpcio/grpc:grpcio",
23+
"//tools/distrib/python/xds_protos",
2624
],
2725
)

third_party/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ exports_files([
1111
"enum34.BUILD",
1212
"futures.BUILD",
1313
"protobuf.patch",
14-
"rules_python.patch",
1514
"protoc-gen-validate.patch",
1615
])

third_party/rules_python.patch

Lines changed: 0 additions & 76 deletions
This file was deleted.

tools/buildgen/generate_projects.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ python3 tools/buildgen/extract_metadata_from_bazel_xml.py
3434

3535
tools/buildgen/build_cleaner.py build_handwritten.yaml
3636

37+
# /usr/local/google/home/rbellevi/dev/tmp/grpc/venv/bin/python3: No module named virtualenv
38+
# Generate xds-protos
39+
[[ -d generate_projects_virtual_environment ]] || python3 -m virtualenv generate_projects_virtual_environment
40+
generate_projects_virtual_environment/bin/pip install --upgrade --ignore-installed grpcio-tools==1.59.0
41+
generate_projects_virtual_environment/bin/python tools/distrib/python/xds_protos/build.py
42+
3743
# check build_autogenerated.yaml is already in its "clean" form
3844
TEST=true tools/buildgen/build_cleaner.py build_autogenerated.yaml
3945

tools/distrib/check_copyright.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ def replace_copyright(license_text, file_text, filename):
296296
or filename.endswith(".upb_minitable.c")
297297
):
298298
continue
299+
# Allow empty __init__.py files for code generated by xds_protos
300+
if filename.startswith("tools/distrib/python/xds_protos") and (
301+
filename.endswith("__init__.py")
302+
or filename.endswith("generated_file_import_test.py")
303+
):
304+
continue
299305
ext = os.path.splitext(filename)[1]
300306
base = os.path.basename(filename)
301307
if filename.startswith(_ENFORCE_CPP_STYLE_COMMENT_PATH_PREFIX) and ext in [

tools/distrib/python/.gitignore

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,2 @@
11
distrib_virtualenv/
2-
__init__.py
3-
generated_file_import_test.py
4-
envoy/
5-
udpa/
6-
validate/
7-
google/
8-
opencensus/
9-
xds/
102
build/
11-
opentelemetry/

tools/distrib/python/grpcio_tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ grpc_root/
66
*.cpp
77
*.egg-info
88
*.so
9+
third_party/

tools/distrib/python/grpcio_tools/protoc_lib_deps.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
'third_party/protobuf/src/google/protobuf/unknown_field_set.cc',
298298
'third_party/protobuf/src/google/protobuf/wire_format.cc',
299299
'third_party/protobuf/src/google/protobuf/wire_format_lite.cc',
300-
'third_party/utf8_range/utf8_validity.cc'
300+
'third_party/protobuf/third_party/utf8_range/utf8_validity.cc'
301301
]
302302

303303
PROTO_FILES=[
@@ -316,7 +316,9 @@
316316
]
317317

318318
CC_INCLUDES=[
319-
'third_party/abseil-cpp', 'third_party/protobuf/src', 'third_party/utf8_range'
319+
'third_party/abseil-cpp',
320+
'third_party/protobuf/src',
321+
'third_party/protobuf/third_party/utf8_range'
320322
]
321323
PROTO_INCLUDE='third_party/protobuf/src'
322324

tools/distrib/python/make_grpcio_tools.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
EXTERNAL_LINKS = [
6464
("@com_google_absl//", "third_party/abseil-cpp/"),
6565
("@com_google_protobuf//", "third_party/protobuf/"),
66-
("@utf8_range//:", "third_party/utf8_range/"),
66+
("@utf8_range//", "third_party/protobuf/third_party/utf8_range"),
6767
]
6868

6969
PROTOBUF_PROTO_PREFIX = "@com_google_protobuf//src/"
@@ -72,7 +72,7 @@
7272
CC_INCLUDES = [
7373
os.path.join("third_party", "abseil-cpp"),
7474
os.path.join("third_party", "protobuf", "src"),
75-
os.path.join("third_party", "utf8_range"),
75+
os.path.join("third_party", "protobuf", "third_party", "utf8_range"),
7676
]
7777

7878
# include path for .proto files
@@ -88,7 +88,10 @@
8888
("src/compiler", "grpc_root/src/compiler"),
8989
("third_party/abseil-cpp/absl", "third_party/abseil-cpp/absl"),
9090
("third_party/protobuf/src", "third_party/protobuf/src"),
91-
("third_party/utf8_range", "third_party/utf8_range"),
91+
(
92+
"third_party/protobuf/third_party/utf8_range",
93+
"third_party/protobuf/third_party/utf8_range",
94+
),
9295
]
9396

9497
DELETE_TARGETS_ON_CLEANUP = ["third_party"]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2024 gRPC authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
package(default_visibility = ["//src/python/grpcio_csds:__subpackages__"])
16+
17+
_XDS_PROTO_DIRECTORIES = [
18+
"contrib",
19+
"envoy",
20+
"google",
21+
"opencensus",
22+
"opentelemetry",
23+
"udpa",
24+
"validate",
25+
"xds",
26+
]
27+
28+
py_library(
29+
name = "xds_protos",
30+
srcs = (["__init__.py"] +
31+
glob([d + "/**/*.py" for d in _XDS_PROTO_DIRECTORIES])),
32+
imports = ["."],
33+
deps = [
34+
"//src/python/grpcio/grpc:grpcio",
35+
"@com_google_protobuf//:protobuf_python",
36+
],
37+
)

tools/distrib/python/xds_protos/__init__.py

Whitespace-only changes.

tools/distrib/python/xds_protos/build.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def has_grpc_service(proto_package_path: str) -> bool:
105105

106106

107107
def compile_protos(proto_root: str, sub_dir: str = ".") -> None:
108+
compiled_any = False
108109
for root, _, files in os.walk(os.path.join(proto_root, sub_dir)):
109110
proto_package_path = os.path.relpath(root, proto_root)
110111
if proto_package_path in EXCLUDE_PROTO_PACKAGES_LIST:
@@ -113,6 +114,7 @@ def compile_protos(proto_root: str, sub_dir: str = ".") -> None:
113114
for file_name in files:
114115
if file_name.endswith(".proto"):
115116
# Compile proto
117+
compiled_any = True
116118
if has_grpc_service(proto_package_path):
117119
return_code = protoc.main(
118120
COMPILE_BOTH + [os.path.join(root, file_name)]
@@ -127,6 +129,10 @@ def compile_protos(proto_root: str, sub_dir: str = ".") -> None:
127129
)
128130
if return_code != 0:
129131
raise Exception("error: {} failed".format(COMPILE_BOTH))
132+
# Ensure a deterministic order.
133+
TEST_IMPORTS.sort()
134+
if not compiled_any:
135+
raise Exception("No proto files found at {}. Did you update git submodules?".format(proto_root, sub_dir))
130136

131137

132138
def create_init_file(path: str, package_path: str = "") -> None:

0 commit comments

Comments
 (0)