'Inspect the repository defined by a complex repository dependency definition and take certain steps to
enable installation of the received package name and version to proceed.  The received elem is the
<repository> tag set which defines the complex repository dependency.  The received tool_shed_repository
is the installed tool shed repository for which the tool dependency defined by the received package_name
and package_version is being installed.'

def handle_complex_repository_dependency_for_package(self, elem, package_name, package_version, tool_shed_repository, from_tool_migration_manager=False):

    handled_tool_dependencies = []
    tool_shed_url = elem.attrib['toolshed']
    required_repository_name = elem.attrib['name']
    required_repository_owner = elem.attrib['owner']
    default_required_repository_changeset_revision = elem.attrib['changeset_revision']
    required_repository = repository_util.get_repository_for_dependency_relationship(self.app, tool_shed_url, required_repository_name, required_repository_owner, default_required_repository_changeset_revision)
    tool_shed = common_util.remove_protocol_from_tool_shed_url(tool_shed_url)
    tmp_filename = None
    if required_repository:
        required_repository_changeset_revision = required_repository.installed_changeset_revision
        required_repository_package_install_dir = tool_dependency_util.get_tool_dependency_install_dir(app=self.app, repository_name=required_repository_name, repository_owner=required_repository_owner, repository_changeset_revision=required_repository_changeset_revision, tool_dependency_type='package', tool_dependency_name=package_name, tool_dependency_version=package_version)
        dependent_install_dir = tool_dependency_util.get_tool_dependency_install_dir(app=self.app, repository_name=tool_shed_repository.name, repository_owner=tool_shed_repository.owner, repository_changeset_revision=tool_shed_repository.installed_changeset_revision, tool_dependency_type='package', tool_dependency_name=package_name, tool_dependency_version=package_version)
        if os.path.exists(dependent_install_dir):
            if from_tool_migration_manager:
                can_install_tool_dependency = True
            else:
                (tool_dependency, can_install_tool_dependency) = self.sync_database_with_file_system(self.app, tool_shed_repository, package_name, package_version, dependent_install_dir, tool_dependency_type='package')
                if (not can_install_tool_dependency):
                    log.debug('Tool    dependency    %s    version    %s    cannot    be    installed    (it    was    probably    previously    installed),    so    appending    it    to    the    list    of    handled    tool    dependencies.', str(tool_dependency.name), str(tool_dependency.version))
                    handled_tool_dependencies.append(tool_dependency)
        else:
            can_install_tool_dependency = True
        if can_install_tool_dependency:
            if required_repository.is_deactivated_or_installed:
                if (not os.path.exists(required_repository_package_install_dir)):
                    log.error(('Missing    required    tool    dependency    directory    %s' % str(required_repository_package_install_dir)))
                repo_files_dir = required_repository.repo_files_directory(self.app)
                if (not repo_files_dir):
                    message = ('Unable    to    locate    the    repository    directory    for    revision    %s    of    installed    repository    %s    owned    by    %s.' % (str(required_repository.changeset_revision), str(required_repository.name), str(required_repository.owner)))
                    raise Exception(message)
                tool_dependencies_config = repository_util.get_absolute_path_to_file_in_repository(repo_files_dir, 'tool_dependencies.xml')
                if tool_dependencies_config:
                    config_to_use = tool_dependencies_config
                else:
                    message = ('Unable    to    locate    required    tool_dependencies.xml    file    for    revision    %s    of    installed    repository    %s    owned    by    %s.' % (str(required_repository.changeset_revision), str(required_repository.name), str(required_repository.owner)))
                    raise Exception(message)
            else:
                text = metadata_util.get_updated_changeset_revisions_from_tool_shed(app=self.app, tool_shed_url=tool_shed, name=required_repository_name, owner=required_repository_owner, changeset_revision=required_repository_changeset_revision)
                if text:
                    updated_changeset_revisions = listify(text)
                    required_repository_changeset_revision = updated_changeset_revisions[0]
                tmp_filename = self.create_temporary_tool_dependencies_config(tool_shed, required_repository_name, required_repository_owner, required_repository_changeset_revision)
                config_to_use = tmp_filename
            handled_tool_dependencies = self.create_tool_dependency_with_initialized_env_sh_file(dependent_install_dir=dependent_install_dir, tool_shed_repository=tool_shed_repository, required_repository=required_repository, package_name=package_name, package_version=package_version, tool_dependencies_config=config_to_use)
            self.remove_file(tmp_filename)
    else:
        message = ('Unable    to    locate    required    tool    shed    repository    named    %s    owned    by    %s    with    revision    %s.' % (str(required_repository_name), str(required_repository_owner), str(default_required_repository_changeset_revision)))
        raise Exception(message)
    return handled_tool_dependencies
