def SetupIndexes(app_id, root_path):
  """Ensure that the set of existing composite indexes matches index.yaml.

  Note: this is similar to the algorithm used by the admin console for
  the same purpose.

  Args:
    app_id: Application ID being served.
    root_path: Path to the root of the application.
  """
  index_yaml_file = os.path.join(root_path, 'index.yaml')
  try:
    fh = open(index_yaml_file, 'r')
  except __HOLE__:
    index_yaml_data = None
  else:
    try:
      index_yaml_data = fh.read()
    finally:
      fh.close()

  indexes = []
  if index_yaml_data is not None:
    index_defs = datastore_index.ParseIndexDefinitions(index_yaml_data)
    if index_defs is not None:
      indexes = index_defs.indexes
      if indexes is None:
        indexes = []

  requested_indexes = datastore_index.IndexDefinitionsToProtos(app_id, indexes)

  existing_indexes = datastore_admin.GetIndices(app_id)

  requested = dict((x.definition().Encode(), x) for x in requested_indexes)
  existing = dict((x.definition().Encode(), x) for x in existing_indexes)

  created = 0
  for key, index in requested.iteritems():
    if key not in existing:
      datastore_admin.CreateIndex(index)
      created += 1

  deleted = 0
  for key, index in existing.iteritems():
    if key not in requested:
      datastore_admin.DeleteIndex(index)
      deleted += 1

  if created or deleted:
    logging.info("Created %d and deleted %d index(es); total %d",
                 created, deleted, len(requested))
dataset/ETHPy150Open CollabQ/CollabQ/.google_appengine/google/appengine/tools/dev_appserver_index.py/SetupIndexes