'Reinstall a tool shed repository that has been previously uninstalled, making sure to handle all repository
and tool dependencies of the repository.'

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

    rdim = repository_dependency_manager.RepositoryDependencyInstallManager(trans.app)
    message = escape(kwd.get('message', ''))
    status = kwd.get('status', 'done')
    repository_id = kwd['id']
    tool_shed_repository = repository_util.get_installed_tool_shed_repository(trans.app, repository_id)
    no_changes = kwd.get('no_changes', '')
    no_changes_checked = CheckboxField.is_checked(no_changes)
    install_repository_dependencies = CheckboxField.is_checked(kwd.get('install_repository_dependencies', ''))
    install_tool_dependencies = CheckboxField.is_checked(kwd.get('install_tool_dependencies', ''))
    install_resolver_dependencies = CheckboxField.is_checked(kwd.get('install_resolver_dependencies', ''))
    (shed_tool_conf, tool_path, relative_install_dir) = suc.get_tool_panel_config_tool_path_install_dir(trans.app, tool_shed_repository)
    repository_clone_url = common_util.generate_clone_url_for_installed_repository(trans.app, tool_shed_repository)
    tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(trans.app, tool_shed_repository.tool_shed)
    tool_section = None
    tool_panel_section_id = kwd.get('tool_panel_section_id', '')
    new_tool_panel_section_label = kwd.get('new_tool_panel_section_label', '')
    tool_panel_section_key = None
    tool_panel_section_keys = []
    metadata = tool_shed_repository.metadata
    includes_tool_dependencies = tool_shed_repository.includes_tool_dependencies
    if tool_shed_repository.includes_tools_for_display_in_tool_panel:
        tpm = tool_panel_manager.ToolPanelManager(trans.app)
        (tool_section, tool_panel_section_key) = tpm.handle_tool_panel_selection(toolbox=trans.app.toolbox, metadata=metadata, no_changes_checked=no_changes_checked, tool_panel_section_id=tool_panel_section_id, new_tool_panel_section_label=new_tool_panel_section_label)
        if (tool_section is not None):
            tool_panel_section_id = str(tool_section.id)
    if (tool_shed_repository.status == trans.install_model.ToolShedRepository.installation_status.UNINSTALLED):
        repository_type = suc.get_repository_type_from_tool_shed(trans.app, tool_shed_url, tool_shed_repository.name, tool_shed_repository.owner)
        if (repository_type == rt_util.TOOL_DEPENDENCY_DEFINITION):
            raw_text = suc.get_tool_dependency_definition_metadata_from_tool_shed(trans.app, tool_shed_url, tool_shed_repository.name, tool_shed_repository.owner)
            new_meta = json.loads(raw_text)
            suc.clean_dependency_relationships(trans, new_meta, tool_shed_repository, tool_shed_url)
        tool_shed_repository = repository_util.create_or_update_tool_shed_repository(trans.app, tool_shed_repository.name, tool_shed_repository.description, tool_shed_repository.installed_changeset_revision, tool_shed_repository.ctx_rev, repository_clone_url, metadata, trans.install_model.ToolShedRepository.installation_status.NEW, tool_shed_repository.changeset_revision, tool_shed_repository.owner, tool_shed_repository.dist_to_shed)
    ctx_rev = suc.get_ctx_rev(trans.app, tool_shed_url, tool_shed_repository.name, tool_shed_repository.owner, tool_shed_repository.installed_changeset_revision)
    repo_info_dicts = []
    repo_info_dict = kwd.get('repo_info_dict', None)
    if repo_info_dict:
        if isinstance(repo_info_dict, string_types):
            repo_info_dict = encoding_util.tool_shed_decode(repo_info_dict)
    else:
        if install_repository_dependencies:
            repository_dependencies = rdim.get_repository_dependencies_for_installed_tool_shed_repository(trans.app, tool_shed_repository)
        else:
            repository_dependencies = None
        if metadata:
            tool_dependencies = metadata.get('tool_dependencies', None)
        else:
            tool_dependencies = None
        repo_info_dict = repository_util.create_repo_info_dict(trans.app, repository_clone_url=repository_clone_url, changeset_revision=tool_shed_repository.changeset_revision, ctx_rev=ctx_rev, repository_owner=tool_shed_repository.owner, repository_name=tool_shed_repository.name, tool_dependencies=tool_dependencies, repository_dependencies=repository_dependencies)
    if (repo_info_dict not in repo_info_dicts):
        repo_info_dicts.append(repo_info_dict)
    (created_or_updated_tool_shed_repositories, tool_panel_section_keys, repo_info_dicts, filtered_repo_info_dicts) = rdim.create_repository_dependency_objects(tool_path=tool_path, tool_shed_url=tool_shed_url, repo_info_dicts=repo_info_dicts, install_repository_dependencies=install_repository_dependencies, no_changes_checked=no_changes_checked, tool_panel_section_id=tool_panel_section_id)
    for (index, tps_key) in enumerate(tool_panel_section_keys):
        if (tps_key is None):
            tool_panel_section_keys[index] = tool_panel_section_key
    encoded_repository_ids = [trans.security.encode_id(r.id) for r in created_or_updated_tool_shed_repositories]
    new_kwd = dict(includes_tool_dependencies=includes_tool_dependencies, includes_tools=tool_shed_repository.includes_tools, includes_tools_for_display_in_tool_panel=tool_shed_repository.includes_tools_for_display_in_tool_panel, install_tool_dependencies=install_tool_dependencies, install_resolver_dependencies=install_resolver_dependencies, repo_info_dicts=filtered_repo_info_dicts, message=message, new_tool_panel_section_label=new_tool_panel_section_label, shed_tool_conf=shed_tool_conf, status=status, tool_panel_section_id=tool_panel_section_id, tool_path=tool_path, tool_panel_section_keys=tool_panel_section_keys, tool_shed_repository_ids=encoded_repository_ids, tool_shed_url=tool_shed_url)
    encoded_kwd = encoding_util.tool_shed_encode(new_kwd)
    tsr_ids = [r.id for r in created_or_updated_tool_shed_repositories]
    tool_shed_repositories = []
    for tsr_id in tsr_ids:
        tsr = trans.install_model.context.query(trans.install_model.ToolShedRepository).get(tsr_id)
        tool_shed_repositories.append(tsr)
    clause_list = []
    for tsr_id in tsr_ids:
        clause_list.append((trans.install_model.ToolShedRepository.table.c.id == tsr_id))
    query = trans.install_model.context.current.query(trans.install_model.ToolShedRepository).filter(or_(*clause_list))
    return trans.fill_template('admin/tool_shed_repository/initiate_repository_installation.mako', encoded_kwd=encoded_kwd, query=query, tool_shed_repositories=tool_shed_repositories, initiate_repository_installation_ids=encoded_repository_ids, reinstalling=True)
