Example #1
0
    def _OnData(self, unused_websocket_app, binary_data, opcode,
                unused_finished):
        """Receive a single message from the server.

    Args:
      binary_data: str binary data of proto
      opcode: int signal value for whether data is binary or string
      unused_finished: bool whether this is the final message in a multi-part
                       sequence
    """
        if (opcode not in (websocket.ABNF.OPCODE_CONT,
                           websocket.ABNF.OPCODE_BINARY)):
            log.warning('Unexpected WebSocket opcode [%r].', opcode)
            return

        subprotocol_tag, data = utils.ExtractSubprotocolData(binary_data)
        if data is not None:
            if subprotocol_tag == utils.SUBPROTOCOL_TAG_DATA:
                try:
                    self._data_handler_callback(data)
                except (EnvironmentError, socket.error):
                    log.exception('Error from WebSocket data handler callback')
                    self.Close()
                    raise
            elif subprotocol_tag == utils.SUBPROTOCOL_TAG_CONNECT_SUCCESS_SID:
                self._connection_sid = data
            else:
                log.warning(
                    'Unexpected subprotocol type [%r] with data length [%d].',
                    subprotocol_tag, len(data))
Example #2
0
 def _HandleNewConnection(self, conn, socket_address):
     try:
         self._RunReceiveLocalData(conn, socket_address)
     except EnvironmentError as e:
         log.info('Socket error [%s] while receiving from client.', str(e))
     except:  # pylint: disable=bare-except
         log.exception('Error while receiving from client.')
Example #3
0
 def _HandleNewConnection(self, conn, socket_address):
     try:
         user_agent = transport.MakeUserAgentString()
         self._RunReceiveLocalData(conn, repr(socket_address), user_agent)
     except EnvironmentError as e:
         log.info('Socket error [%s] while receiving from client.',
                  six.text_type(e))
     except:  # pylint: disable=bare-except
         log.exception('Error while receiving from client.')
Example #4
0
    def __LoadCLIFromGroups(self):
        """Load the CLI from a command directory.

    Returns:
      CLI, The generated CLI tool.
    """
        top_group = self.__LoadGroup(self.__command_root_directory,
                                     None,
                                     allow_non_existing_modules=False)
        registered_groups = {None: top_group}
        self.__RegisterAllSubGroups(top_group, registered_groups)

        for module_dot_path, module_dir in self.__modules:
            try:
                match = re.match(CLILoader.PATH_RE, module_dot_path)
                root, name = match.group(1, 2)
                parent_group = registered_groups.get(root)
                exception_if_present = None
                if not parent_group:
                    exception_if_present = backend.LayoutException(
                        'Root [{root}] for command group [{group}] does not exist.'
                        .format(root=root, group=name))

                path_list = module_dot_path.split('.')
                group = self.__LoadGroup(
                    module_dir,
                    parent_group,
                    module_path=path_list,
                    allow_non_existing_modules=self.
                    __allow_non_existing_modules,
                    exception_if_present=exception_if_present,
                    top_group=top_group)
                if group:
                    self.__RegisterAllSubGroups(group, registered_groups)
                    parent_group.AddSubGroup(group)
            except backend.CommandLoadFailure as e:
                log.exception(e)

        parser = top_group.Parser()
        entry_point = frontend.UnboundCommandGroup(None, top_group)
        cli = self.__MakeCLI(entry_point, parser, top_group)

        top_group.MakeShellActions(cli)

        return cli
