HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/396/lib/googlecloudsdk/command_lib/flag_coverage/generate.py
# -*- coding: utf-8 -*- #
# Copyright 2018 Google LLC. 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.

"""Coverage tree generator. Used for flag coverage kokoro presubmit."""

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from googlecloudsdk.calliope import walker
from googlecloudsdk.core.console import progress_tracker
from googlecloudsdk.core.resource import resource_printer
from googlecloudsdk.core.resource import resource_projector


def GenerateCoverageTree(cli, branch=None, restrict=None):
  """Generates and returns the static completion CLI tree.

  Args:
    cli: The CLI.
    branch: The path of the CLI subtree to generate.
    restrict: The paths in the tree that we are allowing the tree to walk under.

  Returns:
    Returns the serialized static completion CLI tree.
  """
  with progress_tracker.ProgressTracker(
      'Generating the flag coverage CLI tree.'):
    return resource_projector.MakeSerializable(
        CoverageTreeGenerator(cli, branch, restrict=restrict).Walk())


class CoverageCommandNode(dict):
  """Command/group info.

  Attributes:
    commands: {str:_Command}, The subcommands in a command group.
    flags: [str], Command flag list. Global flags, available to all commands,
      are in the root command flags list.
  """

  def __init__(self, command, parent):
    super(CoverageCommandNode, self).__init__()
    # _parent is explicitly private so it won't appear in the output.
    self._parent = parent
    if parent is not None:
      name = command.name.replace('_', '-')
      parent[name] = self

    args = command.ai
    # Collect the command specific flags.
    for arg in args.flag_args:
      for name in arg.option_strings:
        if arg.is_hidden:
          continue
        if not name.startswith('--'):
          continue
        if self.IsAncestorFlag(name):
          continue
        self[name] = arg.require_coverage_in_tests

  def IsAncestorFlag(self, flag):
    """Determines if flag is provided by an ancestor command.

    NOTE: This function is used to allow for global flags to be added in at the
          top level but not in subgroups/commands
    Args:
      flag: str, The flag name (no leading '-').

    Returns:
      bool, True if flag provided by an ancestor command, false if not.
    """
    command = self._parent
    while command:
      if flag in command:
        return True
      command = command._parent  # pylint: disable=protected-access
    return False


class CoverageTreeGenerator(walker.Walker):
  """Generates the gcloud static completion CLI tree."""

  def __init__(self, cli=None, branch=None, restrict=None):
    """branch is the command path of the CLI subtree to generate."""
    super(CoverageTreeGenerator, self).__init__(cli=cli, restrict=restrict)
    self._branch = branch

  def Visit(self, node, parent, is_group):
    """Visits each node in the CLI command tree to construct the external rep.

    Args:
      node: group/command CommandCommon info.
      parent: The parent Visit() return value, None at the top level.
      is_group: True if node is a command group.

    Returns:
      The subtree parent value, used here to construct an external rep node.
    """
    return CoverageCommandNode(node, parent)


def OutputCoverageTree(cli, branch=None, out=None, restrict=None):
  """Lists the flag coverage CLI tree as a Python module file.

  Args:
    cli: The CLI.
    branch: The path of the CLI subtree to generate.
    out: The output stream to write to, sys.stdout by default.
    restrict: The paths in the tree that we are allowing the tree to walk under.

  Returns:
    Returns the serialized coverage CLI tree.
  """
  tree = GenerateCoverageTree(cli=cli, branch=branch, restrict=restrict)
  resource_printer.Print(tree, print_format='json', out=out)
  return tree