Skip to content
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/base_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

import sys

from __UPSTREAM_PACKAGE__ import main as upstream_main


def main(*args: str) -> int:
return upstream_main(*args)


if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))
68 changes: 63 additions & 5 deletions tools/base/envoy_python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def envoy_py_library(
deps = [],
data = [],
visibility = ["//visibility:public"],
envoy_prefix = ""):
envoy_prefix = "",
test = True):
_parts = name.split(".")
package = ".".join(_parts[:-1])
name = _parts[-1]
Expand All @@ -46,15 +47,16 @@ def envoy_py_library(
data = data,
visibility = visibility,
)

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

def envoy_py_binary(
name = None,
deps = [],
data = [],
visibility = ["//visibility:public"],
envoy_prefix = "@envoy"):
envoy_prefix = "@envoy",
test = True):
_parts = name.split(".")
package = ".".join(_parts[:-1])
name = _parts[-1]
Expand All @@ -67,4 +69,60 @@ def envoy_py_binary(
visibility = visibility,
)

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

def envoy_py_script(
name,
entry_point,
deps = [],
data = [],
visibility = ["//visibility:public"],
envoy_prefix = "@envoy"):
"""This generates a `py_binary` from an entry_point in a python package

Currently, the actual entrypoint callable is hard-coded to `main`.

For example, if you wish to make use of a `console_script` in an upstream
package that resolves as `envoy.code_format.python.command.main` from a
package named `envoy.code_format.python`, you can use this macro as
follows:

```skylark

envoy_py_script(
name = "tools.code_format.python",
entry_point = "envoy.code_format.python.command",
deps = [requirement("envoy.code_format.python")],
```

You will then be able to use the console script from bazel.

Separate args to be passed to the console_script with `--`, eg:

```console

$ bazel run //tools/code_format:python -- -h
```

"""
py_file = "%s.py" % name.split(".")[-1]
output = "$(@D)/%s" % py_file
template_rule = "%s//tools/base:base_command.py" % envoy_prefix
template = "$(location %s)" % template_rule

native.genrule(
name = "py_script_%s" % py_file,
cmd = "sed s/__UPSTREAM_PACKAGE__/%s/ %s > \"%s\"" % (entry_point, template, output),
tools = [template_rule],
outs = [py_file],
)

envoy_py_binary(
name = name,
deps = deps,
data = data,
visibility = visibility,
envoy_prefix = envoy_prefix,
test = False,
)