# Buggy code
def guess_shed_config(self, app, default=None):
    tool_ids = []
    metadata = self.metadata or {}
    for tool in metadata.get("tools", []):
        tool_ids.append(tool.get("guid"))
    for shed_tool_conf_dict in app.toolbox.dynamic_confs(
        include_migrated_tool_conf=True
    ):
        name = shed_tool_conf_dict["config_filename"]
        for elem in shed_tool_conf_dict["config_elems"]:
            if elem.tag == "tool":
                for sub_elem in elem.findall("id"):
                    tool_id = sub_elem.text.strip()
                    if tool_id in tool_ids:
                        self.shed_config_filename = name
                        return shed_tool_conf_dict
            elif elem.tag == "section":
                for tool_elem in elem.findall("tool"):
                    for sub_elem in tool_elem.findall("id"):
                        tool_id = sub_elem.text.strip()
                        if tool_id in tool_ids:
                            self.shed_config_filename = name
                            return shed_tool_conf_dict
    if self.includes_datatypes:
        # We need to search by file paths here, which is less desirable.
        tool_shed = common_util.remove_protocol_and_port_from_tool_shed_url(
            self.tool_shed
        )
        for shed_tool_conf_dict in app.toolbox.dynamic_confs(
            include_migrated_tool_conf=True
        ):
            tool_path = shed_tool_conf_dict["tool_path"]
            relative_path = os.path.join(
                tool_path,
                tool_shed,
                "repos",
                self.owner,
                self.name,
                self.installed_changeset_revision,
            )
            if os.path.exists(relative_path):
                self.shed_config_filename = shed_tool_conf_dict["config_filename"]
                return shed_tool_conf_dict
    return default


# 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 guess_shed_config(self, app, default=None):
    tool_ids = []
    metadata = self.metadata or {}
    for tool in metadata.get("tools", []):
        tool_ids.append(tool.get("guid"))
    for shed_tool_conf_dict in app.toolbox.dynamic_confs(
        include_migrated_tool_conf=True
    ):
        name = shed_tool_conf_dict["config_filename"]
        for elem in shed_tool_conf_dict["config_elems"]:
            if elem.tag == "tool":
                for sub_elem in elem.findall("id"):
                    tool_id = sub_elem.text.strip()
                    if tool_id in tool_ids:
                        self.shed_config_filename = name
                        return shed_tool_conf_dict
            elif elem.tag == "section":
                for tool_elem in elem.findall("tool"):
                    for sub_elem in tool_elem.findall("id"):
                        tool_id = sub_elem.text.strip()
                        if tool_id in tool_ids:
                            self.shed_config_filename = name
                            return shed_tool_conf_dict
    if self.includes_datatypes or self.includes_data_managers:
        # We need to search by file paths here, which is less desirable.
        tool_shed = common_util.remove_protocol_and_port_from_tool_shed_url(
            self.tool_shed
        )
        for shed_tool_conf_dict in app.toolbox.dynamic_confs(
            include_migrated_tool_conf=True
        ):
            tool_path = shed_tool_conf_dict["tool_path"]
            relative_path = os.path.join(
                tool_path,
                tool_shed,
                "repos",
                self.owner,
                self.name,
                self.installed_changeset_revision,
            )
            if os.path.exists(relative_path):
                self.shed_config_filename = shed_tool_conf_dict["config_filename"]
                return shed_tool_conf_dict
    return default