Ejemplo n.º 1
0
 def get_certified_level_one_tuple(self, repository):
     """
     Return True if the latest installable changeset_revision of the received repository is level one certified.
     """
     if repository is None:
         return (None, False)
     if repository.deleted or repository.deprecated:
         return (None, False)
     repo = hg_util.get_repo_for_repository(self.app,
                                            repository=repository,
                                            repo_path=None,
                                            create=False)
     # Get the latest installable changeset revision since that is all that is currently configured for testing.
     latest_installable_changeset_revision = metadata_util.get_latest_downloadable_changeset_revision(
         self.app, repository, repo)
     if latest_installable_changeset_revision not in [
             None, hg_util.INITIAL_CHANGELOG_HASH
     ]:
         encoded_repository_id = self.app.security.encode_id(repository.id)
         repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(
             self.app, encoded_repository_id,
             latest_installable_changeset_revision)
         if repository_metadata:
             # No repository_metadata.
             return (latest_installable_changeset_revision, True)
     else:
         # No installable changeset_revision.
         return (None, False)
 def get_certified_level_one_tuple(self, repository):
     """
     Return True if the latest installable changeset_revision of the received repository is level one certified.
     """
     if repository is None:
         return (None, False)
     if repository.deleted or repository.deprecated:
         return (None, False)
     # Get the latest installable changeset revision since that is all that is currently configured for testing.
     latest_installable_changeset_revision = metadata_util.get_latest_downloadable_changeset_revision(self.app, repository)
     if latest_installable_changeset_revision not in [None, hg_util.INITIAL_CHANGELOG_HASH]:
         encoded_repository_id = self.app.security.encode_id(repository.id)
         repository_metadata = metadata_util.get_repository_metadata_by_changeset_revision(self.app,
                                                                                           encoded_repository_id,
                                                                                           latest_installable_changeset_revision)
         if repository_metadata:
             # No repository_metadata.
             return (latest_installable_changeset_revision, True)
     else:
         # No installable changeset_revision.
         return (None, False)
Ejemplo n.º 3
0
def build_readme_files_dict(app, repository, changeset_revision, metadata, tool_path=None):
    """
    Return a dictionary of valid readme file name <-> readme file content pairs for all readme files defined in the received metadata.  Since the
    received changeset_revision (which is associated with the received metadata) may not be the latest installable changeset revision, the README
    file contents may not be available on disk.  This method is used by both Galaxy and the Tool Shed.
    """
    if app.name == 'galaxy':
        can_use_disk_files = True
    else:
        latest_downloadable_changeset_revision = metadata_util.get_latest_downloadable_changeset_revision(app, repository)
        can_use_disk_files = changeset_revision == latest_downloadable_changeset_revision
    readme_files_dict = {}
    if metadata:
        if 'readme_files' in metadata:
            for relative_path_to_readme_file in metadata['readme_files']:
                readme_file_name = os.path.split(relative_path_to_readme_file)[1]
                if can_use_disk_files:
                    if tool_path:
                        full_path_to_readme_file = os.path.abspath(os.path.join(tool_path, relative_path_to_readme_file))
                    else:
                        full_path_to_readme_file = os.path.abspath(relative_path_to_readme_file)
                    text = None
                    try:
                        with open(full_path_to_readme_file, encoding='utf-8') as f:
                            text = f.read()
                    except Exception:
                        log.exception("Error reading README file '%s' from disk", relative_path_to_readme_file)
                        text = None
                    if text:
                        text_of_reasonable_length = basic_util.size_string(text)
                        if text_of_reasonable_length.find('.. image:: ') >= 0:
                            # Handle image display for README files that are contained in repositories in the tool shed or installed into Galaxy.
                            try:
                                text_of_reasonable_length = suc.set_image_paths(app,
                                                                                text_of_reasonable_length,
                                                                                encoded_repository_id=app.security.encode_id(repository.id))
                            except Exception:
                                log.exception("Exception in build_readme_files_dict, so images may not be properly displayed")
                        if readme_file_name.endswith('.rst'):
                            text_of_reasonable_length = Template(rst_to_html(text_of_reasonable_length),
                                                                 input_encoding='utf-8',
                                                                 default_filters=['decode.utf8'],
                                                                 encoding_errors='replace')
                            text_of_reasonable_length = text_of_reasonable_length.render(static_path=web.url_for('/static'),
                                                                                         host_url=web.url_for('/', qualified=True))
                            text_of_reasonable_length = unicodify(text_of_reasonable_length)
                        else:
                            text_of_reasonable_length = basic_util.to_html_string(text_of_reasonable_length)
                        readme_files_dict[readme_file_name] = text_of_reasonable_length
                else:
                    # We must be in the tool shed and have an old changeset_revision, so we need to retrieve the file contents from the repository manifest.
                    repo = repository.hg_repo
                    ctx = hg_util.get_changectx_for_changeset(repo, changeset_revision)
                    if ctx:
                        fctx = hg_util.get_file_context_from_ctx(ctx, readme_file_name)
                        if fctx and fctx not in ['DELETED']:
                            try:
                                text = unicodify(fctx.data())
                                readme_files_dict[readme_file_name] = basic_util.size_string(text)
                            except Exception:
                                log.exception("Error reading README file '%s' from repository manifest", relative_path_to_readme_file)
    return readme_files_dict
