Skip to content

Commit

Permalink
Replace rstrip with removesuffix.
Browse files Browse the repository at this point in the history
rstrip considers the argument as a *set* of characters to remove. "accccc_cc".rstrip("_cc") will return "a".

`removesuffix` was added to Python 3.9, and in Bazel 5.2.0.
https://newreleases.io/project/github/bazelbuild/bazel/release/5.2.0

We could drop Python versions before 3.9, but I opted for this version instead.

PiperOrigin-RevId: 468166263
Change-Id: Ie7c9508d04aa57899fbe3c6acea5d420ed971169
  • Loading branch information
jblespiau authored and copybara-github committed Aug 17, 2022
1 parent 2ec16f5 commit 32aaab0
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions reverb/cc/platform/default/build_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@ def reverb_kernel_library(name, srcs = [], deps = [], **kwargs):
**kwargs
)

def _removesuffix(x, txt):
"""Backport of x._removesuffix(txt) for Python version earlier than 3.9."""
if x.endswith(txt):
return x[:-len(txt)]
return x

def _normalize_proto(x):
if x.endswith("_proto"):
x = x.rstrip("_proto")
x = _removesuffix(x, "_proto")
if x.endswith("_cc"):
x = x.rstrip("_cc")
x = _removesuffix(x, "_cc")
if x.endswith("_pb2"):
x = x.rstrip("_pb2")
x = _removesuffix(x, "_pb2")
return x

def _filegroup_name(x):
return _normalize_proto(x) + "_filegroup"

def _strip_proto_suffix(x):
# Workaround for bug that str.rstrip(".END") takes off more than just ".END"
if x.endswith(".proto"):
x = x[:-6]
return x

def reverb_cc_proto_library(name, srcs = [], deps = [], **kwargs):
"""Build a proto cc_library.
Expand All @@ -76,8 +76,8 @@ def reverb_cc_proto_library(name, srcs = [], deps = [], **kwargs):
deps: Any reverb_cc_proto_library targets.
**kwargs: Any additional args for the cc_library rule.
"""
gen_srcs = [_strip_proto_suffix(x) + ".pb.cc" for x in srcs]
gen_hdrs = [_strip_proto_suffix(x) + ".pb.h" for x in srcs]
gen_srcs = [_removesuffix(x, ".proto") + ".pb.cc" for x in srcs]
gen_hdrs = [_removesuffix(x, ".proto") + ".pb.h" for x in srcs]
src_paths = ["$(location {})".format(x) for x in srcs]
dep_srcs = []
for x in deps:
Expand Down Expand Up @@ -134,7 +134,7 @@ def reverb_py_proto_library(name, srcs = [], deps = [], **kwargs):
This rule does three things:
1) Create a filegroup with name `name` that contains `srcs`
1) Create a filegroup with name `<name>_filegroup` that contains `srcs`
and any sources from deps named "x_proto" or "x_py_proto".
2) Uses protoc to compile srcs to _pb2.py files, allowing any
Expand All @@ -149,7 +149,7 @@ def reverb_py_proto_library(name, srcs = [], deps = [], **kwargs):
deps: Any reverb_cc_proto_library targets.
**kwargs: Any additional args for the cc_library rule.
"""
gen_srcs = [_strip_proto_suffix(x) + "_pb2.py" for x in srcs]
gen_srcs = [_removesuffix(x, ".proto") + "_pb2.py" for x in srcs]
src_paths = ["$(location {})".format(x) for x in srcs]
proto_deps = []
py_deps = []
Expand Down Expand Up @@ -212,16 +212,16 @@ def reverb_cc_grpc_library(
generate_mocks: If true, creates mock headers for each source.
**kwargs: Any additional args for the cc_library rule.
"""
gen_srcs = [x.rstrip(".proto") + ".grpc.pb.cc" for x in srcs]
gen_hdrs = [x.rstrip(".proto") + ".grpc.pb.h" for x in srcs]
gen_srcs = [_removesuffix(x, ".proto") + ".grpc.pb.cc" for x in srcs]
gen_hdrs = [_removesuffix(x, ".proto") + ".grpc.pb.h" for x in srcs]
proto_src_deps = []
for x in deps:
if x.endswith("_proto"):
proto_src_deps.append(_filegroup_name(x))
src_paths = ["$(location {})".format(x) for x in srcs]

if generate_mocks:
gen_mocks = [x.rstrip(".proto") + "_mock.grpc.pb.h" for x in srcs]
gen_mocks = [_removesuffix(x, ".proto") + "_mock.grpc.pb.h" for x in srcs]
else:
gen_mocks = []

Expand Down

0 comments on commit 32aaab0

Please sign in to comment.