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
11 changes: 11 additions & 0 deletions tools/base/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@base_pip3//:requirements.bzl", "requirement")
load("//bazel:envoy_build_system.bzl", "envoy_package")
load("//tools/base:envoy_python.bzl", "envoy_py_library")
Expand Down Expand Up @@ -37,3 +38,13 @@ envoy_py_library(
requirement("setuptools"),
],
)

py_binary(
name = "bazel_query",
srcs = ["bazel_query.py"],
main = "bazel_query.py",
deps = [
"@envoy_repo",
requirement("envoy.base.utils"),
],
)
63 changes: 63 additions & 0 deletions tools/base/bazel_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""Envoy Bazel query implementation.

This module can be used either as a `py_binary` or a `py_library`.

cli usage (outputs to json):

```console
$ bazel run //tools/base:bazel_query "deps(source/...)" | jq "."
```

python usage:

```python
from tools.base.bazel_query import query

result = query("deps(source/...)")
```

NB: This allows running queries that do not define scope and cannot be
run as genqueries. **It should not therefore be used in build rules**.
"""

# The upstream lib is maintained here:
#
# https://github.com/envoyproxy/pytooling/tree/main/envoy.base.utils
#
# Please submit issues/PRs to the pytooling repo:
#
# https://github.com/envoyproxy/pytooling
#

import json
import pathlib
import sys
from functools import cached_property

import abstracts

from envoy.base.utils import ABazelQuery

import envoy_repo


@abstracts.implementer(ABazelQuery)
class EnvoyBazelQuery:

@cached_property
def path(self) -> pathlib.Path:
return pathlib.Path(envoy_repo.PATH)


query = EnvoyBazelQuery().query


def main(*args):
print(json.dumps(query(*args[0:1])))


if __name__ == "__main__":
sys.exit(main(*sys.argv[1:]))

__all__ = ("query",)