Beispiel #1
0
    def fgrep(self, path: Text,
              literal: bytes) -> Sequence[jobs_pb2.BufferReference]:
        """Greps for given content on the specified path.

    Args:
      path: A path to a file to be searched.
      literal: A literal expression on search for.

    Returns:
      A list of buffer references to the matched content.
    """
        args = flows_pb2.FileFinderArgs()
        args.paths.append(path)
        args.pathtype = self._path_type

        cond = args.conditions.add()
        cond.condition_type = \
          flows_pb2.FileFinderCondition.Type.CONTENTS_LITERAL_MATCH
        cond.contents_literal_match.mode = \
          flows_pb2.FileFinderContentsLiteralMatchCondition.Mode.ALL_HITS
        cond.contents_literal_match.literal = literal

        args.action.action_type = flows_pb2.FileFinderAction.Action.STAT

        try:
            ff = self._client.CreateFlow(name='FileFinder', args=args)
        except api_errors.AccessForbiddenError as e:
            raise errors.ApprovalMissingError(self.id, e)

        _timeout.await_flow(ff)

        results = [_first_match(result.payload) for result in ff.ListResults()]
        return representer.BufferReferenceList(results)
Beispiel #2
0
  def grep(self, path,
           pattern):
    """Greps for given content on the specified path.

    Args:
      path: A path to a file to be searched.
      pattern: A regular expression on search for.

    Returns:
      A list of buffer references to the matched content.
    """
    args = flows_pb2.FileFinderArgs()
    args.paths.append(path)
    args.pathtype = self._path_type

    cond = args.conditions.add()
    cond.condition_type = \
      flows_pb2.FileFinderCondition.Type.CONTENTS_REGEX_MATCH
    cond.contents_regex_match.mode = \
      flows_pb2.FileFinderContentsRegexMatchCondition.ALL_HITS
    cond.contents_regex_match.regex = pattern

    args.action.action_type = flows_pb2.FileFinderAction.Action.STAT

    try:
      ff = self._client.CreateFlow(name='FileFinder', args=args)
    except api_errors.AccessForbiddenError as e:
      raise errors.ApprovalMissingError(self.id, e)

    _timeout.await_flow(ff)
    return representer.BufferReferenceList(
        [list(_.payload.matches)[0] for _ in ff.ListResults()])
Beispiel #3
0
    def testMultipleItems(self):
        ref1 = jobs_pb2.BufferReference()
        ref1.pathspec.path = '/foo/bar'
        ref1.offset = 42
        ref1.length = 6
        ref1.data = b'foobar'

        ref2 = jobs_pb2.BufferReference()
        ref2.pathspec.path = '/quux'
        ref2.offset = 42
        ref2.length = 4
        ref2.data = b'quux'

        brs = representer.BufferReferenceList([ref1, ref2])

        out = io.StringIO()
        brs._repr_pretty_(pretty.PrettyPrinter(out), cycle=False)

        expected = """
/foo/bar:42-48: b\'foobar\'
/quux:42-46: b\'quux\'
"""
        self.assertEqual(out.getvalue(), expected)
Beispiel #4
0
    def testSlice(self):
        ref1 = jobs_pb2.BufferReference()
        ref2 = jobs_pb2.BufferReference()

        brs = representer.BufferReferenceList([ref1, ref2])
        self.assertIsInstance(brs[:1], representer.BufferReferenceList)
Beispiel #5
0
    def testCycle(self):
        brs = representer.BufferReferenceList([])

        out = io.StringIO()
        with self.assertRaises(AssertionError):
            brs._repr_pretty_(pretty.PrettyPrinter(out), cycle=True)
Beispiel #6
0
    def testEmptyResults(self):
        brs = representer.BufferReferenceList([])

        out = io.StringIO()
        brs._repr_pretty_(pretty.PrettyPrinter(out), cycle=False)
        self.assertEqual(out.getvalue(), 'No results.')