def _read_crosstool_or_ctoolchain_proto()

in tools/migration/ctoolchain_comparator.py [0:0]


def _read_crosstool_or_ctoolchain_proto(input_file, toolchain_identifier=None):
  """Reads a proto file and finds the CToolchain with the given identifier."""
  with open(input_file, "r") as f:
    text = f.read()
  crosstool_release = crosstool_config_pb2.CrosstoolRelease()
  c_toolchain = crosstool_config_pb2.CToolchain()
  try:
    text_format.Merge(text, crosstool_release)
    if toolchain_identifier is None:
      print("CROSSTOOL proto needs a 'toolchain_identifier' specified in "
            "order to be able to select the right toolchain for comparison.")
      return None
    toolchain = _find_toolchain(crosstool_release, toolchain_identifier)
    if toolchain is None:
      print(("Cannot find a CToolchain with an identifier '%s' in CROSSTOOL "
             "file") % toolchain_identifier)
      return None
    return toolchain
  except text_format.ParseError as crosstool_error:
    try:
      text_format.Merge(text, c_toolchain)
      if (toolchain_identifier is not None and
          c_toolchain.toolchain_identifier != toolchain_identifier):
        print(("Expected CToolchain with identifier '%s', got CToolchain with "
               "identifier '%s'" % (toolchain_identifier,
                                    c_toolchain.toolchain_identifier)))
        return None
      return c_toolchain
    except text_format.ParseError as toolchain_error:
      print(("Error parsing file '%s':" % input_file))  # pylint: disable=superfluous-parens
      print("Attempt to parse it as a CROSSTOOL proto:")  # pylint: disable=superfluous-parens
      print(crosstool_error)  # pylint: disable=superfluous-parens
      print("Attempt to parse it as a CToolchain proto:")  # pylint: disable=superfluous-parens
      print(toolchain_error)  # pylint: disable=superfluous-parens
      return None