def _ExtractLLDBInitContent()

in src/TulsiGenerator/Scripts/bootstrap_lldbinit.py [0:0]


  def _ExtractLLDBInitContent(self, lldbinit_path, source_string,
                              add_source_string):
    """Extracts non-Tulsi content in a given lldbinit if needed.

    Args:
      lldbinit_path: Absolute path to the lldbinit we are writing to.
      source_string: String that we wish to write or remove from the lldbinit.
      add_source_string: Boolean indicating whether we intend to write or remove
        the source string.

    Returns:
      (int, [string]): A tuple featuring the status code along with the list
                       of strings representing content to write to lldbinit
                       that does not account for the Tulsi-generated strings.
                       Status code will be 0 if Tulsi-generated strings are
                       not all there. Status code will be 1 if we intend to
                       write Tulsi strings and all strings were accounted for.
                       Alternatively, if we intend to remove the Tulsi strings,
                       the status code will be 1 if none of the strings were
                       found. Status code will be 2 if the lldbinit file could
                       not be found.
    """
    if not os.path.isfile(lldbinit_path):
      return (NOT_FOUND, [])
    content = []
    with open(lldbinit_path) as f:
      ignoring = False

      # Split on the newline. This works as long as the last string isn't
      # suffixed with \n.
      source_lines = source_string.split('\n')

      source_idx = 0

      # If the last line was suffixed with \n, last elements would be length
      # minus 2, accounting for the extra \n.
      source_last = len(source_lines) - 1

      for line in f:

        # For each line found matching source_string, increment the iterator
        # and do not append that line to the list.
        if source_idx <= source_last and source_lines[source_idx] in line:

          # If we intend to write the source string and all lines were found,
          # return an error code with empty content.
          if add_source_string and source_idx == source_last:
            return (NO_CHANGE, [])

          # Increment for each matching line found.
          source_idx += 1
          ignoring = True

        if ignoring:

          # If the last line was found...
          if source_lines[source_last] in line:
            # Stop ignoring lines and continue appending to content.
            ignoring = False
          continue

        # If the line could not be found within source_string, append to the
        # content array.
        content.append(line)

    # If we intend to remove the source string and none of the lines to remove
    # were found, return an error code with empty content.
    if not add_source_string and source_idx == 0:
      return (NO_CHANGE, [])

    return (CHANGE_NEEDED, content)