Skip to content

Commit

Permalink
tooling: Use envoy_entry_point (envoyproxy#19536)
Browse files Browse the repository at this point in the history
This is a follow up to use the macro added in envoyproxy#19450 for existing entry_points

Also contains a fix to use a template to create the py_binary main to ensure the py_binary retains the path to the entry point

Signed-off-by: Ryan Northey <ryan@synca.io>
Signed-off-by: Josh Perry <josh.perry@mx.com>
  • Loading branch information
phlax authored and Josh Perry committed Feb 13, 2022
1 parent d06e845 commit c848fc2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
4 changes: 4 additions & 0 deletions tools/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ licenses(["notice"]) # Apache 2

envoy_package()

exports_files([
"entry_point.py",
])

py_binary(
name = "bazel_query",
srcs = ["bazel_query.py"],
Expand Down
24 changes: 23 additions & 1 deletion tools/base/entry_point.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import pathlib
import subprocess
import sys

ENTRY_POINT_ALIAS = "_ENTRY_POINT_ALIAS_"


def find_tool_path():
# In order to work in both build/run scenarios, and to work
# when the rule is invoked directly, or by another rule, the
# path to the entry_point binary has to be found from the
# `ENTRY_POINT_ALIAS` that is injected by the `genrule`

entry_point_alias = f"external/{ENTRY_POINT_ALIAS.split('/external/')[1]}"
if pathlib.Path(entry_point_alias).exists():
return entry_point_alias
for x in pathlib.Path(".").glob(f"**/{entry_point_alias}"):
return x


def main(*args) -> int:
tool_path = find_tool_path()

if not tool_path:
print(f"Unable to locate tool: {ENTRY_POINT_ALIAS}")
return 1

try:
return subprocess.run(args).returncode
return subprocess.run([tool_path, *args]).returncode
except KeyboardInterrupt:
return 1

Expand Down
40 changes: 27 additions & 13 deletions tools/base/envoy_python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def envoy_entry_point(
entry_point = base_entry_point,
script = None,
data = None,
args = None):
args = None,
envoy_prefix = "@envoy"):
"""This macro provides the convenience of using an `entry_point` while
also being able to create a rule with associated `args` and `data`, as is
possible with the normal `py_binary` rule.
Expand All @@ -97,19 +98,32 @@ def envoy_entry_point(
A `py_binary` is dynamically created to wrap the `entry_point` with provided
`args` and `data`.
"""
script = script or pkg
entry_point_name = "%s_entry_point" % name
native.alias(
name = entry_point_name,
actual = entry_point(
pkg = pkg,
script = script,
),
actual_entry_point = entry_point(
pkg = pkg,
script = script or pkg,
)
entry_point_script = "%s%s" % (envoy_prefix, main)
entry_point_py = "entry_point_%s_main.py" % name
entry_point_wrapper = "entry_point_%s_wrapper" % name
entry_point_path = "$(location %s)" % entry_point_script
entry_point_alias = "$(location %s)" % actual_entry_point

native.genrule(
name = entry_point_wrapper,
cmd = """
sed s#_ENTRY_POINT_ALIAS_#%s# %s > \"$@\"
""" % (entry_point_alias, entry_point_path),
tools = [
actual_entry_point,
entry_point_script,
],
outs = [entry_point_py],
)

py_binary(
name = name,
srcs = [main],
main = main,
args = ["$(location %s)" % entry_point_name] + args,
data = [entry_point_name] + data,
srcs = [entry_point_wrapper, actual_entry_point],
main = entry_point_py,
args = (args or []),
data = (data or []),
)
23 changes: 7 additions & 16 deletions tools/distribution/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//bazel:envoy_build_system.bzl", "envoy_package")
load("@base_pip3//:requirements.bzl", "entry_point")
load("//tools/base:envoy_python.bzl", "envoy_entry_point")

licenses(["notice"]) # Apache 2

Expand All @@ -9,26 +9,17 @@ exports_files([
"distrotest.sh",
])

alias(
envoy_entry_point(
name = "release",
actual = entry_point(
pkg = "envoy.distribution.release",
script = "envoy.distribution.release",
),
pkg = "envoy.distribution.release",
)

alias(
envoy_entry_point(
name = "sign",
actual = entry_point(
pkg = "envoy.gpg.sign",
script = "envoy.gpg.sign",
),
pkg = "envoy.gpg.sign",
)

alias(
envoy_entry_point(
name = "verify",
actual = entry_point(
pkg = "envoy.distribution.verify",
script = "envoy.distribution.verify",
),
pkg = "envoy.distribution.verify",
)
11 changes: 4 additions & 7 deletions tools/docs/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@base_pip3//:requirements.bzl", "entry_point", "requirement")
load("@base_pip3//:requirements.bzl", "requirement")
load("//tools/base:envoy_python.bzl", "envoy_entry_point", "envoy_py_binary")
load("//bazel:envoy_build_system.bzl", "envoy_package")
load("//tools/base:envoy_python.bzl", "envoy_py_binary")

licenses(["notice"]) # Apache 2

Expand Down Expand Up @@ -40,12 +40,9 @@ py_binary(
#
# https://github.com/envoyproxy/pytooling

alias(
envoy_entry_point(
name = "sphinx_runner",
actual = entry_point(
pkg = "envoy.docs.sphinx_runner",
script = "envoy.docs.sphinx_runner",
),
pkg = "envoy.docs.sphinx_runner",
)

envoy_py_binary(
Expand Down

0 comments on commit c848fc2

Please sign in to comment.