def _RunClientAction(self, action, request, parser_factory): """Runs the client action with the request and parses the result.""" responses = list(action(request)) if parser_factory is None: return responses # parse the responses parsed_responses = [] for response in responses: for parser in parser_factory.SingleResponseParsers(): parsed_responses.extend( parser.ParseResponse(self.knowledge_base, response)) for parser in parser_factory.SingleFileParsers(): precondition.AssertType(response, rdf_client_fs.StatEntry) pathspec = response.pathspec with vfs.VFSOpen(pathspec) as filedesc: parsed_responses.extend( parser.ParseFile(self.knowledge_base, pathspec, filedesc)) for parser in parser_factory.MultiResponseParsers(): parsed_responses.extend( parser.ParseResponses(self.knowledge_base, responses)) for parser in parser_factory.MultiFileParsers(): precondition.AssertIterableType(responses, rdf_client_fs.StatEntry) pathspecs = [response.pathspec for response in responses] with vfs.VFSMultiOpen(pathspecs) as filedescs: parsed_responses.extend( parser.ParseFiles(self.knowledge_base, pathspecs, filedescs)) return parsed_responses
def testProgressCallback(self): with temp.AutoTempFilePath() as temppath: self._Touch(temppath, b"QUUX") pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=temppath) func = mock.MagicMock() with vfs.VFSMultiOpen([pathspec], progress_callback=func) as filedescs: self.assertLen(filedescs, 1) self.assertEqual(filedescs[0].Read(), b"QUUX") self.assertTrue(func.called)
def testMultipleFiles(self): with temp.AutoTempDirPath(remove_non_empty=True) as tempdir: foo_path = os.path.join(tempdir, "foo") bar_path = os.path.join(tempdir, "bar") baz_path = os.path.join(tempdir, "baz") self._Touch(foo_path, b"FOO") self._Touch(bar_path, b"BAR") self._Touch(baz_path, b"BAZ") foo_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=foo_path) bar_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=bar_path) baz_pathspec = rdf_paths.PathSpec( pathtype=rdf_paths.PathSpec.PathType.OS, path=baz_path) pathspecs = [foo_pathspec, bar_pathspec, baz_pathspec] with vfs.VFSMultiOpen(pathspecs) as filedescs: self.assertLen(filedescs, 3) self.assertEqual(filedescs[0].Read(), b"FOO") self.assertEqual(filedescs[1].Read(), b"BAR") self.assertEqual(filedescs[2].Read(), b"BAZ")