'Process the received repository_elem which is a <repository> tag either from a
repository_dependencies.xml file or a tool_dependencies.xml file.  If the former,
we\'re generating repository dependencies metadata for a repository in the Tool Shed.
If the latter, we\'re generating package dependency metadata within Galaxy or the
Tool Shed.'

def handle_repository_elem(self, repository_elem, only_if_compiling_contained_td=False):

    is_valid = True
    error_message = ''
    toolshed = repository_elem.get('toolshed', None)
    name = repository_elem.get('name', None)
    owner = repository_elem.get('owner', None)
    changeset_revision = repository_elem.get('changeset_revision', None)
    prior_installation_required = str(repository_elem.get('prior_installation_required', False))
    repository_dependency_tup = [toolshed, name, owner, changeset_revision, prior_installation_required, str(only_if_compiling_contained_td)]
    if (self.app.name == 'galaxy'):
        if self.updating_installed_repository:
            pass
        elif ((toolshed is None) or (name is None) or (owner is None) or (changeset_revision is None)):
            error_message = 'Installation    encountered    an    invalid    repository    dependency    definition:\n'
            error_message += xml_util.xml_to_string(repository_elem, use_indent=True)
            log.error(error_message)
            return (repository_dependency_tup, False, error_message)
    if (not toolshed):
        toolshed = str(url_for('/', qualified=True)).rstrip('/')
        repository_dependency_tup[0] = toolshed
    user = None
    repository = None
    toolshed = common_util.remove_protocol_from_tool_shed_url(toolshed)
    if (self.app.name == 'galaxy'):
        repository = repository_util.get_repository_for_dependency_relationship(self.app, toolshed, name, owner, changeset_revision)
        if repository:
            return (repository_dependency_tup, is_valid, error_message)
        else:
            text = metadata_util.get_updated_changeset_revisions_from_tool_shed(self.app, toolshed, name, owner, changeset_revision)
            if text:
                updated_changeset_revisions = util.listify(text)
                for updated_changeset_revision in updated_changeset_revisions:
                    repository = repository_util.get_repository_for_dependency_relationship(self.app, toolshed, name, owner, updated_changeset_revision)
                    if repository:
                        return (repository_dependency_tup, is_valid, error_message)
                    if self.updating_installed_repository:
                        return (repository_dependency_tup, is_valid, error_message)
            if self.updating_installed_repository:
                return (repository_dependency_tup, is_valid, error_message)
            if (not only_if_compiling_contained_td):
                error_message = ('Ignoring    invalid    repository    dependency    definition    for    tool    shed    %s,    name    %s,    owner    %s,    ' % (toolshed, name, owner))
                error_message += ('changeset    revision    %s.' % changeset_revision)
                log.debug(error_message)
                is_valid = False
                return (repository_dependency_tup, is_valid, error_message)
    elif suc.tool_shed_is_this_tool_shed(toolshed):
        try:
            user = self.sa_session.query(self.app.model.User).filter((self.app.model.User.table.c.username == owner)).one()
        except Exception:
            error_message = ('Ignoring    repository    dependency    definition    for    tool    shed    %s,    name    %s,    owner    %s,    ' % (toolshed, name, owner))
            error_message += ('changeset    revision    %s    because    the    owner    is    invalid.' % changeset_revision)
            log.debug(error_message)
            is_valid = False
            return (repository_dependency_tup, is_valid, error_message)
        try:
            repository = self.sa_session.query(self.app.model.Repository).filter(and_((self.app.model.Repository.table.c.name == name), (self.app.model.Repository.table.c.user_id == user.id))).one()
        except:
            error_message = ('Ignoring    repository    dependency    definition    for    tool    shed    %s,    name    %s,    owner    %s,    ' % (toolshed, name, owner))
            error_message += ('changeset    revision    %s    because    the    name    is    invalid.        ' % changeset_revision)
            log.debug(error_message)
            is_valid = False
            return (repository_dependency_tup, is_valid, error_message)
        repo = hg_util.get_repo_for_repository(self.app, repository=repository, repo_path=None, create=False)
        tip_ctx = repo.changectx(repo.changelog.tip())
        if ((changeset_revision is None) and (tip_ctx.rev() >= 0)):
            changeset_revision = str(tip_ctx)
            repository_dependency_tup = [toolshed, name, owner, changeset_revision, prior_installation_required, str(only_if_compiling_contained_td)]
            return (repository_dependency_tup, is_valid, error_message)
        else:
            found = False
            for changeset in repo.changelog:
                changeset_hash = str(repo.changectx(changeset))
                if (changeset_hash == changeset_revision):
                    found = True
                    break
            if (not found):
                error_message = ('Ignoring    repository    dependency    definition    for    tool    shed    %s,    name    %s,    owner    %s,    ' % (toolshed, name, owner))
                error_message += ('changeset    revision    %s    because    the    changeset    revision    is    invalid.        ' % changeset_revision)
                log.debug(error_message)
                is_valid = False
                return (repository_dependency_tup, is_valid, error_message)
    else:
        error_message = 'Repository    dependencies    are    currently    supported    only    within    the    same    tool    shed.        Ignoring    '
        error_message += ('repository    dependency    definition        for    tool    shed    %s,    name    %s,    owner    %s,    changeset    revision    %s.        ' % (toolshed, name, owner, changeset_revision))
        log.debug(error_message)
        is_valid = False
        return (repository_dependency_tup, is_valid, error_message)
    return (repository_dependency_tup, is_valid, error_message)
