# Buggy code
def __initiate_and_install_repositories(
    self, tool_shed_url, repository_revision_dict, repo_info_dicts, install_options
):
    try:
        has_repository_dependencies = repository_revision_dict[
            "has_repository_dependencies"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'has_repository_dependencies'."
        )
    try:
        includes_tools = repository_revision_dict["includes_tools"]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tools'."
        )
    try:
        includes_tool_dependencies = repository_revision_dict[
            "includes_tool_dependencies"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tool_dependencies'."
        )
    try:
        includes_tools_for_display_in_tool_panel = repository_revision_dict[
            "includes_tools_for_display_in_tool_panel"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tools_for_display_in_tool_panel'."
        )
    # Get the information about the Galaxy components (e.g., tool pane section, tool config file, etc) that will contain the repository information.
    install_repository_dependencies = install_options.get(
        "install_repository_dependencies", False
    )
    install_resolver_dependencies = install_options.get(
        "install_resolver_dependencies", False
    )
    install_tool_dependencies = install_options.get("install_tool_dependencies", False)
    if install_tool_dependencies:
        self.__assert_can_install_dependencies()
    new_tool_panel_section_label = install_options.get(
        "new_tool_panel_section_label", ""
    )
    tool_panel_section_mapping = install_options.get("tool_panel_section_mapping", {})
    shed_tool_conf = install_options.get("shed_tool_conf", None)
    if shed_tool_conf:
        # Get the tool_path setting.
        shed_conf_dict = self.tpm.get_shed_tool_conf_dict(shed_tool_conf)
        tool_path = shed_conf_dict["tool_path"]
    else:
        # Don't use migrated_tools_conf.xml.
        try:
            shed_config_dict = self.app.toolbox.dynamic_confs(
                include_migrated_tool_conf=False
            )[0]
        except IndexError:
            raise exceptions.RequestParameterMissingException(
                "Missing required parameter 'shed_tool_conf'."
            )
        shed_tool_conf = shed_config_dict["config_filename"]
        tool_path = shed_config_dict["tool_path"]
    tool_panel_section_id = self.app.toolbox.find_section_id(
        install_options.get("tool_panel_section_id", "")
    )
    # Build the dictionary of information necessary for creating tool_shed_repository database records
    # for each repository being installed.
    installation_dict = dict(
        install_repository_dependencies=install_repository_dependencies,
        new_tool_panel_section_label=new_tool_panel_section_label,
        tool_panel_section_mapping=tool_panel_section_mapping,
        no_changes_checked=False,
        repo_info_dicts=repo_info_dicts,
        tool_panel_section_id=tool_panel_section_id,
        tool_path=tool_path,
        tool_shed_url=tool_shed_url,
    )
    # Create the tool_shed_repository database records and gather additional information for repository installation.
    (
        created_or_updated_tool_shed_repositories,
        tool_panel_section_keys,
        repo_info_dicts,
        filtered_repo_info_dicts,
    ) = self.handle_tool_shed_repositories(installation_dict)
    if created_or_updated_tool_shed_repositories:
        # Build the dictionary of information necessary for installing the repositories.
        installation_dict = dict(
            created_or_updated_tool_shed_repositories=created_or_updated_tool_shed_repositories,
            filtered_repo_info_dicts=filtered_repo_info_dicts,
            has_repository_dependencies=has_repository_dependencies,
            includes_tool_dependencies=includes_tool_dependencies,
            includes_tools=includes_tools,
            includes_tools_for_display_in_tool_panel=includes_tools_for_display_in_tool_panel,
            install_repository_dependencies=install_repository_dependencies,
            install_resolver_dependencies=install_resolver_dependencies,
            install_tool_dependencies=install_tool_dependencies,
            message="",
            new_tool_panel_section_label=new_tool_panel_section_label,
            shed_tool_conf=shed_tool_conf,
            status="done",
            tool_panel_section_id=tool_panel_section_id,
            tool_panel_section_keys=tool_panel_section_keys,
            tool_panel_section_mapping=tool_panel_section_mapping,
            tool_path=tool_path,
            tool_shed_url=tool_shed_url,
        )
        # Prepare the repositories for installation.  Even though this
        # method receives a single combination of tool_shed_url, name,
        # owner and changeset_revision, there may be multiple repositories
        # for installation at this point because repository dependencies
        # may have added additional repositories for installation along
        # with the single specified repository.
        encoded_kwd, query, tool_shed_repositories, encoded_repository_ids = (
            self.initiate_repository_installation(installation_dict)
        )
        # Some repositories may have repository dependencies that are
        # required to be installed before the dependent repository, so
        # we'll order the list of tsr_ids to ensure all repositories
        # install in the required order.
        tsr_ids = [
            self.app.security.encode_id(tool_shed_repository.id)
            for tool_shed_repository in tool_shed_repositories
        ]

        decoded_kwd = dict(
            shed_tool_conf=shed_tool_conf,
            tool_path=tool_path,
            tool_panel_section_keys=tool_panel_section_keys,
            repo_info_dicts=filtered_repo_info_dicts,
            install_resolver_dependencies=install_resolver_dependencies,
            install_tool_dependencies=install_tool_dependencies,
            tool_panel_section_mapping=tool_panel_section_mapping,
        )
        return self.install_repositories(
            tsr_ids, decoded_kwd, reinstalling=False, install_options=install_options
        )


