'Some repositories may have repository dependencies that are required to be installed before the dependent
repository.  This method will inspect the list of repositories about to be installed and make sure to order
them appropriately.  For each repository about to be installed, if required repositories are not contained
in the list of repositories about to be installed, then they are not considered.  Repository dependency
definitions that contain circular dependencies should not result in an infinite loop, but obviously prior
installation will not be handled for one or more of the repositories that require prior installation.  This
process is similar to the process used when installing tool shed repositories, but does not handle managing
tool panel sections and other components since repository dependency definitions contained in tool shed
repositories with migrated tools must never define a relationship to a repository dependency that contains
a tool.'

def order_repositories_for_installation(self, tool_shed_repositories, repository_dependencies_dict):

    ordered_tool_shed_repositories = []
    ordered_tsr_ids = []
    processed_tsr_ids = []
    prior_install_required_dict = self.get_prior_install_required_dict(tool_shed_repositories, repository_dependencies_dict)
    while (len(processed_tsr_ids) != len(prior_install_required_dict.keys())):
        tsr_id = suc.get_next_prior_import_or_install_required_dict_entry(prior_install_required_dict, processed_tsr_ids)
        processed_tsr_ids.append(tsr_id)
        if (tsr_id not in ordered_tsr_ids):
            prior_install_required_ids = prior_install_required_dict[tsr_id]
            for prior_install_required_id in prior_install_required_ids:
                if (prior_install_required_id not in ordered_tsr_ids):
                    ordered_tsr_ids.append(prior_install_required_id)
            ordered_tsr_ids.append(tsr_id)
    for ordered_tsr_id in ordered_tsr_ids:
        for tool_shed_repository in tool_shed_repositories:
            if (tool_shed_repository.id == ordered_tsr_id):
                ordered_tool_shed_repositories.append(tool_shed_repository)
                break
    return ordered_tool_shed_repositories