Example #5
0
  def __LoadCLIFromGroups(self):
    """Load the CLI from a command directory.

    Returns:
      CLI, The generated CLI tool.
    """
    top_group = self.__LoadGroup(self.__command_root_directory,
                                 None, allow_non_existing_modules=False)
    registered_groups = {None: top_group}
    self.__RegisterAllSubGroups(top_group, registered_groups)

    for module_dot_path, module_dir in self.__modules:
      try:
        match = re.match(CLILoader.PATH_RE, module_dot_path)
        root, name = match.group(1, 2)
        parent_group = registered_groups.get(root)
        exception_if_present = None
        if not parent_group:
          exception_if_present = backend.LayoutException(
              'Root [{root}] for command group [{group}] does not exist.'
              .format(root=root, group=name))

        path_list = module_dot_path.split('.')
        group = self.__LoadGroup(
            module_dir, parent_group, module_path=path_list,
            allow_non_existing_modules=self.__allow_non_existing_modules,
            exception_if_present=exception_if_present, top_group=top_group)
        if group:
          self.__RegisterAllSubGroups(group, registered_groups)
          parent_group.AddSubGroup(group)
      except backend.CommandLoadFailure as e:
        log.exception(e)

    parser = top_group.Parser()
    entry_point = frontend.UnboundCommandGroup(None, top_group)
    cli = self.__MakeCLI(entry_point, parser, top_group)

    top_group.MakeShellActions(cli)

    return cli
Example #6
0
  def __LoadCLIFromGroups(self):
    """Load the CLI from a command directory.

    Returns:
      CLI, The generated CLI tool.
    """
    top_group = self.__LoadTopGroup(
        self.__GetGroupInfo(
            module_directory=self.__command_root_directory, module_path=None,
            allow_non_existing_modules=False, exception_if_present=None,
            is_top_group=True))
    self.__AddBuiltinGlobalFlags(top_group)

    for module_dot_path, module_dir in self.__modules:
      try:
        match = CLILoader.PATH_RE.match(module_dot_path)
        root, name = match.group(1, 2)
        parent_group = self.__FindParentGroup(top_group, root)
        exception_if_present = None
        if not parent_group:
          exception_if_present = backend.LayoutException(
              'Root [{root}] for command group [{group}] does not exist.'
              .format(root=root, group=name))

        path_list = module_dot_path.split('.')
        group_info = self.__GetGroupInfo(
            module_directory=module_dir, module_path=path_list,
            allow_non_existing_modules=self.__allow_non_existing_modules,
            exception_if_present=exception_if_present, is_top_group=False)

        if group_info:
          parent_group.AddSubGroup(group_info)
      except backend.CommandLoadFailure as e:
        log.exception(e)

    cli = self.__MakeCLI(top_group)

    return cli
