1# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#   http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16def split_stream(stream, fn_process_line, fn_line_outside_chunk):
17  """ Reads the given input stream and splits it into chunks based on
18      information extracted from individual lines.
19
20  Arguments:
21   - fnProcessLine: Called on each line with the text and line number. Must
22     return a triplet, composed of the name of the chunk started on this line,
23     the data extracted, and the name of the architecture this test applies to
24     (or None to indicate that all architectures should run this test).
25   - fnLineOutsideChunk: Called on attempt to attach data prior to creating
26     a chunk.
27  """
28  line_no = 0
29  all_chunks = []
30  current_chunk = None
31
32  for line in stream:
33    line_no += 1
34    line = line.strip()
35    if not line:
36      continue
37
38    # Let the child class process the line and return information about it.
39    # The _processLine method can modify the content of the line (or delete it
40    # entirely) and specify whether it starts a new group.
41    processed_line, new_chunk_name, test_arch = fn_process_line(line, line_no)
42    # Currently, only a full chunk can be specified as architecture-specific.
43    assert test_arch is None or new_chunk_name is not None
44    if new_chunk_name is not None:
45      current_chunk = (new_chunk_name, [], line_no, test_arch)
46      all_chunks.append(current_chunk)
47    if processed_line is not None:
48      if current_chunk is not None:
49        current_chunk[1].append(processed_line)
50      else:
51        fn_line_outside_chunk(line, line_no)
52  return all_chunks
53