'The installation directory defined by the received tool_dependency_install_dir exists, so check for
the presence of INSTALLATION_LOG.  If the files exists, we\'ll assume the tool dependency is installed,
but not necessarily successfully (it could be in an error state on disk.  However, we can justifiably
assume here that no matter the state, an associated database record will exist.'

def sync_database_with_file_system(self, app, tool_shed_repository, tool_dependency_name, tool_dependency_version, tool_dependency_install_dir, tool_dependency_type='package'):

    log.debug('Synchronizing    the    database    with    the    file    system...')
    try:
        log.debug(('The    value    of    app.config.running_functional_tests    is:    %s' % str(app.config.running_functional_tests)))
    except:
        pass
    sa_session = app.install_model.context
    can_install_tool_dependency = False
    tool_dependency = tool_dependency_util.get_tool_dependency_by_name_version_type_repository(app, tool_shed_repository, tool_dependency_name, tool_dependency_version, tool_dependency_type)
    if (tool_dependency.status == app.install_model.ToolDependency.installation_status.INSTALLING):
        log.debug(('Skipping    installation    of    tool    dependency    %s    version    %s    because    it    has    a    status    of    %s' % (str(tool_dependency.name), str(tool_dependency.version), str(tool_dependency.status))))
    else:
        tool_dependency_installation_directory_contents = os.listdir(tool_dependency_install_dir)
        if (basic_util.INSTALLATION_LOG in tool_dependency_installation_directory_contents):
            log.debug(('Skipping    installation    of    tool    dependency    %s    version    %s    because    it    is    installed    in    %s' % (str(tool_dependency.name), str(tool_dependency.version), str(tool_dependency_install_dir))))
            if app.config.running_functional_tests:
                tool_dependency.status = app.install_model.ToolDependency.installation_status.INSTALLED
            else:
                error_message = 'The    installation    directory    for    this    tool    dependency    had    contents    but    the    database    had    no    record.    '
                error_message += 'The    installation    log    may    show    this    tool    dependency    to    be    correctly    installed,    but    due    to    the    '
                error_message += 'missing    database    record    it    is    now    being    set    to    Error.'
                tool_dependency.status = app.install_model.ToolDependency.installation_status.ERROR
                tool_dependency.error_message = error_message
        else:
            error_message = ('\nInstallation    path    %s    for    tool    dependency    %s    version    %s    exists,    but    the    expected    file    %s' % (str(tool_dependency_install_dir), str(tool_dependency_name), str(tool_dependency_version), str(basic_util.INSTALLATION_LOG)))
            error_message += '    is    missing.        This    indicates    an    installation    error    so    the    tool    dependency    is    being'
            error_message += '    prepared    for    re-installation.'
            log.error(error_message)
            tool_dependency.status = app.install_model.ToolDependency.installation_status.NEVER_INSTALLED
            basic_util.remove_dir(tool_dependency_install_dir)
            can_install_tool_dependency = True
        sa_session.add(tool_dependency)
        sa_session.flush()
    try:
        log.debug(('Returning    from    sync_database_with_file_system    with    tool_dependency    %s,    can_install_tool_dependency    %s.' % (str(tool_dependency.name), str(can_install_tool_dependency))))
    except Exception as e:
        log.debug(str(e))
    return (tool_dependency, can_install_tool_dependency)
