# Buggy code
def _write_integrated_tool_panel_config_file(self):
    """
    Write the current in-memory version of the integrated_tool_panel.xml file to disk.  Since Galaxy administrators
    use this file to manage the tool panel, we'll not use xml_to_string() since it doesn't write XML quite right.
    """
    tracking_directory = self._integrated_tool_panel_tracking_directory
    if not tracking_directory:
        fd, filename = tempfile.mkstemp()
    else:
        if not os.path.exists(tracking_directory):
            os.makedirs(tracking_directory)
        name = "integrated_tool_panel_%.10f.xml" % time.time()
        filename = os.path.join(tracking_directory, name)
        open_file = open(filename, "w")
        fd = open_file.fileno()
    os.write(fd, '<?xml version="1.0"?>\n')
    os.write(fd, "<toolbox>\n")
    os.write(fd, "    <!--\n    ")
    os.write(
        fd,
        "\n    ".join([l for l in INTEGRATED_TOOL_PANEL_DESCRIPTION.split("\n") if l]),
    )
    os.write(fd, "\n    -->\n")
    for key, item_type, item in self._integrated_tool_panel.panel_items_iter():
        if item:
            if item_type == panel_item_types.TOOL:
                os.write(fd, '    <tool id="%s" />\n' % item.id)
            elif item_type == panel_item_types.WORKFLOW:
                os.write(fd, '    <workflow id="%s" />\n' % item.id)
            elif item_type == panel_item_types.LABEL:
                label_id = item.id or ""
                label_text = item.text or ""
                label_version = item.version or ""
                os.write(
                    fd,
                    '    <label id="%s" text="%s" version="%s" />\n'
                    % (label_id, label_text, label_version),
                )
            elif item_type == panel_item_types.SECTION:
                section_id = item.id or ""
                section_name = item.name or ""
                section_version = item.version or ""
                os.write(
                    fd,
                    '    <section id="%s" name="%s" version="%s">\n'
                    % (section_id, section_name, section_version),
                )
                for (
                    section_key,
                    section_item_type,
                    section_item,
                ) in item.panel_items_iter():
                    if section_item_type == panel_item_types.TOOL:
                        if section_item:
                            os.write(fd, '        <tool id="%s" />\n' % section_item.id)
                    elif section_item_type == panel_item_types.WORKFLOW:
                        if section_item:
                            os.write(
                                fd, '        <workflow id="%s" />\n' % section_item.id
                            )
                    elif section_item_type == panel_item_types.LABEL:
                        if section_item:
                            label_id = section_item.id or ""
                            label_text = section_item.text or ""
                            label_version = section_item.version or ""
                            os.write(
                                fd,
                                '        <label id="%s" text="%s" version="%s" />\n'
                                % (label_id, label_text, label_version),
                            )
                os.write(fd, "    </section>\n")
    os.write(fd, "</toolbox>\n")
    os.close(fd)
    destination = os.path.abspath(self._integrated_tool_panel_config)
    if tracking_directory:
        open(filename + ".stack", "w").write("".join(traceback.format_stack()))
        shutil.copy(filename, filename + ".copy")
        filename = filename + ".copy"
    shutil.move(filename, destination)
    os.chmod(self._integrated_tool_panel_config, 0o644)


# Traceback (ParseError)
...
galaxy.jobs DEBUG 2016-10-24 14:39:57,371 Loading job configuration from ./config/job_conf.xml
galaxy.jobs DEBUG 2016-10-24 14:39:57,372 Read definition for handler 'main'
galaxy.jobs INFO 2016-10-24 14:39:57,373 Setting <handlers> default to child with id 'main'
galaxy.jobs DEBUG 2016-10-24 14:39:57,374 <destinations> default set to child with id or tag 'all.q'
galaxy.jobs DEBUG 2016-10-24 14:39:57,374 Done loading job configuration
beaker.container DEBUG 2016-10-24 14:39:58,124 data file ./database/citations/data/container_file/4/48/48e563f148dc04d8b31c94878c138019862e580d.cache
Traceback (most recent call last):
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/webapps/galaxy/buildapp.py", line 63, in paste_app_factory
app = galaxy.app.UniverseApplication( global_conf=global_conf, **kwargs )
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/app.py", line 93, in __init__
self._configure_toolbox()
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/config.py", line 775, in _configure_toolbox
self.reload_toolbox()
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/config.py", line 761, in reload_toolbox
self.toolbox = tools.ToolBox( tool_configs, self.config.tool_path, self )
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/tools/__init__.py", line 111, in __init__
app=app,
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/tools/toolbox/base.py", line 1051, in __init__
super(BaseGalaxyToolBox, self).__init__(config_filenames, tool_root_dir, app)
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/tools/toolbox/base.py", line 71, in __init__
self._init_integrated_tool_panel( app.config )
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/tools/toolbox/integrated_panel.py", line 36, in _init_integrated_tool_panel
self._load_integrated_tool_panel_keys()
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/tools/toolbox/base.py", line 355, in _load_integrated_tool_panel_keys
tree = parse_xml( self._integrated_tool_panel_config )
File "/mnt/shared/galaxy/galaxy-dist/lib/galaxy/util/__init__.py", line 187, in parse_xml
root = tree.parse( fname, parser=ElementTree.XMLParser( target=DoctypeSafeCallbackTarget() ) )
File "/mnt/shared/galaxy/apps/python/2.7.11/lib/python2.7/xml/etree/ElementTree.py", line 656, in parse
parser.feed(data)
File "/mnt/shared/galaxy/apps/python/2.7.11/lib/python2.7/xml/etree/ElementTree.py", line 1642, in feed
self._raiseerror(v)
File "/mnt/shared/galaxy/apps/python/2.7.11/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
raise err
ParseError: not well-formed (invalid token): line 164, column 40
Removing PID file paster.pid

