From abb12cdc06989ee823676ffd643fa3215b5d51eb Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Wed, 8 Sep 2021 10:26:10 +0100 Subject: [PATCH 1/3] bazel: Add implementation of ABazelQuery Signed-off-by: Ryan Northey --- tools/base/BUILD | 11 +++++++++++ tools/base/bazel_query.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tools/base/bazel_query.py diff --git a/tools/base/BUILD b/tools/base/BUILD index c1d243c119ed7..0a4488ddb4d33 100644 --- a/tools/base/BUILD +++ b/tools/base/BUILD @@ -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") @@ -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"), + ], +) diff --git a/tools/base/bazel_query.py b/tools/base/bazel_query.py new file mode 100644 index 0000000000000..d31d5c042016f --- /dev/null +++ b/tools/base/bazel_query.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +"""Envoy Bazel query implementation. + +This module can be used either as a `py_binary` or a `py_library`. +""" +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(query(*args[0:1])) + + +if __name__ == "__main__": + sys.exit(main(*sys.argv[1:])) + +__all__ = ("query",) From b13ff4b192f746f9462aa1bc93aef6ea4eaec759 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Wed, 15 Sep 2021 15:19:20 +0100 Subject: [PATCH 2/3] add usage/upstream information Signed-off-by: Ryan Northey --- tools/base/bazel_query.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tools/base/bazel_query.py b/tools/base/bazel_query.py index d31d5c042016f..47f11e16905dc 100644 --- a/tools/base/bazel_query.py +++ b/tools/base/bazel_query.py @@ -2,7 +2,35 @@ """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("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 @@ -26,7 +54,7 @@ def path(self) -> pathlib.Path: def main(*args): - print(query(*args[0:1])) + print(json.dumps(query(*args[0:1]))) if __name__ == "__main__": From 40efec8aa288b1afb64a46fa343090d0ff126686 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Wed, 15 Sep 2021 20:24:05 +0100 Subject: [PATCH 3/3] bazel-query-typo Signed-off-by: Ryan Northey --- tools/base/bazel_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/base/bazel_query.py b/tools/base/bazel_query.py index 47f11e16905dc..48825838de45e 100644 --- a/tools/base/bazel_query.py +++ b/tools/base/bazel_query.py @@ -14,7 +14,7 @@ ```python from tools.base.bazel_query import query -result = query("source/...") +result = query("deps(source/...)") ``` NB: This allows running queries that do not define scope and cannot be