# 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'
                    % (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)


# Traceback (OSError)
Exception in thread ToolConfWatcher.thread:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "lib/galaxy/tools/toolbox/watcher.py", line 138, in check
self.reload_callback()
File "lib/galaxy/webapps/galaxy/config_watchers.py", line 24, in <lambda>
self.tool_config_watcher = get_tool_conf_watcher(reload_callback=lambda: reload_toolbox(self.app), tool_cache=self.app.tool_cache)
File "lib/galaxy/queue_worker.py", line 92, in reload_toolbox
_get_new_toolbox(app)
File "lib/galaxy/queue_worker.py", line 111, in _get_new_toolbox
new_toolbox = tools.ToolBox(tool_configs, app.config.tool_path, app)
File "lib/galaxy/tools/__init__.py", line 226, in __init__
app=app,
File "lib/galaxy/tools/toolbox/base.py", line 1061, in __init__
super(BaseGalaxyToolBox, self).__init__(config_filenames, tool_root_dir, app)
File "lib/galaxy/tools/toolbox/base.py", line 87, in __init__
self._save_integrated_tool_panel()
File "lib/galaxy/tools/toolbox/integrated_panel.py", line 46, in _save_integrated_tool_panel
self._write_integrated_tool_panel_config_file()
File "lib/galaxy/tools/toolbox/integrated_panel.py", line 106, in _write_integrated_tool_panel_config_file
os.chmod(self._integrated_tool_panel_config, 0o644)
OSError: [Errno 2] No such file or directory: '/data/users/mvandenb/gx/config/integrated_tool_panel.xml'

# 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)
    try:
        os.chmod(destination, 0o644)
    except OSError:
        # That can happen if multiple threads are simultaneously moving/chmod'ing this file
        # Should be harmless, though this race condition should be avoided.
        pass