# Fixed code
def _write_integrated_tool_panel_config_file(self):
    """
    Write the current in-memory version of the integrated_tool_panel.xml file to disk.  Since Galaxy administrators
    use this file to manage the tool panel, we'll not use xml_to_string() since it doesn't write XML quite right.
    """
    tracking_directory = self._integrated_tool_panel_tracking_directory
    if not tracking_directory:
        fd, filename = tempfile.mkstemp()
    else:
        if not os.path.exists(tracking_directory):
            os.makedirs(tracking_directory)
        name = "integrated_tool_panel_%.10f.xml" % time.time()
        filename = os.path.join(tracking_directory, name)
        open_file = open(filename, "w")
        fd = open_file.fileno()
    os.write(fd, '<?xml version="1.0"?>\n')
    os.write(fd, "<toolbox>\n")
    os.write(fd, "    <!--\n    ")
    os.write(
        fd,
        "\n    ".join([l for l in INTEGRATED_TOOL_PANEL_DESCRIPTION.split("\n") if l]),
    )
    os.write(fd, "\n    -->\n")
    for key, item_type, item in self._integrated_tool_panel.panel_items_iter():
        if item:
            if item_type == panel_item_types.TOOL:
                os.write(fd, '    <tool id="%s" />\n' % item.id)
            elif item_type == panel_item_types.WORKFLOW:
                os.write(fd, '    <workflow id="%s" />\n' % item.id)
            elif item_type == panel_item_types.LABEL:
                label_id = item.id or ""
                label_text = item.text or ""
                label_version = item.version or ""
                os.write(
                    fd,
                    '    <label id="%s" text="%s" version="%s" />\n'
                    % (label_id, label_text, label_version),
                )
            elif item_type == panel_item_types.SECTION:
                section_id = item.id or ""
                section_name = item.name or ""
                section_version = item.version or ""
                os.write(
                    fd,
                    '    <section id="%s" name="%s" version="%s">\n'
                    % (escape(section_id), escape(section_name), section_version),
                )
                for (
                    section_key,
                    section_item_type,
                    section_item,
                ) in item.panel_items_iter():
                    if section_item_type == panel_item_types.TOOL:
                        if section_item:
                            os.write(fd, '        <tool id="%s" />\n' % section_item.id)
                    elif section_item_type == panel_item_types.WORKFLOW:
                        if section_item:
                            os.write(
                                fd, '        <workflow id="%s" />\n' % section_item.id
                            )
                    elif section_item_type == panel_item_types.LABEL:
                        if section_item:
                            label_id = section_item.id or ""
                            label_text = section_item.text or ""
                            label_version = section_item.version or ""
                            os.write(
                                fd,
                                '        <label id="%s" text="%s" version="%s" />\n'
                                % (label_id, label_text, label_version),
                            )
                os.write(fd, "    </section>\n")
    os.write(fd, "</toolbox>\n")
    os.close(fd)
    destination = os.path.abspath(self._integrated_tool_panel_config)
    if tracking_directory:
        open(filename + ".stack", "w").write("".join(traceback.format_stack()))
        shutil.copy(filename, filename + ".copy")
        filename = filename + ".copy"
    shutil.move(filename, destination)
    os.chmod(self._integrated_tool_panel_config, 0o644)