# Buggy code
def install_tool_shed_repository(
    self,
    tool_shed_repository,
    repo_info_dict,
    tool_panel_section_key,
    shed_tool_conf,
    tool_path,
    install_resolver_dependencies,
    install_tool_dependencies,
    reinstalling=False,
    tool_panel_section_mapping={},
    install_options=None,
):
    self.app.install_model.context.flush()
    if tool_panel_section_key:
        _, tool_section = self.app.toolbox.get_section(tool_panel_section_key)
        if tool_section is None:
            log.debug(
                'Invalid tool_panel_section_key "%s" specified.  Tools will be loaded outside of sections in the tool panel.',
                str(tool_panel_section_key),
            )
    else:
        tool_section = None
    if isinstance(repo_info_dict, string_types):
        repo_info_dict = encoding_util.tool_shed_decode(repo_info_dict)
    repo_info_tuple = repo_info_dict[tool_shed_repository.name]
    (
        description,
        repository_clone_url,
        changeset_revision,
        ctx_rev,
        repository_owner,
        repository_dependencies,
        tool_dependencies,
    ) = repo_info_tuple
    if changeset_revision != tool_shed_repository.changeset_revision:
        # This is an update
        tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(
            self.app, tool_shed_repository.tool_shed
        )
        return self.update_tool_shed_repository(
            tool_shed_repository,
            tool_shed_url,
            ctx_rev,
            changeset_revision,
            install_options=install_options,
        )
    # Clone the repository to the configured location.
    self.update_tool_shed_repository_status(
        tool_shed_repository,
        self.install_model.ToolShedRepository.installation_status.CLONING,
    )
    relative_clone_dir = repository_util.generate_tool_shed_repository_install_dir(
        repository_clone_url, tool_shed_repository.installed_changeset_revision
    )
    relative_install_dir = os.path.join(relative_clone_dir, tool_shed_repository.name)
    install_dir = os.path.join(tool_path, relative_install_dir)
    log.info(
        "Cloning repository '%s' at %s:%s",
        repository_clone_url,
        ctx_rev,
        tool_shed_repository.changeset_revision,
    )
    cloned_ok, error_message = hg_util.clone_repository(
        repository_clone_url, os.path.abspath(install_dir), ctx_rev
    )
    if cloned_ok:
        if reinstalling:
            # Since we're reinstalling the repository we need to find the latest changeset revision to
            # which it can be updated.
            changeset_revision_dict = self.app.update_repository_manager.get_update_to_changeset_revision_and_ctx_rev(
                tool_shed_repository
            )
            current_changeset_revision = changeset_revision_dict.get(
                "changeset_revision", None
            )
            current_ctx_rev = changeset_revision_dict.get("ctx_rev", None)
            if current_ctx_rev != ctx_rev:
                repo_path = os.path.abspath(install_dir)
                hg_util.pull_repository(
                    repo_path, repository_clone_url, current_changeset_revision
                )
                hg_util.update_repository(repo_path, ctx_rev=current_ctx_rev)
        self.__handle_repository_contents(
            tool_shed_repository=tool_shed_repository,
            tool_path=tool_path,
            repository_clone_url=repository_clone_url,
            relative_install_dir=relative_install_dir,
            tool_shed=tool_shed_repository.tool_shed,
            tool_section=tool_section,
            shed_tool_conf=shed_tool_conf,
            reinstalling=reinstalling,
            tool_panel_section_mapping=tool_panel_section_mapping,
        )
        metadata = tool_shed_repository.metadata
        if "tools" in metadata and install_resolver_dependencies:
            self.update_tool_shed_repository_status(
                tool_shed_repository,
                self.install_model.ToolShedRepository.installation_status.INSTALLING_TOOL_DEPENDENCIES,
            )
            new_tools = [
                self.app.toolbox._tools_by_id.get(tool_d["guid"], None)
                for tool_d in metadata["tools"]
            ]
            new_requirements = set(
                [tool.requirements.packages for tool in new_tools if tool]
            )
            [self._view.install_dependencies(r) for r in new_requirements]
            dependency_manager = self.app.toolbox.dependency_manager
            if dependency_manager.cached:
                [dependency_manager.build_cache(r) for r in new_requirements]

        if (
            install_tool_dependencies
            and tool_shed_repository.tool_dependencies
            and "tool_dependencies" in metadata
        ):
            work_dir = tempfile.mkdtemp(prefix="tmp-toolshed-itsr")
            # Install tool dependencies.
            self.update_tool_shed_repository_status(
                tool_shed_repository,
                self.install_model.ToolShedRepository.installation_status.INSTALLING_TOOL_DEPENDENCIES,
            )
            # Get the tool_dependencies.xml file from the repository.
            tool_dependencies_config = hg_util.get_config_from_disk(
                "tool_dependencies.xml", install_dir
            )
            itdm = InstallToolDependencyManager(self.app)
            itdm.install_specified_tool_dependencies(
                tool_shed_repository=tool_shed_repository,
                tool_dependencies_config=tool_dependencies_config,
                tool_dependencies=tool_shed_repository.tool_dependencies,
                from_tool_migration_manager=False,
            )
            basic_util.remove_dir(work_dir)
        self.update_tool_shed_repository_status(
            tool_shed_repository,
            self.install_model.ToolShedRepository.installation_status.INSTALLED,
        )
        if self.app.config.manage_dependency_relationships:
            # Add the installed repository and any tool dependencies to the in-memory dictionaries
            # in the installed_repository_manager.
            self.app.installed_repository_manager.handle_repository_install(
                tool_shed_repository
            )
    else:
        # An error occurred while cloning the repository, so reset everything necessary to enable another attempt.
        repository_util.set_repository_attributes(
            self.app,
            tool_shed_repository,
            status=self.install_model.ToolShedRepository.installation_status.ERROR,
            error_message=error_message,
            deleted=False,
            uninstalled=False,
            remove_from_disk=True,
        )


