def get_tool_shed_status_for_installed_repository(app, repository): """ Send a request to the tool shed to retrieve information about newer installable repository revisions, current revision updates, whether the repository revision is the latest downloadable revision, and whether the repository has been deprecated in the tool shed. The received repository is a ToolShedRepository object from Galaxy. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, str(repository.tool_shed)) params = dict(name=repository.name, owner=repository.owner, changeset_revision=repository.changeset_revision) pathspec = ['repository', 'status_for_installed_repository'] try: encoded_tool_shed_status_dict = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) tool_shed_status_dict = encoding_util.tool_shed_decode(encoded_tool_shed_status_dict) return tool_shed_status_dict except HTTPError as e: # This should handle backward compatility to the Galaxy 12/20/12 release. We used to only handle updates for an installed revision # using a boolean value. log.debug("Error attempting to get tool shed status for installed repository %s: %s\nAttempting older 'check_for_updates' method.\n" % (str(repository.name), str(e))) pathspec = ['repository', 'check_for_updates'] params['from_update_manager'] = True try: # The value of text will be 'true' or 'false', depending upon whether there is an update available for the installed revision. text = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return dict(revision_update=text) except Exception: # The required tool shed may be unavailable, so default the revision_update value to 'false'. return dict(revision_update='false') except Exception: log.exception("Error attempting to get tool shed status for installed repository %s", str(repository.name)) return {}
def test_get_url_retry_after(): # This test is not ideal since it contacts an external resource # and doesn't acutally verify multiple attempts have been made. # responses doesn't mock the right place to fully simulate this. url = 'https://httpbin.org/status/429' with pytest.raises(requests.exceptions.RetryError): url_get(url, max_retries=2, backoff_factor=0.01)
def test_get_url_forbidden(): url = 'https://toolshed.g2.bx.psu.edu/' responses.add(responses.GET, url, body='Forbidden', status=403) with pytest.raises(requests.exceptions.HTTPError) as excinfo: url_get(url) assert '403 Client Error: Forbidden for url: https://toolshed.g2.bx.psu.edu/' in str( excinfo)
def shed_repository( self, trans, **kwd ): """ GET /api/tool_shed_repositories/shed_repository Get details about the specified repository from its shed. :param tsr_id: the tool_shed_repository_id """ tool_dependencies = dict() tools = dict() tool_shed_url = kwd.get( 'tool_shed_url', '' ) tsr_id = kwd.get( 'tsr_id', '' ) tool_panel_section_select_field = tool_util.build_tool_panel_section_select_field( trans.app ) tool_panel_section_dict = { 'name': tool_panel_section_select_field.name, 'id': tool_panel_section_select_field.field_id, 'sections': [] } for name, id, _ in tool_panel_section_select_field.options: tool_panel_section_dict['sections'].append( dict( id=id, name=name ) ) repository_data = dict() repository_data[ 'repository' ] = json.loads( util.url_get( tool_shed_url, pathspec=[ 'api', 'repositories', tsr_id ] ) ) repository_data[ 'repository' ][ 'metadata' ] = json.loads( util.url_get( tool_shed_url, pathspec=[ 'api', 'repositories', tsr_id, 'metadata' ] ) ) repository_data[ 'shed_conf' ] = tool_util.build_shed_tool_conf_select_field( trans.app ).get_html().replace('\n', '') repository_data[ 'panel_section_html' ] = tool_panel_section_select_field.get_html( extra_attr={ 'style': 'width: 30em;' } ).replace( '\n', '' ) repository_data[ 'panel_section_dict' ] = tool_panel_section_dict for changeset, metadata in repository_data[ 'repository' ][ 'metadata' ].items(): if changeset not in tool_dependencies: tool_dependencies[ changeset ] = [] if metadata[ 'includes_tools_for_display_in_tool_panel' ]: if changeset not in tools: tools[ changeset ] = [] for tool_dict in metadata[ 'tools' ]: tool_info = dict( clean=re.sub( '[^a-zA-Z0-9]+', '_', tool_dict[ 'name' ] ).lower(), guid=tool_dict[ 'guid' ], name=tool_dict[ 'name' ], version=tool_dict[ 'version' ], description=tool_dict[ 'description' ] ) if tool_info not in tools[ changeset ]: tools[ changeset ].append( tool_info ) if metadata[ 'has_repository_dependencies' ]: for repository_dependency in metadata[ 'repository_dependencies' ]: tools[ changeset ] = self.__get_tools( repository_dependency, tools[ changeset ] ) repository_data[ 'tools' ] = tools for key, dependency_dict in metadata[ 'tool_dependencies' ].items(): if 'readme' in dependency_dict: del( dependency_dict[ 'readme' ] ) if dependency_dict not in tool_dependencies[ changeset ]: tool_dependencies[ changeset ].append( dependency_dict ) # log.debug(tool_dependencies) if metadata[ 'has_repository_dependencies' ]: for repository_dependency in metadata[ 'repository_dependencies' ]: tool_dependencies[ changeset ] = self.__get_tool_dependencies( repository_dependency, tool_dependencies[ changeset ] ) repository_data[ 'tool_dependencies' ] = tool_dependencies return repository_data
def create_temporary_tool_dependencies_config( self, tool_shed_url, name, owner, changeset_revision ): """Make a call to the tool shed to get the required repository's tool_dependencies.xml file.""" tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( self.app, tool_shed_url ) if tool_shed_url is None or name is None or owner is None or changeset_revision is None: message = "Unable to retrieve required tool_dependencies.xml file from the Tool Shed because one or more of the " message += "following required parameters is None: tool_shed_url: %s, name: %s, owner: %s, changeset_revision: %s " % \ ( str( tool_shed_url ), str( name ), str( owner ), str( changeset_revision ) ) raise Exception( message ) params = dict( name=name, owner=owner, changeset_revision=changeset_revision ) pathspec = [ 'repository', 'get_tool_dependencies_config_contents' ] text = url_get( tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) if text: # Write the contents to a temporary file on disk so it can be reloaded and parsed. fh = tempfile.NamedTemporaryFile( 'wb', prefix="tmp-toolshed-cttdc" ) tmp_filename = fh.name fh.close() fh = open( tmp_filename, 'wb' ) fh.write( text ) fh.close() return tmp_filename else: message = "Unable to retrieve required tool_dependencies.xml file from the Tool Shed for revision " message += "%s of installed repository %s owned by %s." % ( str( changeset_revision ), str( name ), str( owner ) ) raise Exception( message ) return None
def request(self, trans, **params): """ GET /api/tool_shed/request """ tool_shed_url = params.pop("tool_shed_url") controller = params.pop("controller") if controller is None: raise MessageException( "Please provide a toolshed controller name.") tool_shed_registry = trans.app.tool_shed_registry if tool_shed_registry is None: raise MessageException("Toolshed registry not available.") if tool_shed_url in tool_shed_registry.tool_sheds.values(): pathspec = ["api", controller] if "id" in params: pathspec.append(params.pop("id")) if "action" in params: pathspec.append(params.pop("action")) try: return json.loads( util.url_get(tool_shed_url, params=dict(params), pathspec=pathspec)) except Exception as e: raise MessageException("Invalid server response. %s." % str(e)) else: raise MessageException("Invalid toolshed url.")
def get_tool_shed_repo_requirements(app, tool_shed_url, repository): """ Contact tool_shed_url for a list of requirements for a repository or a list of repositories. Returns a list of requirements, where each requirement is a dictionary with name and version as keys. """ if not isinstance(repository, list): repositories = [repository] else: repositories = repository pathspec = ["api", "repositories", "get_repository_revision_install_info"] tools = [] for repository in repositories: params = { "name": repository.name, "owner": repository.owner, "changeset_revision": repository.changeset_revision } response = util.url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) json_response = json.loads(response) valid_tools = json_response[1].get('valid_tools', []) if valid_tools: tools.extend(valid_tools) return get_unique_requirements_from_tools(tools)
def show(self, trans, **kwd): """ GET /api/tool_shed/contents Display a list of categories in the selected toolshed. :param tool_shed_url: the url of the toolshed to get categories from """ tool_shed_url = urlunquote(kwd.get('tool_shed_url', '')) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url) url = util.build_url(tool_shed_url, pathspec=['api', 'categories']) categories = [] try: for category in json.loads(util.url_get(url)): api_url = web.url_for(controller='api/tool_shed', action='category', tool_shed_url=urlquote(tool_shed_url), category_id=category['id'], qualified=True) category['url'] = api_url categories.append(category) except Exception: raise exceptions.ObjectNotFound("Tool Shed %s is not responding." % tool_shed_url) return categories
def get_cilogon_idps(self, trans, **kwargs): allowed_idps = trans.app.authnz_manager.get_allowed_idps() try: cilogon_idps = json.loads( url_get('https://cilogon.org/idplist/', params=dict(kwargs))) except Exception as e: raise Exception("Invalid server response. %s." % str(e)) if (allowed_idps): validated_idps = list( filter(lambda idp: idp['EntityID'] in allowed_idps, cilogon_idps)) if not (len(validated_idps) == len(allowed_idps)): validated_entity_ids = [ entity['EntityID'] for entity in validated_idps ] for idp in allowed_idps: if (idp not in validated_entity_ids): log.debug( "Invalid EntityID entered: %s. Use https://cilogon.org/idplist/ to find desired institution's EntityID.", idp) return validated_idps else: return cilogon_idps
def get_latest_installable_revision( self, trans, payload, **kwd ): """ POST /api/tool_shed_repositories/get_latest_installable_revision Get the latest installable revision of a specified repository from a specified Tool Shed. :param key: the current Galaxy admin user's API key The following parameters are included in the payload. :param tool_shed_url (required): the base URL of the Tool Shed from which to retrieve the Repository revision. :param name (required): the name of the Repository :param owner (required): the owner of the Repository """ # Get the information about the repository to be installed from the payload. tool_shed_url, name, owner = self.__parse_repository_from_payload( payload ) # Make sure the current user's API key proves he is an admin user in this Galaxy instance. if not trans.user_is_admin(): raise exceptions.AdminRequiredException( 'You are not authorized to request the latest installable revision for a repository in this Galaxy instance.' ) params = dict( name=name, owner=owner ) pathspec = [ 'api', 'repositories', 'get_ordered_installable_revisions' ] try: raw_text = util.url_get( tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) except Exception as e: message = "Error attempting to retrieve the latest installable revision from tool shed %s for repository %s owned by %s: %s" % \ ( str( tool_shed_url ), str( name ), str( owner ), str( e ) ) log.debug( message ) return dict( status='error', error=message ) if raw_text: # If successful, the response from get_ordered_installable_revisions will be a list of # changeset_revision hash strings. changeset_revisions = json.loads( raw_text ) if len( changeset_revisions ) >= 1: return changeset_revisions[ -1 ] return hg_util.INITIAL_CHANGELOG_HASH
def category(self, trans, **kwd): """ GET /api/tool_shed/category Display a list of repositories in the selected category. :param tool_shed_url: the url of the toolshed to get repositories from :param category_id: the category to get repositories from """ tool_shed_url = urlunquote(kwd.get('tool_shed_url', '')) category_id = kwd.get('category_id', '') params = dict(installable=True) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(trans.app, tool_shed_url) url = util.build_url(tool_shed_url, pathspec=['api', 'categories', category_id, 'repositories'], params=params) repositories = [] return_json = json.loads(util.url_get(url)) for repository in return_json['repositories']: api_url = web.url_for(controller='api/tool_shed', action='repository', tool_shed_url=urlquote(tool_shed_url), repository_id=repository['id'], qualified=True) repository['url'] = api_url repositories.append(repository) return_json['repositories'] = repositories return return_json
def get_tool_shed_repo_requirements(app, tool_shed_url, repository): """ Contact tool_shed_url for a list of requirements for a repository or a list of repositories. Returns a list of requirements, where each requirement is a dictionary with name and version as keys. """ if not isinstance(repository, list): repositories = [repository] else: repositories = repository pathspec = ["api", "repositories", "get_repository_revision_install_info"] tools = [] for repository in repositories: params = {"name": repository.name, "owner": repository.owner, "changeset_revision": repository.changeset_revision} response = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) json_response = json.loads(response) valid_tools = json_response[1].get('valid_tools', []) if valid_tools: tools.extend(valid_tools) return get_unique_requirements_from_tools(tools)
def get_tool_dependencies(app, tool_shed_url, repository_name, repository_owner, changeset_revision): tool_dependencies = [] tool_shed_accessible = True params = dict(name=repository_name, owner=repository_owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_tool_dependencies'] try: text = util.url_get( tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) tool_shed_accessible = True except Exception as e: tool_shed_accessible = False log.warning( "The URL\n%s\nraised the exception:\n%s\n", util.build_url(tool_shed_url, pathspec=pathspec, params=params), e) if tool_shed_accessible: if text: tool_dependencies_dict = encoding_util.tool_shed_decode(text) for requirements_dict in tool_dependencies_dict.values(): tool_dependency_name = requirements_dict['name'] tool_dependency_version = requirements_dict['version'] tool_dependency_type = requirements_dict['type'] tool_dependencies.append( (tool_dependency_name, tool_dependency_version, tool_dependency_type)) return tool_shed_accessible, tool_dependencies
def tool_json(self, trans, **kwd): """ GET /api/tool_shed_repositories/shed_tool_json Get the tool form JSON for a tool in a toolshed repository. :param guid: the GUID of the tool :param guid: str :param tsr_id: the ID of the repository :param tsr_id: str :param changeset: the changeset at which to load the tool json :param changeset: str :param tool_shed_url: the URL of the toolshed to load from :param tool_shed_url: str """ tool_shed_url = kwd.get('tool_shed_url', None) tsr_id = kwd.get('tsr_id', None) guid = kwd.get('guid', None) changeset = kwd.get('changeset', None) if None in [tool_shed_url, tsr_id, guid, changeset]: message = 'Tool shed URL, changeset, repository ID, and tool GUID are all required parameters.' trans.response.status = 400 return {'status': 'error', 'message': message} response = json.loads(util.url_get(tool_shed_url, params=dict(tsr_id=tsr_id, guid=guid, changeset=changeset.split(':')[-1]), pathspec=['api', 'tools', 'json'])) return response
def get_repository_dependencies(app, tool_shed_url, repository_name, repository_owner, changeset_revision): repository_dependencies_dict = {} tool_shed_accessible = True params = dict(name=repository_name, owner=repository_owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_repository_dependencies'] try: raw_text = util.url_get( tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) tool_shed_accessible = True except Exception as e: tool_shed_accessible = False log.warning( "The URL\n%s\nraised the exception:\n%s\n", util.build_url(tool_shed_url, pathspec=pathspec, params=params), e) if tool_shed_accessible: if len(raw_text) > 2: encoded_text = json.loads(util.unicodify(raw_text)) repository_dependencies_dict = encoding_util.tool_shed_decode( encoded_text) return tool_shed_accessible, repository_dependencies_dict
def check_for_tool_dependencies( self, trans, migration_stage ): # Get the 000x_tools.xml file associated with migration_stage. tools_xml_file_path = os.path.abspath( os.path.join( trans.app.config.root, 'scripts', 'migrate_tools', '%04d_tools.xml' % migration_stage ) ) tree = galaxy.util.parse_xml( tools_xml_file_path ) root = tree.getroot() tool_shed = root.get( 'name' ) shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed ) repo_name_dependency_tups = [] if shed_url: for elem in root: if elem.tag == 'repository': tool_dependencies = [] tool_dependencies_dict = {} repository_name = elem.get( 'name' ) changeset_revision = elem.get( 'changeset_revision' ) params = dict( name=repository_name, owner='devteam', changeset_revision=changeset_revision ) pathspec = [ 'repository', 'get_tool_dependencies' ] text = url_get( shed_url, password_mgr=self.app.tool_shed_registry.url_auth( shed_url ), pathspec=pathspec, params=params ) if text: tool_dependencies_dict = encoding_util.tool_shed_decode( text ) for dependency_key, requirements_dict in tool_dependencies_dict.items(): tool_dependency_name = requirements_dict[ 'name' ] tool_dependency_version = requirements_dict[ 'version' ] tool_dependency_type = requirements_dict[ 'type' ] tool_dependency_readme = requirements_dict.get( 'readme', '' ) tool_dependencies.append( ( tool_dependency_name, tool_dependency_version, tool_dependency_type, tool_dependency_readme ) ) repo_name_dependency_tups.append( ( repository_name, tool_dependencies ) ) return repo_name_dependency_tups
def get_repository_dependencies_for_installed_tool_shed_repository( self, app, repository): """ Send a request to the appropriate tool shed to retrieve the dictionary of repository dependencies defined for the received repository which is installed into Galaxy. This method is called only from Galaxy. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, str(repository.tool_shed)) params = dict(name=str(repository.name), owner=str(repository.owner), changeset_revision=str(repository.changeset_revision)) pathspec = ['repository', 'get_repository_dependencies'] try: raw_text = url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) except Exception: log.exception( "Error while trying to get URL: %s", build_url(tool_shed_url, pathspec=pathspec, params=params)) return '' if len(raw_text) > 2: encoded_text = json.loads(raw_text) text = encoding_util.tool_shed_decode(encoded_text) else: text = '' return text
def create_temporary_tool_dependencies_config(self, tool_shed_url, name, owner, changeset_revision): """Make a call to the tool shed to get the required repository's tool_dependencies.xml file.""" tool_shed_url = get_tool_shed_url_from_tool_shed_registry( self.app, tool_shed_url) if tool_shed_url is None or name is None or owner is None or changeset_revision is None: message = "Unable to retrieve required tool_dependencies.xml file from the Tool Shed because one or more of the " message += "following required parameters is None: tool_shed_url: %s, name: %s, owner: %s, changeset_revision: %s " % \ (str(tool_shed_url), str(name), str(owner), str(changeset_revision)) raise Exception(message) params = dict(name=name, owner=owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_tool_dependencies_config_contents'] text = url_get( tool_shed_url, auth=self.app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) if text: # Write the contents to a temporary file on disk so it can be reloaded and parsed. fh = tempfile.NamedTemporaryFile('w', prefix="tmp-toolshed-cttdc") tmp_filename = fh.name fh.close() fh = open(tmp_filename, 'w') fh.write(text) fh.close() return tmp_filename else: message = "Unable to retrieve required tool_dependencies.xml file from the Tool Shed for revision " message += f"{str(changeset_revision)} of installed repository {str(name)} owned by {str(owner)}." raise Exception(message)
def shed_categories( self, trans, **kwd ): """ GET /api/tool_shed_repositories/shed_categories Display a list of categories in the selected toolshed. :param tool_shed_url: the url of the toolshed to get categories from """ tool_shed_url = kwd.get( 'tool_shed_url', '' ) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url ) url = util.build_url( tool_shed_url, pathspec=[ 'api', 'categories' ] ) categories = json.loads( util.url_get( url ) ) repositories = [] url = util.build_url( tool_shed_url, pathspec=[ 'api', 'repositories' ] ) for repo in json.loads( util.url_get( url ) ): repositories.append( dict( value=repo[ 'id' ], label='%s/%s' % ( repo[ 'owner' ], repo[ 'name' ] ) ) ) return { 'categories': categories, 'repositories': repositories }
def get_repository_for_dependency_relationship(app, tool_shed, name, owner, changeset_revision): """ Return an installed tool_shed_repository database record that is defined by either the current changeset revision or the installed_changeset_revision. """ # This method is used only in Galaxy, not the Tool Shed. We store the port (if one exists) in the database. tool_shed = common_util.remove_protocol_from_tool_shed_url(tool_shed) if tool_shed is None or name is None or owner is None or changeset_revision is None: message = "Unable to retrieve the repository record from the database because one or more of the following " message += "required parameters is None: tool_shed: %s, name: %s, owner: %s, changeset_revision: %s " % \ (str(tool_shed), str(name), str(owner), str(changeset_revision)) raise Exception(message) app.tool_shed_repository_cache.rebuild() repository = get_installed_repository( app=app, tool_shed=tool_shed, name=name, owner=owner, installed_changeset_revision=changeset_revision) if not repository: repository = get_installed_repository( app=app, tool_shed=tool_shed, name=name, owner=owner, changeset_revision=changeset_revision) if not repository: tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, tool_shed) repository_clone_url = os.path.join(tool_shed_url, 'repos', owner, name) repo_info_tuple = (None, repository_clone_url, changeset_revision, None, owner, None, None) repository, pcr = repository_was_previously_installed( app, tool_shed_url, name, repo_info_tuple) if not repository: # The received changeset_revision is no longer installable, so get the next changeset_revision # in the repository's changelog in the tool shed that is associated with repository_metadata. tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, tool_shed) params = dict(name=name, owner=owner, changeset_revision=changeset_revision) pathspec = ['repository', 'next_installable_changeset_revision'] text = util.url_get( tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) if text: repository = get_installed_repository(app=app, tool_shed=tool_shed, name=name, owner=owner, changeset_revision=text) return repository
def get_ctx_rev(app, tool_shed_url, name, owner, changeset_revision): """ Send a request to the tool shed to retrieve the ctx_rev for a repository defined by the combination of a name, owner and changeset revision. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, tool_shed_url) params = dict(name=name, owner=owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_ctx_rev'] ctx_rev = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return ctx_rev
def __get_repo_info_dict( self, trans, repositories, tool_shed_url ): repo_ids = [] changesets = [] for repository_id, changeset in repositories: repo_ids.append( repository_id ) changesets.append( changeset ) params = dict( repository_ids=str( ','.join( repo_ids ) ), changeset_revisions=str( ','.join( changesets ) ) ) pathspec = [ 'repository', 'get_repository_information' ] raw_text = util.url_get( tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) return json.loads( raw_text )
def get_repository_type_from_tool_shed(app, tool_shed_url, name, owner): """ Send a request to the tool shed to retrieve the type for a repository defined by the combination of a name and owner. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, tool_shed_url) params = dict(name=name, owner=owner) pathspec = ['repository', 'get_repository_type'] repository_type = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return repository_type
def get_tool_dependencies( app, tool_shed_url, repository_name, repository_owner, changeset_revision ): tool_dependencies = [] tool_shed_accessible = True params = dict( name=repository_name, owner=repository_owner, changeset_revision=changeset_revision ) pathspec = [ 'repository', 'get_tool_dependencies' ] try: text = util.url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) tool_shed_accessible = True except Exception, e: tool_shed_accessible = False log.warn( "The URL\n%s\nraised the exception:\n%s\n", util.build_url( tool_shed_url, pathspec=pathspec, params=params ), e )
def get_tool_dependency_definition_metadata_from_tool_shed(app, tool_shed_url, name, owner): """ Send a request to the tool shed to retrieve the current metadata for a repository of type tool_dependency_definition defined by the combination of a name and owner. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, tool_shed_url) params = dict(name=name, owner=owner) pathspec = ['repository', 'get_tool_dependency_definition_metadata'] metadata = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return metadata
def shed_categories(self, trans, **kwd): """ GET /api/tool_shed_repositories/shed_categories Display a list of categories in the selected toolshed. :param tool_shed_url: the url of the toolshed to get categories from """ tool_shed_url = kwd.get('tool_shed_url', '') tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url) url = util.build_url(tool_shed_url, pathspec=['api', 'categories']) categories = json.loads(util.url_get(url)) repositories = [] url = util.build_url(tool_shed_url, pathspec=['api', 'repositories']) for repo in json.loads(util.url_get(url)): repositories.append( dict(value=repo['id'], label='%s/%s' % (repo['owner'], repo['name']))) return {'categories': categories, 'repositories': repositories}
def repository_was_previously_installed(app, tool_shed_url, repository_name, repo_info_tuple, from_tip=False): """ Find out if a repository is already installed into Galaxy - there are several scenarios where this is necessary. For example, this method will handle the case where the repository was previously installed using an older changeset_revsion, but later the repository was updated in the tool shed and now we're trying to install the latest changeset revision of the same repository instead of updating the one that was previously installed. We'll look in the database instead of on disk since the repository may be currently uninstalled. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, tool_shed_url) description, repository_clone_url, changeset_revision, ctx_rev, repository_owner, repository_dependencies, tool_dependencies = \ get_repo_info_tuple_contents(repo_info_tuple) tool_shed = get_tool_shed_from_clone_url(repository_clone_url) # See if we can locate the repository using the value of changeset_revision. tool_shed_repository = get_installed_repository( app, tool_shed=tool_shed, name=repository_name, owner=repository_owner, installed_changeset_revision=changeset_revision) if tool_shed_repository: return tool_shed_repository, changeset_revision # Get all previous changeset revisions from the tool shed for the repository back to, but excluding, # the previous valid changeset revision to see if it was previously installed using one of them. params = dict(galaxy_url=web.url_for('/', qualified=True), name=repository_name, owner=repository_owner, changeset_revision=changeset_revision, from_tip=str(from_tip)) pathspec = ['repository', 'previous_changeset_revisions'] text = util.url_get(tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) if text: changeset_revisions = util.listify(text) for previous_changeset_revision in changeset_revisions: tool_shed_repository = get_installed_repository( app, tool_shed=tool_shed, name=repository_name, owner=repository_owner, installed_changeset_revision=previous_changeset_revision) if tool_shed_repository: return tool_shed_repository, previous_changeset_revision return None, None
def get_tool_shed_repo_requirements(app, tool_shed_url, repositories=None, repo_info_dicts=None): """ Contact tool_shed_url for a list of requirements for a repository or a list of repositories. Returns a list of requirements, where each requirement is a dictionary with name and version as keys. """ if not repositories and not repo_info_dicts: raise Exception("Need to pass either repository or repo_info_dicts") if repositories: if not isinstance(repositories, list): repositories = [repositories] repository_params = [{ 'name': repository.name, 'owner': repository.owner, 'changeset_revision': repository.changeset_revision } for repository in repositories] else: if not isinstance(repo_info_dicts, list): repo_info_dicts = [repo_info_dicts] repository_params = [] for repo_info_dict in repo_info_dicts: for name, repo_info_tuple in repo_info_dict.items(): # repo_info_tuple is a list, but keep terminology owner = repo_info_tuple[4] changeset_revision = repo_info_tuple[2] repository_params.append({ 'name': name, 'owner': owner, 'changeset_revision': changeset_revision }) pathspec = ["api", "repositories", "get_repository_revision_install_info"] tools = [] for params in repository_params: response = util.url_get( tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) json_response = json.loads(response) valid_tools = json_response[1].get('valid_tools', []) if valid_tools: tools.extend(valid_tools) return get_requirements_from_tools(tools)
def search(self, trans, **kwd): """ GET /api/tool_shed/search Search for a specific repository in the toolshed. :param q: the query string to search for :param q: str :param tool_shed_url: the URL of the toolshed to search :param tool_shed_url: str """ tool_shed_url = kwd.get('tool_shed_url', None) q = kwd.get('term', None) if None in [q, tool_shed_url]: return {} response = json.loads(util.url_get(tool_shed_url, params=dict(q=q), pathspec=['api', 'repositories'])) return response
def shed_category( self, trans, **kwd ): """ GET /api/tool_shed_repositories/shed_category Display a list of repositories in the selected category. :param tool_shed_url: the url of the toolshed to get repositories from :param category_id: the category to get repositories from """ tool_shed_url = kwd.get( 'tool_shed_url', '' ) category_id = kwd.get( 'category_id', '' ) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url ) url = util.build_url( tool_shed_url, pathspec=[ 'api', 'categories', category_id, 'repositories' ] ) category = json.loads( util.url_get( url ) ) return category
def get_updated_changeset_revisions_from_tool_shed(app, tool_shed_url, name, owner, changeset_revision): """ Get all appropriate newer changeset revisions for the repository defined by the received tool_shed_url / name / owner combination. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, tool_shed_url) if tool_shed_url is None or name is None or owner is None or changeset_revision is None: message = "Unable to get updated changeset revisions from the Tool Shed because one or more of the following " message += "required parameters is None: tool_shed_url: %s, name: %s, owner: %s, changeset_revision: %s " % \ (str(tool_shed_url), str(name), str(owner), str(changeset_revision)) raise Exception(message) params = dict(name=name, owner=owner, changeset_revision=changeset_revision) pathspec = ['repository', 'updated_changeset_revisions'] text = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return text
def __get_repo_info_dict(self, trans, repositories, tool_shed_url): repo_ids = [] changesets = [] for repository_id, changeset in repositories: repo_ids.append(repository_id) changesets.append(changeset) params = dict(repository_ids=str(','.join(repo_ids)), changeset_revisions=str(','.join(changesets))) pathspec = ['repository', 'get_repository_information'] raw_text = util.url_get( tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) return json.loads(raw_text)
def get_updated_changeset_revisions_from_tool_shed( app, tool_shed_url, name, owner, changeset_revision ): """ Get all appropriate newer changeset revisions for the repository defined by the received tool_shed_url / name / owner combination. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, tool_shed_url ) if tool_shed_url is None or name is None or owner is None or changeset_revision is None: message = "Unable to get updated changeset revisions from the Tool Shed because one or more of the following " message += "required parameters is None: tool_shed_url: %s, name: %s, owner: %s, changeset_revision: %s " % \ ( str( tool_shed_url ), str( name ), str( owner ), str( changeset_revision ) ) raise Exception( message ) params = dict( name=name, owner=owner, changeset_revision=changeset_revision ) pathspec = [ 'repository', 'updated_changeset_revisions' ] text = util.url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) return text
def get_readme_files_dict_for_display( app, tool_shed_url, repo_info_dict ): """ Return a dictionary of README files contained in the single repository being installed so they can be displayed on the tool panel section selection page. """ name = next(iter(repo_info_dict)) repo_info_tuple = repo_info_dict[ name ] description, repository_clone_url, changeset_revision, ctx_rev, repository_owner, repository_dependencies, installed_td = \ repository_util.get_repo_info_tuple_contents( repo_info_tuple ) # Handle changing HTTP protocols over time. tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, tool_shed_url ) params = dict( name=name, owner=repository_owner, changeset_revision=changeset_revision ) pathspec = [ 'repository', 'get_readme_files' ] raw_text = url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) readme_files_dict = json.loads( raw_text ) return readme_files_dict
def get_readme_files_dict_for_display(app, tool_shed_url, repo_info_dict): """ Return a dictionary of README files contained in the single repository being installed so they can be displayed on the tool panel section selection page. """ name = next(iter(repo_info_dict)) repo_info_tuple = repo_info_dict[name] description, repository_clone_url, changeset_revision, ctx_rev, repository_owner, repository_dependencies, installed_td = \ repository_util.get_repo_info_tuple_contents(repo_info_tuple) # Handle changing HTTP protocols over time. tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(app, tool_shed_url) params = dict(name=name, owner=repository_owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_readme_files'] raw_text = url_get(tool_shed_url, auth=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) readme_files_dict = json.loads(raw_text) return readme_files_dict
def get_repository_dependencies(app, tool_shed_url, repository_name, repository_owner, changeset_revision): repository_dependencies_dict = {} tool_shed_accessible = True params = dict(name=repository_name, owner=repository_owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_repository_dependencies'] try: raw_text = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) tool_shed_accessible = True except Exception as e: tool_shed_accessible = False log.warning("The URL\n%s\nraised the exception:\n%s\n", util.build_url(tool_shed_url, pathspec=pathspec, params=params), e) if tool_shed_accessible: if len(raw_text) > 2: encoded_text = json.loads(raw_text) repository_dependencies_dict = encoding_util.tool_shed_decode(encoded_text) return tool_shed_accessible, repository_dependencies_dict
def get_update_to_changeset_revision_and_ctx_rev( self, repository ): """Return the changeset revision hash to which the repository can be updated.""" changeset_revision_dict = {} tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( self.app, str( repository.tool_shed ) ) params = dict( name=str( repository.name ), owner=str( repository.owner ), changeset_revision=str( repository.installed_changeset_revision ) ) pathspec = [ 'repository', 'get_changeset_revision_and_ctx_rev' ] try: encoded_update_dict = util.url_get( tool_shed_url, password_mgr=self.app.tool_shed_registry.url_auth( tool_shed_url ), pathspec=pathspec, params=params ) if encoded_update_dict: update_dict = encoding_util.tool_shed_decode( encoded_update_dict ) includes_data_managers = update_dict.get( 'includes_data_managers', False ) includes_datatypes = update_dict.get( 'includes_datatypes', False ) includes_tools = update_dict.get( 'includes_tools', False ) includes_tools_for_display_in_tool_panel = update_dict.get( 'includes_tools_for_display_in_tool_panel', False ) includes_tool_dependencies = update_dict.get( 'includes_tool_dependencies', False ) includes_workflows = update_dict.get( 'includes_workflows', False ) has_repository_dependencies = update_dict.get( 'has_repository_dependencies', False ) has_repository_dependencies_only_if_compiling_contained_td = update_dict.get( 'has_repository_dependencies_only_if_compiling_contained_td', False ) changeset_revision = update_dict.get( 'changeset_revision', None ) ctx_rev = update_dict.get( 'ctx_rev', None ) changeset_revision_dict[ 'includes_data_managers' ] = includes_data_managers changeset_revision_dict[ 'includes_datatypes' ] = includes_datatypes changeset_revision_dict[ 'includes_tools' ] = includes_tools changeset_revision_dict[ 'includes_tools_for_display_in_tool_panel' ] = includes_tools_for_display_in_tool_panel changeset_revision_dict[ 'includes_tool_dependencies' ] = includes_tool_dependencies changeset_revision_dict[ 'includes_workflows' ] = includes_workflows changeset_revision_dict[ 'has_repository_dependencies' ] = has_repository_dependencies changeset_revision_dict[ 'has_repository_dependencies_only_if_compiling_contained_td' ] = has_repository_dependencies_only_if_compiling_contained_td changeset_revision_dict[ 'changeset_revision' ] = changeset_revision changeset_revision_dict[ 'ctx_rev' ] = ctx_rev except Exception as e: log.debug( "Error getting change set revision for update from the tool shed for repository '%s': %s" % ( repository.name, str( e ) ) ) changeset_revision_dict[ 'includes_data_managers' ] = False changeset_revision_dict[ 'includes_datatypes' ] = False changeset_revision_dict[ 'includes_tools' ] = False changeset_revision_dict[ 'includes_tools_for_display_in_tool_panel' ] = False changeset_revision_dict[ 'includes_tool_dependencies' ] = False changeset_revision_dict[ 'includes_workflows' ] = False changeset_revision_dict[ 'has_repository_dependencies' ] = False changeset_revision_dict[ 'has_repository_dependencies_only_if_compiling_contained_td' ] = False changeset_revision_dict[ 'changeset_revision' ] = None changeset_revision_dict[ 'ctx_rev' ] = None return changeset_revision_dict
def shed_category(self, trans, **kwd): """ GET /api/tool_shed_repositories/shed_category Display a list of repositories in the selected category. :param tool_shed_url: the url of the toolshed to get repositories from :param category_id: the category to get repositories from """ tool_shed_url = kwd.get('tool_shed_url', '') category_id = kwd.get('category_id', '') tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url) url = util.build_url( tool_shed_url, pathspec=['api', 'categories', category_id, 'repositories']) category = json.loads(util.url_get(url)) return category
def get_tool_dependencies(app, tool_shed_url, repository_name, repository_owner, changeset_revision): tool_dependencies = [] tool_shed_accessible = True params = dict(name=repository_name, owner=repository_owner, changeset_revision=changeset_revision) pathspec = ['repository', 'get_tool_dependencies'] try: text = util.url_get(tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) tool_shed_accessible = True except Exception as e: tool_shed_accessible = False log.warning("The URL\n%s\nraised the exception:\n%s\n", util.build_url(tool_shed_url, pathspec=pathspec, params=params), e) if tool_shed_accessible: if text: tool_dependencies_dict = encoding_util.tool_shed_decode(text) for requirements_dict in tool_dependencies_dict.values(): tool_dependency_name = requirements_dict['name'] tool_dependency_version = requirements_dict['version'] tool_dependency_type = requirements_dict['type'] tool_dependencies.append((tool_dependency_name, tool_dependency_version, tool_dependency_type)) return tool_shed_accessible, tool_dependencies
def show(self, trans, **kwd): """ GET /api/tool_shed/contents Display a list of categories in the selected toolshed. :param tool_shed_url: the url of the toolshed to get categories from """ tool_shed_url = urlunquote(kwd.get('tool_shed_url', '')) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry(trans.app, tool_shed_url) url = util.build_url(tool_shed_url, pathspec=['api', 'categories']) categories = [] for category in json.loads(util.url_get(url)): api_url = web.url_for(controller='api/tool_shed', action='category', tool_shed_url=urlquote(tool_shed_url), category_id=category['id'], qualified=True) category['url'] = api_url categories.append(category) return categories
def category(self, trans, **kwd): """ GET /api/tool_shed/category Display a list of repositories in the selected category. :param tool_shed_url: the url of the toolshed to get repositories from :param category_id: the category to get repositories from :param sort_key: the field by which the repositories should be sorted :param sort_order: ascending or descending sort :param page: the page number to return """ sort_order = kwd.get('sort_order', 'asc') sort_key = kwd.get('sort_key', 'name') page = kwd.get('page', 1) tool_shed_url = urlunquote(kwd.get('tool_shed_url', '')) category_id = kwd.get('category_id', '') params = dict(installable=True, sort_order=sort_order, sort_key=sort_key, page=page) tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( trans.app, tool_shed_url) url = util.build_url( tool_shed_url, pathspec=['api', 'categories', category_id, 'repositories'], params=params) repositories = [] return_json = json.loads(util.url_get(url)) for repository in return_json['repositories']: api_url = web.url_for(controller='api/tool_shed', action='repository', tool_shed_url=urlquote(tool_shed_url), repository_id=repository['id'], qualified=True) repository['url'] = api_url repositories.append(repository) return_json['repositories'] = repositories return return_json
def get_repository_dependencies_for_installed_tool_shed_repository( self, app, repository): """ Send a request to the appropriate tool shed to retrieve the dictionary of repository dependencies defined for the received repository which is installed into Galaxy. This method is called only from Galaxy. """ tool_shed_url = common_util.get_tool_shed_url_from_tool_shed_registry( app, str(repository.tool_shed)) params = dict(name=str(repository.name), owner=str(repository.owner), changeset_revision=str(repository.changeset_revision)) pathspec = ['repository', 'get_repository_dependencies'] try: raw_text = url_get( tool_shed_url, password_mgr=app.tool_shed_registry.url_auth(tool_shed_url), pathspec=pathspec, params=params) except Exception, e: log.error( "The URL\n%s\nraised the exception:\n%s\n", build_url(tool_shed_url, pathspec=pathspec, params=params), str(e)) return ''