'Install a tool dependency package defined by the XML element elem.  The value of tool_dependencies is
a partial or full list of ToolDependency records associated with the tool_shed_repository.'

def install_package(self, elem, tool_shed_repository, tool_dependencies=None, from_tool_migration_manager=False):

    tag_manager = TagManager(self.app)
    package_name = elem.get('name', None)
    package_version = elem.get('version', None)
    if (tool_dependencies and package_name and package_version):
        tool_dependency = None
        for tool_dependency in tool_dependencies:
            if ((package_name == str(tool_dependency.name)) and (package_version == str(tool_dependency.version))):
                break
        if (tool_dependency is not None):
            for package_elem in elem:
                (tool_dependency, proceed_with_install, actions_elem_tuples) = tag_manager.process_tag_set(tool_shed_repository, tool_dependency, package_elem, package_name, package_version, from_tool_migration_manager=from_tool_migration_manager, tool_dependency_db_records=None)
                if (proceed_with_install and actions_elem_tuples):
                    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)
                    binary_installed = False
                    for actions_elem_tuple in actions_elem_tuples:
                        (in_actions_group, actions_elems) = actions_elem_tuple
                        if in_actions_group:
                            can_install_from_source = False
                            for actions_elem in actions_elems:
                                system = actions_elem.get('os')
                                architecture = actions_elem.get('architecture')
                                if (system and architecture):
                                    if binary_installed:
                                        continue
                                    tool_dependency = self.install_via_fabric(tool_shed_repository, tool_dependency, install_dir, package_name=package_name, actions_elem=actions_elem, action_elem=None)
                                    if (tool_dependency.status == self.install_model.ToolDependency.installation_status.INSTALLED):
                                        binary_installed = True
                                    else:
                                        log.debug(('Error    downloading    binary    for    tool    dependency    %s    version    %s:    %s' % (str(package_name), str(package_version), str(tool_dependency.error_message))))
                                elif (actions_elem.tag == 'actions'):
                                    if (not binary_installed):
                                        installation_directory = tool_dependency.installation_directory(self.app)
                                        if os.path.exists(installation_directory):
                                            installation_directory_contents = os.listdir(installation_directory)
                                            if installation_directory_contents:
                                                (removed, error_message) = tool_dependency_util.remove_tool_dependency(self.app, tool_dependency)
                                                if removed:
                                                    can_install_from_source = True
                                                else:
                                                    log.debug(('Error    removing    old    files    from    installation    directory    %s:    %s' % str(installation_directory, str(error_message))))
                                            else:
                                                can_install_from_source = True
                                        else:
                                            can_install_from_source = True
                                    if can_install_from_source:
                                        log.debug(('Proceeding    with    install    and    compile    recipe    for    tool    dependency    %s.' % str(tool_dependency.name)))
                                        tool_dependency.status = self.install_model.ToolDependency.installation_status.INSTALLING
                                        tool_dependency = self.install_via_fabric(tool_shed_repository, tool_dependency, install_dir, package_name=package_name, actions_elem=actions_elem, action_elem=None)
                                if ((actions_elem.tag == 'action') and (tool_dependency.status != self.install_model.ToolDependency.installation_status.ERROR)):
                                    tool_dependency = self.install_via_fabric(tool_shed_repository, tool_dependency, install_dir, package_name=package_name, actions_elem=None, action_elem=actions_elem)
                        else:
                            tool_dependency = self.install_via_fabric(tool_shed_repository, tool_dependency, install_dir, package_name=package_name, actions_elem=actions_elems, action_elem=None)
                            if (tool_dependency.status != self.install_model.ToolDependency.installation_status.ERROR):
                                log.debug(('Tool    dependency    %s    version    %s    has    been    installed    in    %s.' % (str(package_name), str(package_version), str(install_dir))))
    return tool_dependency