# Traceback (ParseError)
Nov 09 13:22:10 sn04.bi.uni-freiburg.de uwsgi[4107748]: [pid: 4124485|app: 0|req: 9911/42155] 88.66.136.83 () {84 vars in 1575 bytes} [Sat Nov  9 13:22:10 2019] POST /admin_toolshed/repository_installation_status_updates => generated 2 bytes in 23 msecs (HTTP/1.1 200) 3 headers in 96 bytes (1 switches on core 0)
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.datatypes.registry DEBUG 2019-11-09 13:22:11,317 [p:119216,w:4,m:0] [Thread-1] Loaded external metadata tool: __SET_METADATA__
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.tools.special_tools DEBUG 2019-11-09 13:22:11,323 [p:119216,w:4,m:0] [Thread-1] Loaded history import tool: __IMPORT_HISTORY__
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.tools.special_tools DEBUG 2019-11-09 13:22:11,327 [p:119216,w:4,m:0] [Thread-1] Loaded data fetch tool: __DATA_FETCH__
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.tools.special_tools DEBUG 2019-11-09 13:22:11,330 [p:119216,w:4,m:0] [Thread-1] Loaded history export tool: __EXPORT_HISTORY__
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.queue_worker INFO 2019-11-09 13:22:11,332 [p:119216,w:4,m:0] [Thread-1] Queuing async task rebuild_toolbox_search_index for main.web.4.
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.queue_worker DEBUG 2019-11-09 13:22:11,336 [p:119216,w:4,m:0] [Thread-1] Toolbox reload (29743.348 ms)
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.queue_worker INFO 2019-11-09 13:22:11,338 [p:119216,w:4,m:0] [Thread-1] Instance 'main.web.4' received 'reload_toolbox' task, executing now.
Nov 09 13:22:11 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.queue_worker DEBUG 2019-11-09 13:22:11,338 [p:119216,w:4,m:0] [Thread-1] Executing toolbox reload on 'main.web.4'
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3843198]: 115.146.92.55 - - [09/Nov/2019:13:22:11 +0200] "GET /api/jobs/4d16a9b9d521c9b3?key=59001665ead9d0bfd82dad0c0253dda0 HTTP/1.1" 200 - "-" "python-requests/2.22.0"
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3843198]: [pid: 3882852|app: 0|req: 11155/55634] 115.146.92.55 () {70 vars in 992 bytes} [Sat Nov  9 13:22:11 2019] GET /api/jobs/4d16a9b9d521c9b3?key=59001665ead9d0bfd82dad0c0253dda0 => generated 1118 bytes in 88 msecs (HTTP/1.1 200) 2 headers in 110 bytes (1 switches on core 0)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3843198]: galaxy.tools.search DEBUG 2019-11-09 13:22:12,036 [p:228435,w:4,m:0] [Thread-1] Toolbox index finished (29660.792 ms)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.util ERROR 2019-11-09 13:22:12,537 [p:119216,w:4,m:0] [Thread-1] Error parsing file /opt/galaxy/mutable-config/integrated_tool_panel.xml
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: Traceback (most recent call last):
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/util/__init__.py", line 236, in parse_xml
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: root = tree.parse(fname, parser=ElementTree.XMLParser(target=DoctypeSafeCallbackTarget()))
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 657, in parse
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._root = parser.close()
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1654, in close
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._raiseerror(v)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: raise err
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: ParseError: unclosed token: line 10952, column 8
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: galaxy.queue_worker ERROR 2019-11-09 13:22:12,538 [p:119216,w:4,m:0] [Thread-1] Error running control task type: reload_toolbox
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: Traceback (most recent call last):
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/queue_worker.py", line 372, in process_task
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: result = f(self.app, **body['kwargs'])
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/queue_worker.py", line 171, in reload_toolbox
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: _get_new_toolbox(app)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/queue_worker.py", line 188, in _get_new_toolbox
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: new_toolbox = tools.ToolBox(tool_configs, app.config.tool_path, app)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/tools/__init__.py", line 253, in __init__
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: app=app,
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/tools/toolbox/base.py", line 1163, in __init__
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: super(BaseGalaxyToolBox, self).__init__(config_filenames, tool_root_dir, app)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/tools/toolbox/base.py", line 100, in __init__
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._init_integrated_tool_panel(app.config)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/tools/toolbox/integrated_panel.py", line 39, in _init_integrated_tool_panel
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._load_integrated_tool_panel_keys()
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/tools/toolbox/base.py", line 445, in _load_integrated_tool_panel_keys
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: tree = parse_xml(self._integrated_tool_panel_config)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "lib/galaxy/util/__init__.py", line 236, in parse_xml
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: root = tree.parse(fname, parser=ElementTree.XMLParser(target=DoctypeSafeCallbackTarget()))
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 657, in parse
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._root = parser.close()
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1654, in close
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: self._raiseerror(v)
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: raise err
Nov 09 13:22:12 sn04.bi.uni-freiburg.de uwsgi[3191341]: ParseError: unclosed token: line 10952, column 8