Ejemplo n.º 4
0
 def handle_elem(self, elem):
     """Populate or unpopulate the changeset_revision and toolshed attributes of repository tags."""
     # <repository name="molecule_datatypes" owner="test" changeset_revision="1a070566e9c6" />
     # <repository changeset_revision="xxx" name="package_xorg_macros_1_17_1" owner="test" toolshed="yyy">
     #    <package name="xorg_macros" version="1.17.1" />
     # </repository>
     error_message = ''
     name = elem.get('name')
     owner = elem.get('owner')
     # The name and owner attributes are always required, so if either are missing, return the error message.
     if not name or not owner:
         error_message = self.check_tag_attributes(elem)
         return False, elem, error_message
     altered = False
     toolshed = elem.get('toolshed')
     changeset_revision = elem.get('changeset_revision')
     # Over a short period of time a bug existed which caused the prior_installation_required attribute
     # to be set to False and included in the <repository> tag when a repository was exported along with
     # its dependencies.  The following will eliminate this problematic attribute upon import.
     prior_installation_required = elem.get('prior_installation_required')
     if prior_installation_required is not None and not asbool(prior_installation_required):
         del elem.attrib['prior_installation_required']
     sub_elems = [child_elem for child_elem in list(elem)]
     if len(sub_elems) > 0:
         # At this point, a <repository> tag will point only to a package.
         # <package name="xorg_macros" version="1.17.1" />
         # Coerce the list to an odict().
         sub_elements = odict()
         packages = []
         for sub_elem in sub_elems:
             sub_elem_type = sub_elem.tag
             sub_elem_name = sub_elem.get('name')
             sub_elem_version = sub_elem.get('version')
             if sub_elem_type and sub_elem_name and sub_elem_version:
                 packages.append((sub_elem_name, sub_elem_version))
         sub_elements['packages'] = packages
     else:
         # Set to None.
         sub_elements = None
     if self.unpopulate:
         # We're exporting the repository, so eliminate all toolshed and changeset_revision attributes
         # from the <repository> tag.
         if toolshed or changeset_revision:
             attributes = odict()
             attributes['name'] = name
             attributes['owner'] = owner
             prior_installation_required = elem.get('prior_installation_required')
             if asbool(prior_installation_required):
                 attributes['prior_installation_required'] = 'True'
             new_elem = xml_util.create_element('repository', attributes=attributes, sub_elements=sub_elements)
             altered = True
         return altered, new_elem, error_message
     # From here on we're populating the toolshed and changeset_revision attributes if necessary.
     if not toolshed:
         # Default the setting to the current tool shed.
         toolshed = str(url_for('/', qualified=True)).rstrip('/')
         elem.attrib['toolshed'] = toolshed
         altered = True
     if not changeset_revision:
         # Populate the changeset_revision attribute with the latest installable metadata revision for
         # the defined repository.  We use the latest installable revision instead of the latest metadata
         # revision to ensure that the contents of the revision are valid.
         repository = repository_util.get_repository_by_name_and_owner(self.app, name, owner)
         if repository:
             lastest_installable_changeset_revision = \
                 metadata_util.get_latest_downloadable_changeset_revision(self.app, repository)
             if lastest_installable_changeset_revision != hg_util.INITIAL_CHANGELOG_HASH:
                 elem.attrib['changeset_revision'] = lastest_installable_changeset_revision
                 altered = True
             else:
                 error_message = 'Invalid latest installable changeset_revision %s ' % \
                     str(lastest_installable_changeset_revision)
                 error_message += 'retrieved for repository %s owned by %s.  ' % (str(name), str(owner))
         else:
             error_message = 'Unable to locate repository with name %s and owner %s.  ' % (str(name), str(owner))
     return altered, elem, error_message
