# Buggy code
def copy_sample_file(app, filename, dest_path=None):
    """
    Copies a sample file at `filename` to `the dest_path`
    directory and strips the '.sample' extensions from `filename`.
    """
    if dest_path is None:
        dest_path = os.path.abspath(app.config.tool_data_path)
    sample_file_name = basic_util.strip_path(filename)
    copied_file = sample_file_name.rsplit(".sample", 1)[0]
    full_source_path = os.path.abspath(filename)
    full_destination_path = os.path.join(dest_path, sample_file_name)
    # Don't copy a file to itself - not sure how this happens, but sometimes it does...
    if full_source_path != full_destination_path:
        # It's ok to overwrite the .sample version of the file.
        shutil.copy(full_source_path, full_destination_path)
    # Only create the .loc file if it does not yet exist.  We don't overwrite it in case it
    # contains stuff proprietary to the local instance.
    if not os.path.lexists(os.path.join(dest_path, copied_file)):
        shutil.copy(full_source_path, os.path.join(dest_path, copied_file))


# 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 copy_sample_file(app, filename, dest_path=None):
    """
    Copies a sample file at `filename` to `the dest_path`
    directory and strips the '.sample' extensions from `filename`.
    Returns the path to the copied file (with the .sample extension).
    """
    if dest_path is None:
        dest_path = os.path.abspath(app.config.tool_data_path)
    sample_file_name = basic_util.strip_path(filename)
    copied_file = sample_file_name.rsplit(".sample", 1)[0]
    full_source_path = os.path.abspath(filename)
    full_destination_path = os.path.join(dest_path, sample_file_name)
    # Don't copy a file to itself - not sure how this happens, but sometimes it does...
    if full_source_path != full_destination_path:
        # It's ok to overwrite the .sample version of the file.
        shutil.copy(full_source_path, full_destination_path)
    # Only create the .loc file if it does not yet exist.  We don't overwrite it in case it
    # contains stuff proprietary to the local instance.
    non_sample_path = os.path.join(dest_path, copied_file)
    if not os.path.lexists(non_sample_path):
        shutil.copy(full_source_path, os.path.join(dest_path, copied_file))
    return non_sample_path