Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bazel: Add python envoy_entry_point #19450

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tools/base/entry_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import subprocess
import sys


def main(*args) -> int:
try:
return subprocess.run(args).returncode
except KeyboardInterrupt:
return 1


if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
42 changes: 42 additions & 0 deletions tools/base/envoy_python.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@base_pip3//:requirements.bzl", base_entry_point = "entry_point")

def envoy_py_test(name, package, visibility, envoy_prefix = "@envoy"):
filepath = "$(location %s//tools/testing:base_pytest_runner.py)" % envoy_prefix
Expand Down Expand Up @@ -71,3 +72,44 @@ def envoy_py_binary(

if test:
envoy_py_test(name, package, visibility, envoy_prefix = envoy_prefix)

def envoy_entry_point(
name,
pkg,
main = "//tools/base:entry_point.py",
entry_point = base_entry_point,
script = None,
data = None,
args = None):
"""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.

We may wish to remove this macro should https://github.com/bazelbuild/rules_python/issues/600
be resolved.

The `script` and `pkg` args are passed directly to the `entry_point`.

By default, the pip `entry_point` from `@base_pip3` is used. You can provide
a custom `entry_point` if eg you want to provide an `entry_point` with dev
requirements, or from some other requirements set.

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,
htuch marked this conversation as resolved.
Show resolved Hide resolved
script = script,
),
)
py_binary(
name = name,
srcs = [main],
main = main,
args = ["$(location %s)" % entry_point_name] + args,
data = [entry_point_name] + data,
)