'Update an exsiting tool dependency whose definition was updated in a change set
pulled by a Galaxy administrator when getting updates to an installed tool shed
repository.  The original_dependency_dict is a single tool dependency definition,
an example of which is::
{"name": "bwa",
"readme": "\nCompiling BWA requires zlib and libpthread to be present on your system.\n        ",
"type": "package",
"version": "0.6.2"}
The new_dependencies_dict is the dictionary generated by the metadata_util.generate_tool_dependency_metadata method.'

def update_existing_tool_dependency(self, repository, original_dependency_dict, new_dependencies_dict):

    new_tool_dependency = None
    original_name = original_dependency_dict['name']
    original_type = original_dependency_dict['type']
    original_version = original_dependency_dict['version']
    tool_dependency = None
    for tool_dependency in repository.tool_dependencies:
        if ((tool_dependency.name == original_name) and (tool_dependency.type == original_type) and (tool_dependency.version == original_version)):
            break
    if (tool_dependency and tool_dependency.can_update):
        dependency_install_dir = tool_dependency.installation_directory(self.app)
        (removed_from_disk, error_message) = tool_dependency_util.remove_tool_dependency_installation_directory(dependency_install_dir)
        if removed_from_disk:
            context = self.app.install_model.context
            new_dependency_name = None
            new_dependency_type = None
            new_dependency_version = None
            for (new_dependency_key, new_dependency_val_dict) in new_dependencies_dict.items():
                if (original_name == new_dependency_val_dict['name']):
                    new_dependency_name = new_dependency_val_dict['name']
                    new_dependency_type = new_dependency_val_dict['type']
                    new_dependency_version = new_dependency_val_dict['version']
                    break
            if (new_dependency_name and new_dependency_type and new_dependency_version):
                log.debug(('Updating    version    %s    of    tool    dependency    %s    %s    to    have    new    version    %s    and    type    %s.' % (str(tool_dependency.version), str(tool_dependency.type), str(tool_dependency.name), str(new_dependency_version), str(new_dependency_type))))
                tool_dependency.type = new_dependency_type
                tool_dependency.version = new_dependency_version
                tool_dependency.status = self.app.install_model.ToolDependency.installation_status.UNINSTALLED
                tool_dependency.error_message = None
                context.add(tool_dependency)
                context.flush()
                new_tool_dependency = tool_dependency
            else:
                log.debug(('Deleting    version    %s    of    tool    dependency    %s    %s    from    the    database    since    it    is    no    longer    defined.' % (str(tool_dependency.version), str(tool_dependency.type), str(tool_dependency.name))))
                context.delete(tool_dependency)
                context.flush()
    return new_tool_dependency
