packages feed

cabal-buck2-0.6.0.0: buck2/prelude/python/sourcedb/filter.bxl

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.

BUCK_PYTHON_RULE_KINDS = [
    "python_binary",
    "python_library",
    "python_test",
]
BUCK_PYTHON_RULE_KIND_QUERY = "|".join(BUCK_PYTHON_RULE_KINDS)

def get_unfiltered_root_targets(query: bxl.UqueryContext, target_patterns: typing.Any) -> bxl.UnconfiguredTargetSet:
    candidate_targets = utarget_set()
    for pattern in target_patterns:
        candidate_targets += query.kind(
            BUCK_PYTHON_RULE_KIND_QUERY,
            pattern,
        )
    return candidate_targets

def filter_root_targets(query: bxl.UqueryContext, target_patterns: typing.Any) -> bxl.UnconfiguredTargetSet:
    # Find all Pure-Python targets
    candidate_targets = get_unfiltered_root_targets(query, target_patterns)

    # Don't check generated rules
    filtered_targets = candidate_targets - query.attrfilter(
        "labels",
        "generated",
        candidate_targets,
    )

    # Provide an opt-out label
    filtered_targets = filtered_targets - query.attrfilter(
        "labels",
        "no_pyre",
        candidate_targets,
    )
    return filtered_targets

def do_filter(query: bxl.UqueryContext, target_patterns: typing.Any) -> list[TargetLabel]:
    root_targets = filter_root_targets(query, target_patterns)
    return [root_target.label for root_target in root_targets]

def _do_filter_entry_point(ctx: bxl.Context) -> None:
    query = ctx.uquery()
    targets = do_filter(
        query,
        [query.eval(target) for target in ctx.cli_args.target],
    )
    ctx.output.print_json(targets)

filter = bxl_main(
    doc = ("Expand target patterns and look for all targets in immediate sources " + "that will be built by Pyre."),
    impl = _do_filter_entry_point,
    cli_args = {
        "target": cli_args.list(cli_args.string()),
    },
)