This file is indexed.

/usr/lib/python2.7/dist-packages/googlecloudapis/apitools/base/py/base_cli.py is in python-googlecloudapis 0.9.30+debian1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Base script for generated CLI."""

# pylint: disable=g-import-not-at-top

import atexit
import code
import logging
import os
import readline
import rlcompleter
import sys

from google.apputils import appcommands
import gflags as flags

from googlecloudapis.apitools.base.py import encoding
from googlecloudapis.apitools.base.py import exceptions

__all__ = [
    'ConsoleWithReadline',
    'DeclareBaseFlags',
    'FormatOutput',
    'SetupLogger',
    'run_main',
]


# TODO(user): We should move all the flags for the
# StandardQueryParameters into this file, so that they can be used
# elsewhere easily.

_BASE_FLAGS_DECLARED = False
_OUTPUT_FORMATTER_MAP = {
    'protorpc': lambda x: x,
    'json': encoding.MessageToJson,
}


def DeclareBaseFlags():
  """Declare base flags for all CLIs."""
  # TODO(user): FlagValidators?
  global _BASE_FLAGS_DECLARED
  if _BASE_FLAGS_DECLARED:
    return
  flags.DEFINE_boolean(
      'log_request', False,
      'Log requests.')
  flags.DEFINE_boolean(
      'log_response', False,
      'Log responses.')
  flags.DEFINE_boolean(
      'log_request_response', False,
      'Log requests and responses.')
  flags.DEFINE_enum(
      'output_format',
      'protorpc',
      _OUTPUT_FORMATTER_MAP.viewkeys(),
      'Display format for results.')
  _BASE_FLAGS_DECLARED = True

# NOTE: This is specified here so that it can be read by other files
# without depending on the flag to be registered.
TRACE_HELP = (
    'A tracing token of the form "token:<tokenid>" '
    'to include in api requests.')
FLAGS = flags.FLAGS


def SetupLogger():
  if FLAGS.log_request or FLAGS.log_response or FLAGS.log_request_response:
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)


def FormatOutput(message, output_format=None):
  """Convert the output to the user-specified format."""
  output_format = output_format or FLAGS.output_format
  formatter = _OUTPUT_FORMATTER_MAP.get(FLAGS.output_format)
  if formatter is None:
    raise exceptions.UserError('Unknown output format: %s' % output_format)
  return formatter(message)


class _SmartCompleter(rlcompleter.Completer):

  def _callable_postfix(self, val, word):
    if ('(' in readline.get_line_buffer() or
        not callable(val)):
      return word
    else:
      return word + '('

  def complete(self, text, state):
    if not readline.get_line_buffer().strip():
      if not state:
        return '  '
      else:
        return None
    return rlcompleter.Completer.complete(self, text, state)


class ConsoleWithReadline(code.InteractiveConsole):
  """InteractiveConsole with readline, tab completion, and history."""

  def __init__(self, env, filename='<console>', histfile=None):
    new_locals = dict(env)
    new_locals.update({
        '_SmartCompleter': _SmartCompleter,
        'readline': readline,
        'rlcompleter': rlcompleter,
    })
    code.InteractiveConsole.__init__(self, new_locals, filename)
    readline.parse_and_bind('tab: complete')
    readline.set_completer(_SmartCompleter(new_locals).complete)
    if histfile is not None:
      histfile = os.path.expanduser(histfile)
      if os.path.exists(histfile):
        readline.read_history_file(histfile)
      atexit.register(lambda: readline.write_history_file(histfile))


# pylint: disable=g-bad-name
def run_main():
  """Function to be used as setuptools script entry point.

  Appcommands assumes that it always runs as __main__, but launching
  via a setuptools-generated entry_point breaks this rule. We do some
  trickery here to make sure that appcommands and flags find their
  state where they expect to by faking ourselves as __main__.
  """

  # Put the flags for this module somewhere the flags module will look
  # for them.
  # pylint: disable=protected-access
  new_name = flags._GetMainModule()
  sys.modules[new_name] = sys.modules['__main__']
  for flag in FLAGS.FlagsByModuleDict().get(__name__, []):
    FLAGS._RegisterFlagByModule(new_name, flag)
    for key_flag in FLAGS.KeyFlagsByModuleDict().get(__name__, []):
      FLAGS._RegisterKeyFlagForModule(new_name, key_flag)
  # pylint: enable=protected-access

  # Now set __main__ appropriately so that appcommands will be
  # happy.
  sys.modules['__main__'] = sys.modules[__name__]
  appcommands.Run()
  sys.modules['__main__'] = sys.modules.pop(new_name)


if __name__ == '__main__':
  appcommands.Run()