# 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 __initiate_and_install_repositories(
    self, tool_shed_url, repository_revision_dict, repo_info_dicts, install_options
):
    try:
        has_repository_dependencies = repository_revision_dict[
            "has_repository_dependencies"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'has_repository_dependencies'."
        )
    try:
        includes_tools = repository_revision_dict["includes_tools"]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tools'."
        )
    try:
        includes_tool_dependencies = repository_revision_dict[
            "includes_tool_dependencies"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tool_dependencies'."
        )
    try:
        includes_tools_for_display_in_tool_panel = repository_revision_dict[
            "includes_tools_for_display_in_tool_panel"
        ]
    except KeyError:
        raise exceptions.InternalServerError(
            "Tool shed response missing required parameter 'includes_tools_for_display_in_tool_panel'."
        )
    # Get the information about the Galaxy components (e.g., tool panel section, tool config file, etc) that will contain the repository information.
    install_repository_dependencies = install_options.get(
        "install_repository_dependencies", False
    )
    install_resolver_dependencies = install_options.get(
        "install_resolver_dependencies", False
    )
    install_tool_dependencies = install_options.get("install_tool_dependencies", False)
    if install_tool_dependencies:
        self.__assert_can_install_dependencies()
    new_tool_panel_section_label = install_options.get(
        "new_tool_panel_section_label", ""
    )
    tool_panel_section_mapping = install_options.get("tool_panel_section_mapping", {})
    shed_tool_conf = install_options.get("shed_tool_conf", None)
    if shed_tool_conf:
        # Get the tool_path setting.
        shed_conf_dict = self.tpm.get_shed_tool_conf_dict(shed_tool_conf)
        tool_path = shed_conf_dict["tool_path"]
    else:
        # Don't use migrated_tools_conf.xml and prefer shed_tool_config_file.
        try:
            for shed_config_dict in self.app.toolbox.dynamic_confs(
                include_migrated_tool_conf=False
            ):
                if (
                    shed_config_dict.get("config_filename")
                    == self.app.config.shed_tool_config_file
                ):
                    break
            else:
                shed_config_dict = self.app.toolbox.dynamic_confs(
                    include_migrated_tool_conf=False
                )[0]
        except IndexError:
            raise exceptions.RequestParameterMissingException(
                "Missing required parameter 'shed_tool_conf'."
            )
        shed_tool_conf = shed_config_dict["config_filename"]
        tool_path = shed_config_dict["tool_path"]
    tool_panel_section_id = self.app.toolbox.find_section_id(
        install_options.get("tool_panel_section_id", "")
    )
    # Build the dictionary of information necessary for creating tool_shed_repository database records
    # for each repository being installed.
    installation_dict = dict(
        install_repository_dependencies=install_repository_dependencies,
        new_tool_panel_section_label=new_tool_panel_section_label,
        tool_panel_section_mapping=tool_panel_section_mapping,
        no_changes_checked=False,
        repo_info_dicts=repo_info_dicts,
        tool_panel_section_id=tool_panel_section_id,
        tool_path=tool_path,
        tool_shed_url=tool_shed_url,
    )
    # Create the tool_shed_repository database records and gather additional information for repository installation.
    (
        created_or_updated_tool_shed_repositories,
        tool_panel_section_keys,
        repo_info_dicts,
        filtered_repo_info_dicts,
    ) = self.handle_tool_shed_repositories(installation_dict)
    if created_or_updated_tool_shed_repositories:
        # Build the dictionary of information necessary for installing the repositories.
        installation_dict = dict(
            created_or_updated_tool_shed_repositories=created_or_updated_tool_shed_repositories,
            filtered_repo_info_dicts=filtered_repo_info_dicts,
            has_repository_dependencies=has_repository_dependencies,
            includes_tool_dependencies=includes_tool_dependencies,
            includes_tools=includes_tools,
            includes_tools_for_display_in_tool_panel=includes_tools_for_display_in_tool_panel,
            install_repository_dependencies=install_repository_dependencies,
            install_resolver_dependencies=install_resolver_dependencies,
            install_tool_dependencies=install_tool_dependencies,
            message="",
            new_tool_panel_section_label=new_tool_panel_section_label,
            shed_tool_conf=shed_tool_conf,
            status="done",
            tool_panel_section_id=tool_panel_section_id,
            tool_panel_section_keys=tool_panel_section_keys,
            tool_panel_section_mapping=tool_panel_section_mapping,
            tool_path=tool_path,
            tool_shed_url=tool_shed_url,
        )
        # Prepare the repositories for installation.  Even though this
        # method receives a single combination of tool_shed_url, name,
        # owner and changeset_revision, there may be multiple repositories
        # for installation at this point because repository dependencies
        # may have added additional repositories for installation along
        # with the single specified repository.
        encoded_kwd, query, tool_shed_repositories, encoded_repository_ids = (
            self.initiate_repository_installation(installation_dict)
        )
        # Some repositories may have repository dependencies that are
        # required to be installed before the dependent repository, so
        # we'll order the list of tsr_ids to ensure all repositories
        # install in the required order.
        tsr_ids = [
            self.app.security.encode_id(tool_shed_repository.id)
            for tool_shed_repository in tool_shed_repositories
        ]

        decoded_kwd = dict(
            shed_tool_conf=shed_tool_conf,
            tool_path=tool_path,
            tool_panel_section_keys=tool_panel_section_keys,
            repo_info_dicts=filtered_repo_info_dicts,
            install_resolver_dependencies=install_resolver_dependencies,
            install_tool_dependencies=install_tool_dependencies,
            tool_panel_section_mapping=tool_panel_section_mapping,
        )
        return self.install_repositories(
            tsr_ids, decoded_kwd, reinstalling=False, install_options=install_options
        )