Skip to content

Commit

Permalink
generating macros
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhp committed Jan 18, 2024
1 parent b6745a8 commit d0a84ba
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ tasks:
- make test_workspace_import_prefix
- make test_workspace_nested_output_directory
- make test_workspace_objc_capitalisation
- make test_workspace_prefix_path
- make test_workspace_python3_grpc
- make test_workspace_python_dashes
- make test_workspace_python_deps
Expand Down Expand Up @@ -433,6 +434,10 @@ tasks:
- cd test_workspaces/objc_capitalisation
- bazel --batch test %BAZEL_EXTRA_FLAGS% --enable_runfiles --verbose_failures --test_output=errors --disk_cache=../bazel-disk-cache //... || exit 1
- cd ../..
- echo prefix_path
- cd test_workspaces/prefix_path
- bazel --batch test %BAZEL_EXTRA_FLAGS% --enable_runfiles --verbose_failures --test_output=errors --disk_cache=../bazel-disk-cache //... || exit 1
- cd ../..
- echo python3_grpc
- cd test_workspaces/python3_grpc
- bazel --batch test %BAZEL_EXTRA_FLAGS% --enable_runfiles --verbose_failures --test_output=errors --disk_cache=../bazel-disk-cache //... || exit 1
Expand Down Expand Up @@ -479,6 +484,7 @@ tasks:
- make test_workspace_import_prefix
- make test_workspace_nested_output_directory
- make test_workspace_objc_capitalisation
- make test_workspace_prefix_path
- make test_workspace_python3_grpc
- make test_workspace_python_dashes
- make test_workspace_python_deps
Expand Down
2 changes: 1 addition & 1 deletion modules/python/python_grpc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def python_grpc_library(name, **kwargs):
} # Forward args
)

# for other code to import generated code with prefix_path if that exists
# for other code to import generated code with prefix_path if it's given
output_mode = kwargs.get("output_mode", "PREFIXED")
if output_mode == "PREFIXED":
imports = [name_pb]
Expand Down
3 changes: 2 additions & 1 deletion modules/python/python_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ def python_proto_library(name, **kwargs):
} # Forward args
)

# Create python library
# for other code to import generated code with prefix_path if it's given
output_mode = kwargs.get("output_mode", "PREFIXED")
if output_mode == "PREFIXED":
imports = [name_pb]
else:
imports = ["."]

# Create python library
py_library(
name = name,
srcs = [name_pb],
Expand Down
7 changes: 6 additions & 1 deletion test_workspaces/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ test_workspace_objc_capitalisation:
cd test_workspaces/objc_capitalisation; \
bazel --batch test ${BAZEL_EXTRA_FLAGS} --verbose_failures --disk_cache=../bazel-disk-cache --test_output=errors //...

.PHONY: test_workspace_prefix_path
test_workspace_prefix_path:
cd test_workspaces/prefix_path; \
bazel --batch test ${BAZEL_EXTRA_FLAGS} --verbose_failures --disk_cache=../bazel-disk-cache --test_output=errors //...

.PHONY: test_workspace_python3_grpc
test_workspace_python3_grpc:
cd test_workspaces/python3_grpc; \
Expand Down Expand Up @@ -84,4 +89,4 @@ test_workspace_special_characters:
bazel --batch test ${BAZEL_EXTRA_FLAGS} --verbose_failures --disk_cache=../bazel-disk-cache --test_output=errors //...

.PHONY: all_test_workspaces
all_test_workspaces: test_workspace_absolute_strip_import_prefix test_workspace_combined_strip_and_add_prefix test_workspace_common_cpp_library test_workspace_exclusions test_workspace_generated_proto test_workspace_go_fixer test_workspace_go_importpath test_workspace_import_prefix test_workspace_nested_output_directory test_workspace_objc_capitalisation test_workspace_python3_grpc test_workspace_python_dashes test_workspace_python_deps test_workspace_python_dots test_workspace_relative_strip_import_prefix test_workspace_shared_proto test_workspace_special_characters
all_test_workspaces: test_workspace_absolute_strip_import_prefix test_workspace_combined_strip_and_add_prefix test_workspace_common_cpp_library test_workspace_exclusions test_workspace_generated_proto test_workspace_go_fixer test_workspace_go_importpath test_workspace_import_prefix test_workspace_nested_output_directory test_workspace_objc_capitalisation test_workspace_prefix_path test_workspace_python3_grpc test_workspace_python_dashes test_workspace_python_deps test_workspace_python_dots test_workspace_relative_strip_import_prefix test_workspace_shared_proto test_workspace_special_characters
4 changes: 2 additions & 2 deletions test_workspaces/prefix_path/example.com/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ proto_library(

python_grpc_library(
name = "py_lib",
protos = [":proto_lib"],
prefix_path = "demo",
output_mode = "NO_PREFIX_FLAT",
prefix_path = "demo",
protos = [":proto_lib"],
python_version = "PY3",
)

Expand Down
23 changes: 21 additions & 2 deletions tools/rulegen/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ def {{ .Rule.Name }}(name, **kwargs):
{{ .Common.CompileArgsForwardingSnippet }}
)
# for other code to import generated code with prefix_path if it's given
output_mode = kwargs.get("output_mode", "PREFIXED")
if output_mode == "PREFIXED":
imports = [name_pb]
else:
imports = ["."]
# Create {{ .Lang.Name }} library
py_library(
name = name,
srcs = [name_pb],
deps = PROTO_DEPS + kwargs.get("deps", []),
data = kwargs.get("data", []), # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case
imports = [name_pb],
imports = imports,
{{ .Common.LibraryArgsForwardingSnippet }}
)
Expand All @@ -40,13 +47,25 @@ def {{ .Rule.Name }}(name, **kwargs):
{{ .Common.CompileArgsForwardingSnippet }}
)
# for other code to import generated code with prefix_path if it's given
output_mode = kwargs.get("output_mode", "PREFIXED")
if output_mode == "PREFIXED":
imports = [name_pb]
else:
imports = ["."]
# for pb2_grpc.py to import pb2.py
prefix_path = kwargs.get("prefix_path", None)
if prefix_path:
imports.append(imports[0] + "/" + prefix_path)
# Create {{ .Lang.Name }} library
py_library(
name = name,
srcs = [name_pb],
deps = GRPC_DEPS + kwargs.get("deps", []),
data = kwargs.get("data", []), # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case
imports = [name_pb],
imports = imports,
{{ .Common.LibraryArgsForwardingSnippet }}
)
Expand Down

0 comments on commit d0a84ba

Please sign in to comment.