Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0d94dba
Changes to reproduce
XuanWang-Amos Sep 4, 2024
aaebd2d
Test Protobuf py_proto_library
XuanWang-Amos Oct 8, 2024
1e65d48
test bazel 6.5.0
XuanWang-Amos Oct 8, 2024
b11bb25
Use protobuf py_proto_library
XuanWang-Amos Oct 8, 2024
afb211e
Test fixes
XuanWang-Amos Oct 18, 2024
8bc9d0b
Fix more issues
XuanWang-Amos Oct 18, 2024
5378960
Replace our rule to Protobufs
XuanWang-Amos Oct 28, 2024
dafae49
Enable all tests
XuanWang-Amos Oct 28, 2024
45eedaa
Rey remove imports from py_proto_library
XuanWang-Amos Oct 29, 2024
4ab9929
Try add py_proto_library.patch
XuanWang-Amos Oct 30, 2024
4ec2c8b
add logs
XuanWang-Amos Nov 12, 2024
5a74bd7
Working draft to use strip_import_prefix
XuanWang-Amos Dec 10, 2024
9cde446
Use proto_library from Protobuf repo
XuanWang-Amos Dec 10, 2024
e31a20e
Add test_env BUILD_SYSTEM
XuanWang-Amos Dec 10, 2024
ff023e8
Remove imports from all py_proto_library
XuanWang-Amos Dec 10, 2024
c763cbd
Fix bug
XuanWang-Amos Dec 12, 2024
bff48e0
Fix missed path
XuanWang-Amos Dec 17, 2024
e5c3b36
Add BUILD_SYSTEM flag to bazel distrib test
XuanWang-Amos Dec 17, 2024
6a77b15
Refactor
XuanWang-Amos Dec 18, 2024
11299a8
Remove logs
XuanWang-Amos Dec 18, 2024
6b9631b
Fix sanity
XuanWang-Amos Dec 18, 2024
8ec286a
Fix sanity
XuanWang-Amos Dec 18, 2024
a8a21bc
Run balck_code
XuanWang-Amos Dec 18, 2024
9f15588
Test move proto file
XuanWang-Amos Feb 20, 2025
e0fbf28
Add health and reflection proto
XuanWang-Amos Feb 20, 2025
aad636f
Automated change: Fix sanity tests
XuanWang-Amos Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 45 additions & 26 deletions bazel/python_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Generates and compiles Python gRPC stubs from proto_library rules."""

load("@com_google_protobuf//bazel:py_proto_library.bzl", protobuf_py_proto_library = "py_proto_library")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("@rules_python//python:py_info.bzl", "PyInfo")
load(
Expand All @@ -28,6 +29,7 @@ load(
"protos_from_context",
)

_VIRTUAL_IMPORTS = "/_virtual_imports/"
_GENERATED_PROTO_FORMAT = "{}_pb2.py"
_GENERATED_PROTO_STUB_FORMAT = "{}_pb2.pyi"
_GENERATED_GRPC_PROTO_FORMAT = "{}_pb2_grpc.py"
Expand Down Expand Up @@ -158,42 +160,44 @@ def _generate_py_impl(context):
out_pyinfo,
]

py_proto_library = rule(
attrs = {
"deps": attr.label_list(
mandatory = True,
allow_empty = False,
providers = [ProtoInfo],
aspects = [_gen_py_aspect],
),
"_protoc": attr.label(
default = Label("@com_google_protobuf//:protoc"),
providers = ["files_to_run"],
executable = True,
cfg = "exec",
),
"_protobuf_library": attr.label(
default = Label("@com_google_protobuf//:protobuf_python"),
providers = [PyInfo],
),
"imports": attr.string_list(),
},
implementation = _generate_py_impl,
)
# py_proto_library = rule(
# attrs = {
# "deps": attr.label_list(
# mandatory = True,
# allow_empty = False,
# providers = [ProtoInfo],
# aspects = [_gen_py_aspect],
# ),
# "_protoc": attr.label(
# default = Label("@com_google_protobuf//:protoc"),
# providers = ["files_to_run"],
# executable = True,
# cfg = "exec",
# ),
# "_protobuf_library": attr.label(
# default = Label("@com_google_protobuf//:protobuf_python"),
# providers = [PyInfo],
# ),
# "imports": attr.string_list(),
# },
# implementation = _generate_py_impl,
# )

py_proto_library = protobuf_py_proto_library

def _generate_pb2_grpc_src_impl(context):
protos = protos_from_context(context)
includes = includes_from_deps(context.attr.deps)
out_files = declare_out_files(protos, context, _GENERATED_GRPC_PROTO_FORMAT)

out_files = declare_out_files(protos, context, _GENERATED_GRPC_PROTO_FORMAT)
plugin_flags = ["grpc_2_0"] + context.attr.strip_prefixes

arguments = []
tools = [context.executable._protoc, context.executable._grpc_plugin]
out_dir = get_out_dir(protos, context)
if out_dir.import_path:
# is virtual imports
out_path = out_dir.path
out_path = get_include_directory(out_files[0])
else:
out_path = context.genfiles_dir.path
arguments += get_plugin_args(
Expand All @@ -209,7 +213,6 @@ def _generate_pb2_grpc_src_impl(context):
]
arguments.append("--proto_path={}".format(context.genfiles_dir.path))
arguments += get_proto_arguments(protos, context.genfiles_dir.path)

context.actions.run(
inputs = protos + includes,
tools = tools,
Expand All @@ -219,7 +222,23 @@ def _generate_pb2_grpc_src_impl(context):
mnemonic = "ProtocInvocation",
)

p = PyInfo(transitive_sources = depset(direct = out_files))
imports = []

# Adding to PYTHONPATH so the generated modules can be imported.
# This is necessary when there is strip_import_prefix, the Python modules are generated under _virtual_imports.
# But it's undesirable otherwise, because it will put the repo root at the top of the PYTHONPATH, ahead of
# directories added through `imports` attributes.
if _VIRTUAL_IMPORTS in out_path:
import_path = out_path

# Handles virtual import cases
if out_path.startswith(context.genfiles_dir.path):
import_path = import_path[len(context.genfiles_dir.path) + 1:]

import_path = "{}/{}".format(context.workspace_name, import_path)
imports.append(import_path)

p = PyInfo(transitive_sources = depset(direct = out_files), imports = depset(direct = imports))
py_info = _merge_pyinfos(
[
p,
Expand Down
12 changes: 12 additions & 0 deletions src/proto/grpc/channelz/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ grpc_proto_library(
well_known_protos = True,
)

# proto_library(
# name = "channelz_proto_descriptors",
# srcs = ["channelz.proto"],
# # strip_import_prefix = "/src/proto/grpc/channelz",
# deps = [
# "@com_google_protobuf//:any_proto",
# "@com_google_protobuf//:duration_proto",
# "@com_google_protobuf//:timestamp_proto",
# "@com_google_protobuf//:wrappers_proto",
# ],
# )

filegroup(
name = "channelz_proto_file",
srcs = [
Expand Down
1 change: 0 additions & 1 deletion src/python/grpcio_channelz/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.proto
*_pb2.py
*_pb2_grpc.py
build/
Expand Down
20 changes: 17 additions & 3 deletions src/python/grpcio_channelz/grpc_channelz/v1/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")

# Copyright 2021 The gRPC Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,15 +18,27 @@ load("//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library")

package(default_visibility = ["//visibility:public"])

proto_library(
name = "channelz_proto_descriptors",
srcs = ["channelz.proto"],
deps = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:timestamp_proto",
"@com_google_protobuf//:wrappers_proto",
],
)

py_proto_library(
name = "channelz_py_pb2",
imports = ["../../"],
deps = ["//src/proto/grpc/channelz:channelz_proto_only"],
# deps = ["//src/proto/grpc/channelz:channelz_proto_descriptors"],
deps = [":channelz_proto_descriptors"],
)

py_grpc_library(
name = "channelz_py_pb2_grpc",
srcs = ["//src/proto/grpc/channelz:channelz_proto_only"],
# srcs = ["//src/proto/grpc/channelz:channelz_proto_descriptors"],
srcs = [":channelz_proto_descriptors"],
strip_prefixes = ["src.python.grpcio_channelz."],
deps = [":channelz_py_pb2"],
)
Expand Down
Loading