'Populate or unpopulate the changeset_revision and toolshed attributes of repository tags.'

def handle_elem(self, elem):

    error_message = ''
    name = elem.get('name')
    owner = elem.get('owner')
    if ((not name) or (not owner)):
        error_message = self.check_tag_attributes(elem)
        return (False, elem, error_message)
    altered = False
    toolshed = elem.get('toolshed')
    changeset_revision = elem.get('changeset_revision')
    prior_installation_required = elem.get('prior_installation_required')
    if ((prior_installation_required is not None) and (not asbool(prior_installation_required))):
        del elem.attrib['prior_installation_required']
    sub_elems = [child_elem for child_elem in list(elem)]
    if (len(sub_elems) > 0):
        sub_elements = odict()
        packages = []
        for sub_elem in sub_elems:
            sub_elem_type = sub_elem.tag
            sub_elem_name = sub_elem.get('name')
            sub_elem_version = sub_elem.get('version')
            if (sub_elem_type and sub_elem_name and sub_elem_version):
                packages.append((sub_elem_name, sub_elem_version))
        sub_elements['packages'] = packages
    else:
        sub_elements = None
    if self.unpopulate:
        if (toolshed or changeset_revision):
            attributes = odict()
            attributes['name'] = name
            attributes['owner'] = owner
            prior_installation_required = elem.get('prior_installation_required')
            if asbool(prior_installation_required):
                attributes['prior_installation_required'] = 'True'
            new_elem = xml_util.create_element('repository', attributes=attributes, sub_elements=sub_elements)
            altered = True
        return (altered, new_elem, error_message)
    if (not toolshed):
        toolshed = str(url_for('/', qualified=True)).rstrip('/')
        elem.attrib['toolshed'] = toolshed
        altered = True
    if (not changeset_revision):
        repository = repository_util.get_repository_by_name_and_owner(self.app, name, owner)
        if repository:
            repo = hg_util.get_repo_for_repository(self.app, repository=repository, repo_path=None, create=False)
            lastest_installable_changeset_revision = metadata_util.get_latest_downloadable_changeset_revision(self.app, repository, repo)
            if (lastest_installable_changeset_revision != hg_util.INITIAL_CHANGELOG_HASH):
                elem.attrib['changeset_revision'] = lastest_installable_changeset_revision
                altered = True
            else:
                error_message = ('Invalid    latest    installable    changeset_revision    %s    ' % str(lastest_installable_changeset_revision))
                error_message += ('retrieved    for    repository    %s    owned    by    %s.        ' % (str(name), str(owner)))
        else:
            error_message = ('Unable    to    locate    repository    with    name    %s    and    owner    %s.        ' % (str(name), str(owner)))
    return (altered, elem, error_message)
