def _dist_from_eggfile(filename, logger, observer):
    """ Create distribution by unpacking egg file. """
    if not os.path.exists(filename):
        msg = "'%s' not found." % filename
        observer.exception(msg)
        raise ValueError(msg)

    if not zipfile.is_zipfile(filename):
        msg = "'%s' is not an egg/zipfile." % filename
        observer.exception(msg)
        raise ValueError(msg)

    # Extract files.
    archive = zipfile.ZipFile(filename, 'r', allowZip64=True)
    try:
        name = archive.read('EGG-INFO/top_level.txt').split('\n')[0]
        logger.log(LOG_DEBUG2, "    name '%s'", name)

        if observer.observer is not None:
            # Collect totals.
            total_files = 0.
            total_bytes = 0.
            for info in archive.infolist():
                fname = info.filename
                # Just being defensive.
                if not fname.startswith(name) and \
                   not fname.startswith('EGG-INFO'):  #pragma no cover
                    continue
                total_files += 1
                total_bytes += info.file_size
        else:
            total_files = 1.  # Avoid divide-by-zero.
            total_bytes = 1.

        files = 0.
        size = 0.
        for info in archive.infolist():
            fname = info.filename
            # Just being defensive.
            if not fname.startswith(name) and \
               not fname.startswith('EGG-INFO'):  #pragma no cover
                continue

            observer.extract(fname, files/total_files, size/total_bytes)
            dirname = os.path.dirname(fname)
            if dirname == 'EGG-INFO':
                # Extract EGG-INFO as subdirectory.
                archive.extract(fname, name)
            else:
                archive.extract(fname)
                if sys.platform != 'win32':
                    # Set permissions, extract() doesn't.
                    rwx = (info.external_attr >> 16) & 0777
                    if rwx:
                        os.chmod(fname, rwx)  # Only if something valid.
            files += 1
            size += info.file_size

    finally:
        archive.close()

    # Create distribution from extracted files.
    location = os.getcwd()
    egg_info = os.path.join(location, name, 'EGG-INFO')
    provider = pkg_resources.PathMetadata(location, egg_info)
    dist = pkg_resources.Distribution.from_location(location,
                                                    os.path.basename(filename),
                                                    provider)

    logger.log(LOG_DEBUG2, '    project_name: %s', dist.project_name)
    logger.log(LOG_DEBUG2, '    version: %s', dist.version)
    logger.log(LOG_DEBUG2, '    py_version: %s', dist.py_version)
    logger.log(LOG_DEBUG2, '    platform: %s', dist.platform)
    logger.log(LOG_DEBUG2, '    requires:')
    for req in dist.requires():
        logger.log(LOG_DEBUG2, '        %s', req)

    # If any module didn't have a distribution, check that we can import it.
    if provider.has_metadata('openmdao_orphans.txt'):
        errors = 0
        orphan_names = []
        for mod in provider.get_metadata_lines('openmdao_orphans.txt'):
            mod = mod.strip()
            logger.log(LOG_DEBUG2, "    checking for 'orphan' module: %s", mod)
            try:
                __import__(mod)
            # Difficult to generate a distribution that can't be reloaded.
            except __HOLE__:  #pragma no cover
                logger.error("Can't import %s, which didn't have a known"
                             " distribution when the egg was written.", mod)
                orphan_names.append(mod)
                errors += 1
        # Difficult to generate a distribution that can't be reloaded.
        if errors:  #pragma no cover
            plural = 's' if errors > 1 else ''
            msg = "Couldn't import %d 'orphan' module%s: %s." \
                  % (errors, plural, orphan_names)
            observer.exception(msg)
            raise RuntimeError(msg)

    return (name, dist)
dataset/ETHPy150Open OpenMDAO/OpenMDAO-Framework/openmdao.util/src/openmdao/util/eggloader.py/_dist_from_eggfile