blob: 4446f43aa99872184934ac03716e21b85330e419 [file] [log] [blame]
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.query2;
import static com.google.devtools.build.lib.query2.engine.InputsFunction.INPUTS;
import static com.google.devtools.build.lib.query2.engine.MnemonicFunction.MNEMONIC;
import static com.google.devtools.build.lib.query2.engine.OutputsFunction.OUTPUTS;
import com.google.common.collect.Streams;
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.Artifact;
/** Utility class for Aquery */
public class AqueryUtils {
/**
* Return true if the given {@code action} matches the filters specified in {@code actionFilters}.
*
* @param action the analysis metadata of an action
* @param actionFilters the filters parsed from the query expression
* @return whether the action matches the filtering patterns
*/
public static boolean matchesAqueryFilters(
ActionAnalysisMetadata action, AqueryActionFilter actionFilters) {
Iterable<Artifact> inputs = action.getInputs();
Iterable<Artifact> outputs = action.getOutputs();
String mnemonic = action.getMnemonic();
if (actionFilters.hasFilterForFunction(MNEMONIC)) {
if (!actionFilters.matchesAllPatternsForFunction(MNEMONIC, mnemonic)) {
return false;
}
}
if (actionFilters.hasFilterForFunction(INPUTS)) {
Boolean containsFile =
Streams.stream(inputs)
.map(
artifact ->
actionFilters.matchesAllPatternsForFunction(
INPUTS, artifact.getExecPathString()))
.reduce(false, Boolean::logicalOr);
if (!containsFile) {
return false;
}
}
if (actionFilters.hasFilterForFunction(OUTPUTS)) {
Boolean containsFile =
Streams.stream(outputs)
.map(
artifact ->
actionFilters.matchesAllPatternsForFunction(
OUTPUTS, artifact.getExecPathString()))
.reduce(false, Boolean::logicalOr);
return containsFile;
}
return true;
}
}