'Inspect the repository dependency hierarchy for a specified repository and attempt to make sure they are all properly installed as well as
each repository\'s tool dependencies.'

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

    message = escape(kwd.get('message', ''))
    status = kwd.get('status', 'done')
    repository_id = kwd.get('id', None)
    if (not repository_id):
        message = ('Invalid    installed    tool    shed    repository    id    %s    received.' % str(repository_id))
        status = 'error'
        return trans.response.send_redirect(web.url_for(controller='admin_toolshed', action='browse_repositories', message=message, status=status))
    tool_shed_repository = repository_util.get_installed_tool_shed_repository(trans.app, repository_id)
    rrm = RepairRepositoryManager(trans.app)
    if kwd.get('repair_repository_button', False):
        encoded_repair_dict = kwd.get('repair_dict', None)
        if encoded_repair_dict:
            repair_dict = encoding_util.tool_shed_decode(encoded_repair_dict)
        else:
            repair_dict = None
        if (not repair_dict):
            repair_dict = rrm.get_repair_dict(tool_shed_repository)
        ordered_tsr_ids = repair_dict.get('ordered_tsr_ids', [])
        ordered_repo_info_dicts = repair_dict.get('ordered_repo_info_dicts', [])
        if (ordered_tsr_ids and ordered_repo_info_dicts):
            repositories_for_repair = []
            for tsr_id in ordered_tsr_ids:
                repository = trans.install_model.context.query(trans.install_model.ToolShedRepository).get(trans.security.decode_id(tsr_id))
                repositories_for_repair.append(repository)
            return self.repair_tool_shed_repositories(trans, rrm, repositories_for_repair, ordered_repo_info_dicts)
    tool_shed_repository = repository_util.get_installed_tool_shed_repository(trans.app, repository_id)
    repair_dict = rrm.get_repair_dict(tool_shed_repository)
    encoded_repair_dict = encoding_util.tool_shed_encode(repair_dict)
    ordered_tsr_ids = repair_dict.get('ordered_tsr_ids', [])
    ordered_repo_info_dicts = repair_dict.get('ordered_repo_info_dicts', [])
    return trans.fill_template('admin/tool_shed_repository/repair_repository.mako', repository=tool_shed_repository, encoded_repair_dict=encoded_repair_dict, repair_dict=repair_dict, message=message, status=status)
