From 44bcb12967796a5406419395cbe5c1ed85966fb6 Mon Sep 17 00:00:00 2001 From: phlax Date: Thu, 16 Sep 2021 08:23:59 +0100 Subject: [PATCH] bazel: Add implementation of ABazelQuery (#18021) Signed-off-by: Ryan Northey --- tools/base/BUILD | 11 +++++++ tools/base/bazel_query.py | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tools/base/bazel_query.py diff --git a/tools/base/BUILD b/tools/base/BUILD index c1d243c119ed..0a4488ddb4d3 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 000000000000..48825838de45 --- /dev/null +++ b/tools/base/bazel_query.py @@ -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",)