# Fixed code
def install_tool_shed_repository(
    self,
    tool_shed_repository,
    repo_info_dict,
    tool_panel_section_key,
    shed_tool_conf,
    tool_path,
    install_resolver_dependencies,
    install_tool_dependencies,
    reinstalling=False,
    tool_panel_section_mapping={},
    install_options=None,
):
    self.app.install_model.context.flush()
    if tool_panel_section_key:
        _, tool_section = self.app.toolbox.get_section(tool_panel_section_key)
        if tool_section is None:
            log.debug(
                'Invalid tool_panel_section_key "%s" specified.  Tools will be loaded outside of sections in the tool panel.',
                str(tool_panel_section_key),
            )
    else:
        tool_section = None
    if isinstance(repo_info_dict, string_types):
        repo_info_dict = encoding_util.tool_shed_decode(repo_info_dict)
    repo_info_tuple = repo_info_dict[tool_shed_repository.name]
    (
        description,
        repository_clone_url,
        changeset_revision,
        ctx_rev,
        repository_owner,
        repository_dependencies,
        tool_dependencies,
    ) = repo_info_tuple
    if changeset_revision != tool_shed_repository.changeset_revision:
        # This is an update
        tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(
            self.app, tool_shed_repository.tool_shed
        )
        return self.update_tool_shed_repository(
            tool_shed_repository,
            tool_shed_url,
            ctx_rev,
            changeset_revision,
            install_options=install_options,
        )
    # Clone the repository to the configured location.
    self.update_tool_shed_repository_status(
        tool_shed_repository,
        self.install_model.ToolShedRepository.installation_status.CLONING,
    )
    relative_clone_dir = repository_util.generate_tool_shed_repository_install_dir(
        repository_clone_url, tool_shed_repository.installed_changeset_revision
    )
    relative_install_dir = os.path.join(relative_clone_dir, tool_shed_repository.name)
    install_dir = os.path.abspath(os.path.join(tool_path, relative_install_dir))
    log.info(
        "Cloning repository '%s' at %s:%s",
        repository_clone_url,
        ctx_rev,
        tool_shed_repository.changeset_revision,
    )
    if os.path.exists(install_dir):
        # May exist from a previous failed install attempt, just try updating instead of cloning.
        hg_util.pull_repository(install_dir, repository_clone_url, ctx_rev)
        hg_util.update_repository(install_dir, ctx_rev)
        cloned_ok = True
    else:
        cloned_ok, error_message = hg_util.clone_repository(
            repository_clone_url, install_dir, ctx_rev
        )
    if cloned_ok:
        if reinstalling:
            # Since we're reinstalling the repository we need to find the latest changeset revision to
            # which it can be updated.
            changeset_revision_dict = self.app.update_repository_manager.get_update_to_changeset_revision_and_ctx_rev(
                tool_shed_repository
            )
            current_changeset_revision = changeset_revision_dict.get(
                "changeset_revision", None
            )
            current_ctx_rev = changeset_revision_dict.get("ctx_rev", None)
            if current_ctx_rev != ctx_rev:
                repo_path = os.path.abspath(install_dir)
                hg_util.pull_repository(
                    repo_path, repository_clone_url, current_changeset_revision
                )
                hg_util.update_repository(repo_path, ctx_rev=current_ctx_rev)
        self.__handle_repository_contents(
            tool_shed_repository=tool_shed_repository,
            tool_path=tool_path,
            repository_clone_url=repository_clone_url,
            relative_install_dir=relative_install_dir,
            tool_shed=tool_shed_repository.tool_shed,
            tool_section=tool_section,
            shed_tool_conf=shed_tool_conf,
            reinstalling=reinstalling,
            tool_panel_section_mapping=tool_panel_section_mapping,
        )
        metadata = tool_shed_repository.metadata
        if "tools" in metadata and install_resolver_dependencies:
            self.update_tool_shed_repository_status(
                tool_shed_repository,
                self.install_model.ToolShedRepository.installation_status.INSTALLING_TOOL_DEPENDENCIES,
            )
            new_tools = [
                self.app.toolbox._tools_by_id.get(tool_d["guid"], None)
                for tool_d in metadata["tools"]
            ]
            new_requirements = set(
                [tool.requirements.packages for tool in new_tools if tool]
            )
            [self._view.install_dependencies(r) for r in new_requirements]
            dependency_manager = self.app.toolbox.dependency_manager
            if dependency_manager.cached:
                [dependency_manager.build_cache(r) for r in new_requirements]

        if (
            install_tool_dependencies
            and tool_shed_repository.tool_dependencies
            and "tool_dependencies" in metadata
        ):
            work_dir = tempfile.mkdtemp(prefix="tmp-toolshed-itsr")
            # Install tool dependencies.
            self.update_tool_shed_repository_status(
                tool_shed_repository,
                self.install_model.ToolShedRepository.installation_status.INSTALLING_TOOL_DEPENDENCIES,
            )
            # Get the tool_dependencies.xml file from the repository.
            tool_dependencies_config = hg_util.get_config_from_disk(
                "tool_dependencies.xml", install_dir
            )
            itdm = InstallToolDependencyManager(self.app)
            itdm.install_specified_tool_dependencies(
                tool_shed_repository=tool_shed_repository,
                tool_dependencies_config=tool_dependencies_config,
                tool_dependencies=tool_shed_repository.tool_dependencies,
                from_tool_migration_manager=False,
            )
            basic_util.remove_dir(work_dir)
        self.update_tool_shed_repository_status(
            tool_shed_repository,
            self.install_model.ToolShedRepository.installation_status.INSTALLED,
        )
        if self.app.config.manage_dependency_relationships:
            # Add the installed repository and any tool dependencies to the in-memory dictionaries
            # in the installed_repository_manager.
            self.app.installed_repository_manager.handle_repository_install(
                tool_shed_repository
            )
    else:
        # An error occurred while cloning the repository, so reset everything necessary to enable another attempt.
        repository_util.set_repository_attributes(
            self.app,
            tool_shed_repository,
            status=self.install_model.ToolShedRepository.installation_status.ERROR,
            error_message=error_message,
            deleted=False,
            uninstalled=False,
            remove_from_disk=True,
        )