def METHOD_NAME(path, message, as_text=False):
  dir_name = os.path.dirname(path)
  generic_utils.mkdir_if_not_exist(dir_name)
  if dir_name:
    os.makedirs(dir_name, exist_ok=True)
  if as_text:
    with open(path, "w") as f:
      f.write(text_format.MessageToString(message))
  else:
    with open(path, "wb") as f:
      f.write(message.SerializeToString())