From 32aaab078ed26e392d64e1c0996d951435d7d357 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lespiau Date: Wed, 17 Aug 2022 04:48:38 -0700 Subject: [PATCH] Replace rstrip with removesuffix. 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 --- reverb/cc/platform/default/build_rules.bzl | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/reverb/cc/platform/default/build_rules.bzl b/reverb/cc/platform/default/build_rules.bzl index 2e45350..ba334da 100644 --- a/reverb/cc/platform/default/build_rules.bzl +++ b/reverb/cc/platform/default/build_rules.bzl @@ -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. @@ -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: @@ -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 `_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 @@ -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 = [] @@ -212,8 +212,8 @@ 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"): @@ -221,7 +221,7 @@ def reverb_cc_grpc_library( 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 = []