Example #7
0
  def Generate(self):
    """Uses the registered information to generate the CLI tool.

    Returns:
      CLI, The generated CLI tool.
    """
    # The root group of the CLI.
    impl_path = self.__ValidateCommandOrGroupInfo(
        self.__command_root_directory, allow_non_existing_modules=False)
    top_group = backend.CommandGroup(
        [impl_path], [self.__name], calliope_base.ReleaseTrack.GA,
        uuid.uuid4().hex, self, None)
    self.__AddBuiltinGlobalFlags(top_group)

    # Sub groups for each alternate release track.
    loaded_release_tracks = dict([(calliope_base.ReleaseTrack.GA, top_group)])
    track_names = set(track.prefix for track in self.__release_tracks.keys())
    for track, (module_dir, component) in self.__release_tracks.iteritems():
      impl_path = self.__ValidateCommandOrGroupInfo(
          module_dir,
          allow_non_existing_modules=self.__allow_non_existing_modules)
      if impl_path:
        # Add the release track sub group into the top group.
        # pylint: disable=protected-access
        top_group._groups_to_load[track.prefix] = [impl_path]
        # Override the release track because this is specifically a top level
        # release track group.
        track_group = top_group.LoadSubElement(
            track.prefix, allow_empty=True, release_track_override=track)
        # Copy all the root elements of the top group into the release group.
        top_group.CopyAllSubElementsTo(track_group, ignore=track_names)
        loaded_release_tracks[track] = track_group
      elif component:
        self.__missing_components[track.prefix] = component

    # Load the normal set of registered sub groups.
    for module_dot_path, module_dir_path, component in self.__modules:
      is_command = module_dir_path.endswith(_COMMAND_SUFFIX)
      if is_command:
        module_dir_path = module_dir_path[:-len(_COMMAND_SUFFIX)]
      match = CLILoader.PATH_RE.match(module_dot_path)
      root, name = match.group(1, 2)
      try:
        # Mount each registered sub group under each release track that exists.
        for track, track_root_group in loaded_release_tracks.iteritems():
          parent_group = self.__FindParentGroup(track_root_group, root)
          exception_if_present = None
          if not parent_group:
            if track != calliope_base.ReleaseTrack.GA:
              # Don't error mounting sub groups if the parent group can't be
              # found unless this is for the GA group.  The GA should always be
              # there, but for alternate release channels, the parent group
              # might not be enabled for that particular release channel, so it
              # is valid to not exist.
              continue
            exception_if_present = command_loading.LayoutException(
                'Root [{root}] for command group [{group}] does not exist.'
                .format(root=root, group=name))

          cmd_or_grp_name = module_dot_path.split('.')[-1]
          impl_path = self.__ValidateCommandOrGroupInfo(
              module_dir_path,
              allow_non_existing_modules=self.__allow_non_existing_modules,
              exception_if_present=exception_if_present)

          if impl_path:
            # pylint: disable=protected-access
            if is_command:
              parent_group._commands_to_load[cmd_or_grp_name] = [impl_path]
            else:
              parent_group._groups_to_load[cmd_or_grp_name] = [impl_path]
          elif component:
            prefix = track.prefix + '.' if track.prefix else ''
            self.__missing_components[prefix + module_dot_path] = component
      except command_loading.CommandLoadFailure as e:
        log.exception(e)

    cli = self.__MakeCLI(top_group)

    return cli
Example #8
0
  def Generate(self):
    """Uses the registered information to generate the CLI tool.

    Returns:
      CLI, The generated CLI tool.
    """
    # The root group of the CLI.
    top_group = self.__LoadTopGroup(
        self.__GetCommandOrGroupInfo(
            module_dir_path=self.__command_root_directory, name=self.__name,
            release_track=calliope_base.ReleaseTrack.GA,
            allow_non_existing_modules=False, exception_if_present=None))
    self.__AddBuiltinGlobalFlags(top_group)

    # Sub groups for each alternate release track.
    loaded_release_tracks = dict([(calliope_base.ReleaseTrack.GA, top_group)])
    track_names = set(track.prefix for track in self.__release_tracks.keys())
    for track, (module_dir, component) in self.__release_tracks.iteritems():
      group_info = self.__GetCommandOrGroupInfo(
          module_dir_path=module_dir, name=track.prefix, release_track=track,
          allow_non_existing_modules=self.__allow_non_existing_modules,
          exception_if_present=None)
      if group_info:
        # Add the release track sub group into the top group.
        top_group.AddSubGroup(group_info)
        track_group = top_group.LoadSubElement(track.prefix, allow_empty=True)
        # Copy all the root elements of the top group into the release group.
        top_group.CopyAllSubElementsTo(track_group, ignore=track_names)
        loaded_release_tracks[track] = track_group
      elif component:
        self.__missing_components[track.prefix] = component

    # Load the normal set of registered sub groups.
    for module_dot_path, module_dir_path, component in self.__modules:
      is_command = module_dir_path.endswith(_COMMAND_SUFFIX)
      if is_command:
        module_dir_path = module_dir_path[:-len(_COMMAND_SUFFIX)]
      match = CLILoader.PATH_RE.match(module_dot_path)
      root, name = match.group(1, 2)
      try:
        # Mount each registered sub group under each release track that exists.
        for track, track_root_group in loaded_release_tracks.iteritems():
          parent_group = self.__FindParentGroup(track_root_group, root)
          exception_if_present = None
          if not parent_group:
            if track != calliope_base.ReleaseTrack.GA:
              # Don't error mounting sub groups if the parent group can't be
              # found unless this is for the GA group.  The GA should always be
              # there, but for alternate release channels, the parent group
              # might not be enabled for that particular release channel, so it
              # is valid to not exist.
              continue
            exception_if_present = backend.LayoutException(
                'Root [{root}] for command group [{group}] does not exist.'
                .format(root=root, group=name))

          cmd_or_grp_name = module_dot_path.split('.')[-1]
          cmd_or_grp_info = self.__GetCommandOrGroupInfo(
              module_dir_path=module_dir_path,
              name=cmd_or_grp_name,
              release_track=(parent_group.ReleaseTrack(for_help=False)
                             if parent_group else None),
              allow_non_existing_modules=self.__allow_non_existing_modules,
              exception_if_present=exception_if_present)

          if cmd_or_grp_info:
            if is_command:
              parent_group.AddSubCommand(cmd_or_grp_info)
            else:
              parent_group.AddSubGroup(cmd_or_grp_info)
          elif component:
            prefix = track.prefix + '.' if track.prefix else ''
            self.__missing_components[prefix + module_dot_path] = component
      except backend.CommandLoadFailure as e:
        log.exception(e)

    cli = self.__MakeCLI(top_group)

    return cli
