-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
python.go
162 lines (147 loc) · 6.55 KB
/
python.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
var pythonProtoLibraryRuleTemplate = mustTemplate(`load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs")
load("@rules_proto_grpc_python_pip_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load("//:{{ .Lang.Name }}_{{ .Rule.Kind }}_compile.bzl", "{{ .Lang.Name }}_{{ .Rule.Kind }}_compile")
def {{ .Rule.Name }}(name, **kwargs):
# Compile protos
name_pb = name + "_pb"
python_proto_compile(
name = name_pb,
{{ .Common.CompileArgsForwardingSnippet }}
)
# 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],
{{ .Common.LibraryArgsForwardingSnippet }}
)
PROTO_DEPS = [
Label(requirement("protobuf")),
]`)
var pythonGrpcLibraryRuleTemplate = mustTemplate(`load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs")
load("@rules_proto_grpc_python_pip_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load("//:{{ .Lang.Name }}_{{ .Rule.Kind }}_compile.bzl", "{{ .Lang.Name }}_{{ .Rule.Kind }}_compile")
def {{ .Rule.Name }}(name, **kwargs):
# Compile protos
name_pb = name + "_pb"
python_grpc_compile(
name = name_pb,
{{ .Common.CompileArgsForwardingSnippet }}
)
# 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],
{{ .Common.LibraryArgsForwardingSnippet }}
)
GRPC_DEPS = [
Label(requirement("grpcio")),
Label(requirement("protobuf")),
]`)
var pythonGrpclibLibraryRuleTemplate = mustTemplate(`load("@rules_proto_grpc//:defs.bzl", "bazel_build_rule_common_attrs", "proto_compile_attrs")
load("@rules_proto_grpc_python_pip_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load("//:{{ .Lang.Name }}_grpclib_compile.bzl", "{{ .Lang.Name }}_grpclib_compile")
def {{ .Rule.Name }}(name, **kwargs):
# Compile protos
name_pb = name + "_pb"
python_grpclib_compile(
name = name_pb,
{{ .Common.CompileArgsForwardingSnippet }}
)
# Create {{ .Lang.Name }} library
py_library(
name = name,
srcs = [name_pb],
deps = GRPCLIB_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],
{{ .Common.LibraryArgsForwardingSnippet }}
)
GRPCLIB_DEPS = [
Label("@protobuf//:protobuf_python"),
Label(requirement("grpclib")),
]`)
var pythonModuleSuffixLines = `bazel_dep(name = "rules_python", version = "0.29.0")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.11")`
func makePython() *Language {
return &Language{
Name: "python",
DisplayName: "Python",
Notes: mustTemplate("Rules for generating Python protobuf and gRPC ``.py`` files and libraries using standard Protocol Buffers and gRPC or `grpclib <https://github.com/vmagamedov/grpclib>`_. Libraries are created with ``py_library`` from ``rules_python``. To use the fast C++ Protobuf implementation, you can add ``--define=use_fast_cpp_protos=true`` to your build, but this requires you setup the path to your Python headers.\n\n.. note:: If you have proto libraries that produce overlapping import paths, be sure to set ``legacy_create_init=False`` on the top level ``py_binary`` or ``py_test`` to ensure all paths are importable."),
ModuleSuffixLines: pythonModuleSuffixLines,
Aliases: map[string]string{
"py_proto_compile": "python_proto_compile",
"py_grpc_compile": "python_grpc_compile",
"py_grpclib_compile": "python_grpclib_compile",
"py_proto_library": "python_proto_library",
"py_grpc_library": "python_grpc_library",
"py_grpclib_library": "python_grpclib_library",
},
Rules: []*Rule{
&Rule{
Name: "python_proto_compile",
Kind: "proto",
Implementation: compileRuleTemplate,
Plugins: []string{"//:proto_plugin"},
BuildExample: protoCompileExampleTemplate,
Doc: "Generates Python protobuf ``.py`` files",
Attrs: compileRuleAttrs,
},
&Rule{
Name: "python_grpc_compile",
Kind: "grpc",
Implementation: compileRuleTemplate,
Plugins: []string{"//:proto_plugin", "//:grpc_plugin"},
BuildExample: grpcCompileExampleTemplate,
Doc: "Generates Python protobuf and gRPC ``.py`` files",
Attrs: compileRuleAttrs,
},
&Rule{
Name: "python_grpclib_compile",
Kind: "grpc",
Implementation: compileRuleTemplate,
Plugins: []string{"//:proto_plugin", "//:grpclib_plugin"},
BuildExample: grpcCompileExampleTemplate,
Doc: "Generates Python protobuf and grpclib ``.py`` files (supports Python 3 only)",
Attrs: compileRuleAttrs,
SkipTestPlatforms: []string{"windows", "macos"},
},
&Rule{
Name: "python_proto_library",
Kind: "proto",
Implementation: pythonProtoLibraryRuleTemplate,
BuildExample: protoLibraryExampleTemplate,
Doc: "Generates a Python protobuf library using ``py_library`` from ``rules_python``",
Attrs: libraryRuleAttrs,
},
&Rule{
Name: "python_grpc_library",
Kind: "grpc",
Implementation: pythonGrpcLibraryRuleTemplate,
BuildExample: grpcLibraryExampleTemplate,
Doc: "Generates a Python protobuf and gRPC library using ``py_library`` from ``rules_python``",
Attrs: libraryRuleAttrs,
SkipTestPlatforms: []string{"windows"},
},
&Rule{
Name: "python_grpclib_library",
Kind: "grpc",
Implementation: pythonGrpclibLibraryRuleTemplate,
BuildExample: grpcLibraryExampleTemplate,
Doc: "Generates a Python protobuf and grpclib library using ``py_library`` from ``rules_python`` (supports Python 3 only)",
Attrs: libraryRuleAttrs,
SkipTestPlatforms: []string{"windows", "macos"},
},
},
}
}