# Buggy code
def handle_missing_index_file(
    app, tool_path, sample_files, repository_tools_tups, sample_files_copied
):
    """
    Inspect each tool to see if it has any input parameters that are dynamically
    generated select lists that depend on a .loc file.  This method is not called
    from the tool shed, but from Galaxy when a repository is being installed.
    """
    for index, repository_tools_tup in enumerate(repository_tools_tups):
        tup_path, guid, repository_tool = repository_tools_tup
        params_with_missing_index_file = repository_tool.params_with_missing_index_file
        for param in params_with_missing_index_file:
            options = param.options
            missing_file_name = basic_util.strip_path(options.missing_index_file)
            if missing_file_name not in sample_files_copied:
                # The repository must contain the required xxx.loc.sample file.
                for sample_file in sample_files:
                    sample_file_name = basic_util.strip_path(sample_file)
                    if sample_file_name == "%s.sample" % missing_file_name:
                        copy_sample_file(app, os.path.join(tool_path, sample_file))
                        if (
                            options.tool_data_table
                            and options.tool_data_table.missing_index_file
                        ):
                            options.tool_data_table.handle_found_index_file(
                                options.missing_index_file
                            )
                        sample_files_copied.append(options.missing_index_file)
                        break
    return repository_tools_tups, sample_files_copied


# Traceback (IOError)
galaxy.tools.data WARNING 2018-03-06 20:31:23,419 [p:121,w:1,m:0] [uWSGIWorker1Core2] Cannot find index file '/export/galaxy-central/database/files/tmp-toolshed-gmfcr9qbPdD/bowtie2_indices.loc' for tool data table 'bowtie2_indexes'
galaxy.tools.data DEBUG 2018-03-06 20:31:23,419 [p:121,w:1,m:0] [uWSGIWorker1Core2] Loaded tool data table 'bowtie2_indexes' from file '../shed_tools/toolshed.g2.bx.psu.edu/repos/devteam/bowtie2/dc1639b66f12/bowtie2/tool_data_table_conf.xml.sample'
galaxy.tools.data WARNING 2018-03-06 20:31:23,502 [p:121,w:1,m:0] [uWSGIWorker1Core2] Cannot find index file '/export/galaxy-central/database/files/tmp-toolshed-gmfcr9qbPdD/bowtie2_indices.loc' for tool data table 'bowtie2_indexes'
galaxy.tools.data DEBUG 2018-03-06 20:31:23,502 [p:121,w:1,m:0] [uWSGIWorker1Core2] Loading another instance of data table 'bowtie2_indexes' from file '/shed_tools/toolshed.g2.bx.psu.edu/repos/devteam/bowtie2/dc1639b66f12/bowtie2/tool_data_table_conf.xml.sample', attempting to merge content.
galaxy.tools.data DEBUG 2018-03-06 20:31:25,436 [p:121,w:1,m:0] [uWSGIWorker1Core2] Could not parse existing tool data table config, assume no existing elements: [Errno 2] No such file or directory: u'/galaxy-central/tool-data/toolshed.g2.bx.psu.edu/repos/devteam/bowtie2/dc1639b66f12/tool_data_table_conf.xml'
galaxy.tools.data DEBUG 2018-03-06 20:31:25,441 [p:121,w:1,m:0] [uWSGIWorker1Core2] Loaded 0 lines from '/galaxy-central/tool-data/toolshed.g2.bx.psu.edu/repos/devteam/bowtie2/dc1639b66f12/bowtie2_indices.loc' for 'bowtie2_indexes'
galaxy.tools.data DEBUG 2018-03-06 20:31:25,442 [p:121,w:1,m:0] [uWSGIWorker1Core2] Loading another instance of data table 'bowtie2_indexes' from file '/galaxy-central/tool-data/toolshed.g2.bx.psu.edu/repos/devteam/bowtie2/dc1639b66f12/tool_data_table_conf.xml', attempting to merge content.
galaxy.web.framework.decorators ERROR 2018-03-06 20:31:25,532 [p:121,w:1,m:0] [uWSGIWorker1Core2] Uncaught exception in exposed API method:
Traceback (most recent call last):
File "lib/galaxy/web/framework/decorators.py", line 281, in decorator
rval = func(self, trans, *args, **kwargs)
File "lib/galaxy/webapps/galaxy/api/tool_shed_repositories.py", line 531, in install_repository_revision
payload)
File "lib/tool_shed/galaxy_install/install_manager.py", line 700, in install
install_options
File "lib/tool_shed/galaxy_install/install_manager.py", line 799, in __initiate_and_install_repositories
return self.install_repositories(tsr_ids, decoded_kwd, reinstalling=False)
File "lib/tool_shed/galaxy_install/install_manager.py", line 846, in install_repositories
tool_panel_section_mapping=tool_panel_section_mapping)
File "lib/tool_shed/galaxy_install/install_manager.py", line 896, in install_tool_shed_repository
tool_panel_section_mapping=tool_panel_section_mapping)
File "lib/tool_shed/galaxy_install/install_manager.py", line 555, in __handle_repository_contents
sample_files_copied)
File "lib/tool_shed/util/tool_util.py", line 164, in handle_missing_index_file
options.tool_data_table.handle_found_index_file(options.missing_index_file)
File "lib/galaxy/tools/data/__init__.py", line 422, in handle_found_index_file
self.extend_data_with(filename)
File "lib/galaxy/tools/data/__init__.py", line 488, in extend_data_with
self.data.extend(self.parse_file_fields(open(filename), errors=errors, here=here))
IOError: [Errno 2] No such file or directory: '/galaxy/data/location/bowtie2_indices.loc'
127.0.0.1 - - [06/Mar/2018:20:31:14 +0000] "POST /api/tool_shed_repositories/new/install_repository_revision?key=admin HTTP/1.1" 500 - "-" "python-requests/2.18.4"

# Fixed code
def handle_missing_index_file(
    app, tool_path, sample_files, repository_tools_tups, sample_files_copied
):
    """
    Inspect each tool to see if it has any input parameters that are dynamically
    generated select lists that depend on a .loc file.  This method is not called
    from the tool shed, but from Galaxy when a repository is being installed.
    """
    for index, repository_tools_tup in enumerate(repository_tools_tups):
        tup_path, guid, repository_tool = repository_tools_tup
        params_with_missing_index_file = repository_tool.params_with_missing_index_file
        for param in params_with_missing_index_file:
            options = param.options
            missing_file_name = basic_util.strip_path(options.missing_index_file)
            if missing_file_name not in sample_files_copied:
                # The repository must contain the required xxx.loc.sample file.
                for sample_file in sample_files:
                    sample_file_name = basic_util.strip_path(sample_file)
                    if sample_file_name == "%s.sample" % missing_file_name:
                        target_path = copy_sample_file(
                            app, os.path.join(tool_path, sample_file)
                        )
                        if (
                            options.tool_data_table
                            and options.tool_data_table.missing_index_file
                        ):
                            options.tool_data_table.handle_found_index_file(target_path)
                        sample_files_copied.append(target_path)
                        break
    return repository_tools_tups, sample_files_copied