Example #9
0
    def Generate(self):
        """Uses the registered information to generate the CLI tool.

    Returns:
      CLI, The generated CLI tool.
    """
        # The root group of the CLI.
        impl_path = self.__ValidateCommandOrGroupInfo(
            self.__command_root_directory, allow_non_existing_modules=False)
        top_group = backend.CommandGroup([impl_path], [self.__name],
                                         calliope_base.ReleaseTrack.GA,
                                         uuid.uuid4().hex, self, None)
        self.__AddBuiltinGlobalFlags(top_group)

        # Sub groups for each alternate release track.
        loaded_release_tracks = dict([(calliope_base.ReleaseTrack.GA,
                                       top_group)])
        track_names = set(track.prefix
                          for track in self.__release_tracks.keys())
        for track, (module_dir,
                    component) in self.__release_tracks.iteritems():
            impl_path = self.__ValidateCommandOrGroupInfo(
                module_dir,
                allow_non_existing_modules=self.__allow_non_existing_modules)
            if impl_path:
                # Add the release track sub group into the top group.
                # pylint: disable=protected-access
                top_group._groups_to_load[track.prefix] = [impl_path]
                # Override the release track because this is specifically a top level
                # release track group.
                track_group = top_group.LoadSubElement(
                    track.prefix,
                    allow_empty=True,
                    release_track_override=track)
                # Copy all the root elements of the top group into the release group.
                top_group.CopyAllSubElementsTo(track_group, ignore=track_names)
                loaded_release_tracks[track] = track_group
            elif component:
                self.__missing_components[track.prefix] = component

        # Load the normal set of registered sub groups.
        for module_dot_path, module_dir_path, component in self.__modules:
            is_command = module_dir_path.endswith(_COMMAND_SUFFIX)
            if is_command:
                module_dir_path = module_dir_path[:-len(_COMMAND_SUFFIX)]
            match = CLILoader.PATH_RE.match(module_dot_path)
            root, name = match.group(1, 2)
            try:
                # Mount each registered sub group under each release track that exists.
                for track, track_root_group in loaded_release_tracks.iteritems(
                ):
                    parent_group = self.__FindParentGroup(
                        track_root_group, root)
                    exception_if_present = None
                    if not parent_group:
                        if track != calliope_base.ReleaseTrack.GA:
                            # Don't error mounting sub groups if the parent group can't be
                            # found unless this is for the GA group.  The GA should always be
                            # there, but for alternate release channels, the parent group
                            # might not be enabled for that particular release channel, so it
                            # is valid to not exist.
                            continue
                        exception_if_present = command_loading.LayoutException(
                            'Root [{root}] for command group [{group}] does not exist.'
                            .format(root=root, group=name))

                    cmd_or_grp_name = module_dot_path.split('.')[-1]
                    impl_path = self.__ValidateCommandOrGroupInfo(
                        module_dir_path,
                        allow_non_existing_modules=self.
                        __allow_non_existing_modules,
                        exception_if_present=exception_if_present)

                    if impl_path:
                        # pylint: disable=protected-access
                        if is_command:
                            parent_group._commands_to_load[cmd_or_grp_name] = [
                                impl_path
                            ]
                        else:
                            parent_group._groups_to_load[cmd_or_grp_name] = [
                                impl_path
                            ]
                    elif component:
                        prefix = track.prefix + '.' if track.prefix else ''
                        self.__missing_components[prefix +
                                                  module_dot_path] = component
            except command_loading.CommandLoadFailure as e:
                log.exception(e)

        cli = self.__MakeCLI(top_group)

        return cli
