Helper for ``build_index()``: Perform some checks to make sure that
  the given package is consistent.

def _check_package(pkg_xml, zipfilename, zf):
  """
  Helper for ``build_index()``: Perform some checks to make sure that
  the given package is consistent.
  """
  # The filename must patch the id given in the XML file.
  uid = os.path.splitext(os.path.split(zipfilename)[1])[0]
  if pkg_xml.get('id') != uid:
    raise ValueError('package identifier mismatch (%s vs %s)' %
             (pkg_xml.get('id'), uid))

  # Zip file must expand to a subdir whose name matches uid.
  if sum( (name!=uid and not name.startswith(uid+'/'))
      for name in zf.namelist() ):
    raise ValueError('Zipfile %s.zip does not expand to a single '
             'subdirectory %s/' % (uid, uid))