def ApplyLowerPriorityArgs(args, lower_pri_args, issue_cli_warning=False):
    """Apply lower-priority arg values from a dictionary to args without values.

  May be used to apply arg default values, or to merge args from another source,
  such as an arg-file. Args which already have a value are never modified by
  this function. Thus, if there are multiple sets of lower-priority args, they
  should be applied in order from highest-to-lowest precedence.

  Args:
    args: the existing argparse.Namespace. All the arguments that were provided
      to the command invocation (i.e. group and command arguments combined),
      plus any arg defaults already applied to the namespace. These args have
      higher priority than the lower_pri_args.
    lower_pri_args: a dict mapping lower-priority arg names to their values.
    issue_cli_warning: (boolean) issue a warning if an arg already has a value
      from the command line and we do not apply the lower-priority arg value
      (used for arg-files where any args specified in the file are lower in
      priority than the CLI args.).
  """
    for arg in lower_pri_args:
        if getattr(args, arg, None) is None:
            log.debug('Applying default {0}: {1}'.format(
                arg, six.text_type(lower_pri_args[arg])))
            setattr(args, arg, lower_pri_args[arg])
        elif issue_cli_warning and getattr(args, arg) != lower_pri_args[arg]:
            ext_name = exceptions.ExternalArgNameFrom(arg)
            log.warning(
                'Command-line argument "--{0} {1}" overrides file argument "{2}: {3}"'
                .format(ext_name, _FormatArgValue(getattr(args, arg)),
                        ext_name, _FormatArgValue(lower_pri_args[arg])))
Exemple #2
0
def ValidateArgsForTestType(args, test_type, type_rules, shared_rules,
                            all_test_args_set):
    """Raise errors if required args are missing or invalid args are present.

  Args:
    args: an argparse.Namespace object which contains attributes for all the
      arguments that were provided to the command invocation (i.e. command
      group and command arguments combined).
    test_type: string containing the type of test to run.
    type_rules: a nested dictionary defining the required and optional args
      per type of test, plus any default values.
    shared_rules: a nested dictionary defining the required and optional args
      shared among all test types, plus any default values.
    all_test_args_set: a set of strings for every gcloud-test argument to use
      for validation.

  Raises:
    InvalidArgException: If an arg doesn't pair with the test type.
    RequiredArgumentException: If a required arg for the test type is missing.
  """
    required_args = type_rules[test_type]['required'] + shared_rules['required']
    optional_args = type_rules[test_type]['optional'] + shared_rules['optional']
    allowable_args_for_type = required_args + optional_args

    # Raise an error if an optional test arg is not allowed with this test_type.
    for arg in all_test_args_set:
        if getattr(args, arg, None) is not None:  # Ignore args equal to None
            if arg not in allowable_args_for_type:
                raise test_exceptions.InvalidArgException(
                    arg,
                    'may not be used with test type [{0}].'.format(test_type))
    # Raise an error if a required test arg is missing or equal to None.
    for arg in required_args:
        if getattr(args, arg, None) is None:
            raise exceptions.RequiredArgumentException(
                '{0}'.format(test_exceptions.ExternalArgNameFrom(arg)),
                'must be specified with test type [{0}].'.format(test_type))