Example #10
0
  def Generate(self):
    """Uses the registered information to generate the CLI tool.

    Returns:
      CLI, The generated CLI tool.
    """
    # The root group of the CLI.
    top_group = self.__LoadTopGroup(
        self.__GetGroupInfo(
            module_directory=self.__command_root_directory, name=self.__name,
            release_track=calliope_base.ReleaseTrack.GA,
            allow_non_existing_modules=False, exception_if_present=None))
    self.__AddBuiltinGlobalFlags(top_group)

    # Sub groups for each alternate release track.
    loaded_release_tracks = dict([(calliope_base.ReleaseTrack.GA, top_group)])
    track_names = set(track.prefix for track in self.__release_tracks.keys())
    for track, (module_dir, component) in self.__release_tracks.iteritems():
      group_info = self.__GetGroupInfo(
          module_directory=module_dir, name=track.prefix, release_track=track,
          allow_non_existing_modules=self.__allow_non_existing_modules,
          exception_if_present=None)
      if group_info:
        # Add the release track sub group into the top group.
        top_group.AddSubGroup(group_info)
        track_group = top_group.LoadSubElement(track.prefix, allow_empty=True)
        # Copy all the root elements of the top group into the release group.
        top_group.CopyAllSubElementsTo(track_group, ignore=track_names)
        loaded_release_tracks[track] = track_group
      elif component:
        self.__missing_components[track.prefix] = component

    # Load the normal set of registered sub groups.
    for module_dot_path, module_dir, component in self.__modules:
      match = CLILoader.PATH_RE.match(module_dot_path)
      root, name = match.group(1, 2)
      try:
        # Mount each registered sub group under each release track that exists.
        for track, track_root_group in loaded_release_tracks.iteritems():
          parent_group = self.__FindParentGroup(track_root_group, root)
          exception_if_present = None
          if not parent_group:
            if track != calliope_base.ReleaseTrack.GA:
              # Don't error mounting sub groups if the parent group can't be
              # found unless this is for the GA group.  The GA should always be
              # there, but for alternate release channels, the parent group
              # might not be enabled for that particular release channel, so it
              # is valid to not exist.
              continue
            exception_if_present = backend.LayoutException(
                'Root [{root}] for command group [{group}] does not exist.'
                .format(root=root, group=name))

          group_name = module_dot_path.split('.')[-1]
          group_info = self.__GetGroupInfo(
              module_directory=module_dir, name=group_name,
              release_track=(parent_group.ReleaseTrack(for_help=False)
                             if parent_group else None),
              allow_non_existing_modules=self.__allow_non_existing_modules,
              exception_if_present=exception_if_present)

          if group_info:
            parent_group.AddSubGroup(group_info)
          elif component:
            prefix = track.prefix + '.' if track.prefix else ''
            self.__missing_components[prefix + module_dot_path] = component
      except backend.CommandLoadFailure as e:
        log.exception(e)

    cli = self.__MakeCLI(top_group)

    return cli