Search in sources :

Example 1 with AllowValues

use of com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues in project bazel by bazelbuild.

the class InvocationPolicyEnforcer method enforce.

/**
   * Applies this OptionsPolicyEnforcer's policy to the given OptionsParser.
   *
   * @param parser The OptionsParser to enforce policy on.
   * @param command The current blaze command, for flag policies that apply to only specific
   *     commands. Such policies will be enforced only if they contain this command or a command
   *     they inherit from
   * @throws OptionsParsingException if any flag policy is invalid.
   */
public void enforce(OptionsParser parser, @Nullable String command) throws OptionsParsingException {
    if (invocationPolicy == null || invocationPolicy.getFlagPoliciesCount() == 0) {
        return;
    }
    ImmutableSet<String> commandAndParentCommands = command == null ? ImmutableSet.<String>of() : CommandNameCache.CommandNameCacheInstance.INSTANCE.get(command);
    for (FlagPolicy flagPolicy : invocationPolicy.getFlagPoliciesList()) {
        String flagName = flagPolicy.getFlagName();
        // then the policy applies to all commands.
        if (!flagPolicy.getCommandsList().isEmpty() && !commandAndParentCommands.isEmpty()) {
            boolean flagApplies = false;
            for (String policyCommand : flagPolicy.getCommandsList()) {
                if (commandAndParentCommands.contains(policyCommand)) {
                    flagApplies = true;
                    break;
                }
            }
            if (!flagApplies) {
                continue;
            }
        }
        OptionValueDescription valueDescription;
        try {
            valueDescription = parser.getOptionValueDescription(flagName);
        } catch (IllegalArgumentException e) {
            // This flag doesn't exist. We are deliberately lenient if the flag policy has a flag
            // we don't know about. This is for better future proofing so that as new flags are added,
            // new policies can use the new flags without worrying about older versions of Bazel.
            log.info(String.format("Flag '%s' specified by invocation policy does not exist", flagName));
            continue;
        }
        OptionDescription optionDescription = parser.getOptionDescription(flagName);
        // getOptionDescription() will return null if the option does not exist, however
        // getOptionValueDescription() above would have thrown an IllegalArgumentException if that
        // were the case.
        Verify.verifyNotNull(optionDescription);
        switch(flagPolicy.getOperationCase()) {
            case SET_VALUE:
                applySetValueOperation(parser, flagPolicy, flagName, valueDescription, optionDescription);
                break;
            case USE_DEFAULT:
                applyUseDefaultOperation(parser, "UseDefault", flagName);
                break;
            case ALLOW_VALUES:
                AllowValues allowValues = flagPolicy.getAllowValues();
                FilterValueOperation.ALLOW_VALUE_OPERATION.apply(parser, allowValues.getAllowedValuesList(), allowValues.hasNewValue() ? allowValues.getNewValue() : null, allowValues.hasUseDefault(), flagName, valueDescription, optionDescription);
                break;
            case DISALLOW_VALUES:
                DisallowValues disallowValues = flagPolicy.getDisallowValues();
                FilterValueOperation.DISALLOW_VALUE_OPERATION.apply(parser, disallowValues.getDisallowedValuesList(), disallowValues.hasNewValue() ? disallowValues.getNewValue() : null, disallowValues.hasUseDefault(), flagName, valueDescription, optionDescription);
                break;
            case OPERATION_NOT_SET:
                throw new OptionsParsingException(String.format("Flag policy for flag '%s' does not " + "have an operation", flagName));
            default:
                log.warning(String.format("Unknown operation '%s' from invocation policy for flag '%s'", flagPolicy.getOperationCase(), flagName));
                break;
        }
    }
}
Also used : OptionValueDescription(com.google.devtools.common.options.OptionsParser.OptionValueDescription) OptionDescription(com.google.devtools.common.options.OptionsParser.OptionDescription) FlagPolicy(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy) DisallowValues(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues) AllowValues(com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException)

Aggregations

AllowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.AllowValues)1 DisallowValues (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.DisallowValues)1 FlagPolicy (com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.FlagPolicy)1 OptionDescription (com.google.devtools.common.options.OptionsParser.OptionDescription)1 OptionValueDescription (com.google.devtools.common.options.OptionsParser.OptionValueDescription)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1