Skip to content

Commit

Permalink
bazel: Add implementation of ABazelQuery (#18021)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax authored Sep 16, 2021
1 parent bf0a16a commit 44bcb12
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
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",)

0 comments on commit 44bcb12

Please sign in to comment.