Ejemplo n.º 5
0
def build_readme_files_dict( app, repository, changeset_revision, metadata, tool_path=None ):
    """
    Return a dictionary of valid readme file name <-> readme file content pairs for all readme files defined in the received metadata.  Since the
    received changeset_revision (which is associated with the received metadata) may not be the latest installable changeset revision, the README
    file contents may not be available on disk.  This method is used by both Galaxy and the Tool Shed.
    """
    if app.name == 'galaxy':
        can_use_disk_files = True
    else:
        repo = hg_util.get_repo_for_repository( app, repository=repository, repo_path=None, create=False )
        latest_downloadable_changeset_revision = metadata_util.get_latest_downloadable_changeset_revision( app, repository, repo )
        can_use_disk_files = changeset_revision == latest_downloadable_changeset_revision
    readme_files_dict = {}
    if metadata:
        if 'readme_files' in metadata:
            for relative_path_to_readme_file in metadata[ 'readme_files' ]:
                readme_file_name = os.path.split( relative_path_to_readme_file )[ 1 ]
                if can_use_disk_files:
                    if tool_path:
                        full_path_to_readme_file = os.path.abspath( os.path.join( tool_path, relative_path_to_readme_file ) )
                    else:
                        full_path_to_readme_file = os.path.abspath( relative_path_to_readme_file )
                    text = None
                    try:
                        f = open( full_path_to_readme_file, 'r' )
                        text = unicodify( f.read() )
                        f.close()
                    except Exception as e:
                        log.exception( "Error reading README file '%s' from disk", str( relative_path_to_readme_file ) )
                        text = None
                    if text:
                        text_of_reasonable_length = basic_util.size_string( text )
                        if text_of_reasonable_length.find( '.. image:: ' ) >= 0:
                            # Handle image display for README files that are contained in repositories in the tool shed or installed into Galaxy.
                            lock = threading.Lock()
                            lock.acquire( True )
                            try:
                                text_of_reasonable_length = suc.set_image_paths( app,
                                                                                 app.security.encode_id( repository.id ),
                                                                                 text_of_reasonable_length )
                            except Exception as e:
                                log.exception( "Exception in build_readme_files_dict, so images may not be properly displayed" )
                            finally:
                                lock.release()
                        if readme_file_name.endswith( '.rst' ):
                            text_of_reasonable_length = Template( rst_to_html( text_of_reasonable_length ),
                                                                  input_encoding='utf-8',
                                                                  output_encoding='utf-8',
                                                                  default_filters=[ 'decode.utf8' ],
                                                                  encoding_errors='replace' )
                            text_of_reasonable_length = text_of_reasonable_length.render( static_path=web.url_for( '/static' ),
                                                                                          host_url=web.url_for( '/', qualified=True ) )
                            text_of_reasonable_length = unicodify( text_of_reasonable_length )
                        else:
                            text_of_reasonable_length = basic_util.to_html_string( text_of_reasonable_length )
                        readme_files_dict[ readme_file_name ] = text_of_reasonable_length
                else:
                    # We must be in the tool shed and have an old changeset_revision, so we need to retrieve the file contents from the repository manifest.
                    ctx = hg_util.get_changectx_for_changeset( repo, changeset_revision )
                    if ctx:
                        fctx = hg_util.get_file_context_from_ctx( ctx, readme_file_name )
                        if fctx and fctx not in [ 'DELETED' ]:
                            try:
                                text = unicodify( fctx.data() )
                                readme_files_dict[ readme_file_name ] = basic_util.size_string( text )
                            except Exception as e:
                                log.exception( "Error reading README file '%s' from repository manifest: %s" %
                                               ( str( relative_path_to_readme_file ), str( e ) ) )
    return readme_files_dict