Exemplo n.º 1
0
def uninstall_tools(galaxy_server, api_key, names, force):
    galaxy_instance = GalaxyInstance(url=galaxy_server, key=api_key)
    toolshed_client = ToolShedClient(galaxy_instance)

    temp_tool_list_file = 'tmp/installed_tool_list.yml'
    # TODO: Switch to using bioblend to obtain this list
    # ephemeris uses bioblend but without using ephemeris we cut out the need to for a temp file
    os.system('get-tool-list -g %s -a %s -o %s --get_all_tools' %
              (galaxy_server, api_key, temp_tool_list_file))

    tools_to_uninstall = []
    with open(temp_tool_list_file) as tool_file:
        installed_tools = yaml.safe_load(tool_file.read())['tools']
    if not installed_tools:
        raise Exception('No tools to uninstall')
    os.system('rm %s' % temp_tool_list_file)

    for name in names:
        revision = None
        if '@' in name:
            (name, revision) = name.split('@')
        matching_tools = [
            t for t in installed_tools if t['name'] == name and (
                not revision or revision in t['revisions'])
        ]
        if len(matching_tools) == 0:
            id_string = 'name %s revision %s' % (
                name, revision) if revision else 'name %s' % name
            sys.stderr.write('*** Warning: No tool with %s\n' % id_string)
        elif len(matching_tools) > 1 and not force:
            sys.stderr.write(
                '*** Warning: More than one toolshed tool found for %s.  ' %
                name +
                'Not uninstalling any of these tools.  Run script with --force (-f) flag to uninstall anyway\n'
            )
        else:  # Either there is only one matching tool for the name and revision, or there are many and force=True
            for tool in matching_tools:
                tool_copy = tool.copy()
                if revision:
                    tool_copy['revisions'] = [revision]
                tools_to_uninstall.append(tool_copy)

    for tool in tools_to_uninstall:
        try:
            name = tool['name']
            owner = tool['owner']
            tool_shed_url = tool['tool_shed_url']
            revision = tool['revisions'][0]
            sys.stderr.write('Uninstalling %s at revision %s\n' %
                             (name, revision))
            return_value = toolshed_client.uninstall_repository_revision(
                name=name,
                owner=owner,
                changeset_revision=revision,
                tool_shed_url=tool_shed_url)
            sys.stderr.write(str(return_value) + '\n')
        except KeyError as e:
            sys.stderr.write(e)
Exemplo n.º 2
0
def uninstall_tools(galaxy_server, api_key, names, force):
    tools_to_uninstall = []
    galaxy_instance = GalaxyInstance(url=galaxy_server, key=api_key)
    toolshed_client = ToolShedClient(galaxy_instance)
    installed_tools = [
        t for t in toolshed_client.get_repositories()
        if t['status'] != 'Uninstalled'
    ]

    for name in names:
        revision = None
        if '@' in name:
            (name, revision) = name.split('@')
        matching_tools = [
            t for t in installed_tools if (t['name'] == name and (
                not revision or revision == t['changeset_revision']))
        ]
        id_string = 'name %s revision %s' % (
            name, revision) if revision else 'name %s' % name
        if len(matching_tools) == 0:
            print('*** Warning: No tool with %s' % id_string)
        elif len(matching_tools) > 1 and not force:
            print(
                '*** Warning: More than one toolshed tool found for %s.  ' %
                id_string +
                'Not uninstalling any of these tools.  Run script with --force (-f) flag to uninstall anyway'
            )
        else:  # Either there is only one matching tool for the name and revision, or there are many and force=True
            tools_to_uninstall.extend(matching_tools)

    for tool in tools_to_uninstall:
        try:
            print('Uninstalling %s at revision %s' %
                  (tool['name'], tool['changeset_revision']))
            return_value = toolshed_client.uninstall_repository_revision(
                name=tool['name'],
                owner=tool['owner'],
                changeset_revision=tool['changeset_revision'],
                tool_shed_url=tool['tool_shed'],
            )
            print(return_value)
        except Exception as e:
            print(e)