'Handle a request from a local Galaxy instance.'

@web.expose
def check_for_updates(self, trans, **kwd):

    message = escape(kwd.get('message', ''))
    galaxy_url = common_util.handle_galaxy_url(trans, **kwd)
    name = kwd.get('name', None)
    owner = kwd.get('owner', None)
    changeset_revision = kwd.get('changeset_revision', None)
    repository = repository_util.get_repository_by_name_and_owner(trans.app, name, owner)
    repo = hg_util.get_repo_for_repository(trans.app, repository=repository, repo_path=None, create=False)
    update_to_ctx = hg_util.get_changectx_for_changeset(repo, changeset_revision)
    latest_changeset_revision = changeset_revision
    from_update_manager = kwd.get('from_update_manager', False)
    if from_update_manager:
        update = 'true'
        no_update = 'false'
    elif galaxy_url:
        params = dict(tool_shed_url=web.url_for('/', qualified=True), name=str(repository.name), owner=str(repository.user.username), changeset_revision=changeset_revision)
        pathspec = ['admin_toolshed', 'update_to_changeset_revision']
    else:
        message = ('Unable    to    check    for    updates    due    to    an    invalid    Galaxy    URL:    <b>%s</b>.        ' % galaxy_url)
        message += 'You    may    need    to    enable    third-party    cookies    in    your    browser.        '
        return trans.show_error_message(message)
    if (changeset_revision == repository.tip(trans.app)):
        if from_update_manager:
            return no_update
    else:
        repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(trans.app, trans.security.encode_id(repository.id), changeset_revision)
        if repository_metadata:
            if from_update_manager:
                return no_update
        else:
            update_to_changeset_hash = None
            for changeset in repo.changelog:
                changeset_hash = str(repo.changectx(changeset))
                hg_util.get_changectx_for_changeset(repo, changeset_hash)
                if update_to_changeset_hash:
                    if (changeset_hash == repository.tip(trans.app)):
                        update_to_ctx = hg_util.get_changectx_for_changeset(repo, changeset_hash)
                        latest_changeset_revision = changeset_hash
                        break
                    else:
                        repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(trans.app, trans.security.encode_id(repository.id), changeset_hash)
                        if repository_metadata:
                            update_to_ctx = hg_util.get_changectx_for_changeset(repo, changeset_hash)
                            latest_changeset_revision = changeset_hash
                            break
                        else:
                            update_to_changeset_hash = changeset_hash
                elif (changeset_hash == changeset_revision):
                    update_to_changeset_hash = changeset_hash
            if from_update_manager:
                if (latest_changeset_revision == changeset_revision):
                    return no_update
                return update
    params['latest_changeset_revision'] = str(latest_changeset_revision)
    params['latest_ctx_rev'] = str(update_to_ctx.rev())
    url = util.build_url(galaxy_url, pathspec=pathspec, params=params)
    return trans.response.send_redirect(url)
