Python pkg_resources.to_filename() Examples

The following are 4 code examples of pkg_resources.to_filename(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: wheel.py    From pdm with MIT License 5 votes vote down vote up
def wheel_filename(self) -> str:
        name = to_filename(self.meta.project_name)
        version = to_filename(safe_version(self.meta.version))
        return f"{name}-{version}-{self.tag}.whl" 
Example #2
Source File: wheel.py    From pdm with MIT License 5 votes vote down vote up
def dist_info_name(self) -> str:
        name = to_filename(self.meta.project_name)
        version = to_filename(safe_version(self.meta.version))
        return f"{name}-{version}.dist-info" 
Example #3
Source File: sdist.py    From pdm with MIT License 5 votes vote down vote up
def build(self, build_dir: str, **kwargs):
        if not os.path.exists(build_dir):
            os.makedirs(build_dir, exist_ok=True)

        stream.echo("- Building {}...".format(stream.cyan("sdist")))
        version = to_filename(safe_version(self.meta.version))

        target = os.path.join(
            build_dir, "{}-{}.tar.gz".format(self.meta.project_name, version)
        )
        tar = tarfile.open(target, mode="w:gz", format=tarfile.PAX_FORMAT)

        try:
            tar_dir = "{}-{}".format(self.meta.project_name, version)

            files_to_add = self.find_files_to_add(True)

            for relpath in files_to_add:
                tar.add(
                    relpath,
                    arcname=os.path.join(tar_dir, str(relpath)),
                    recursive=False,
                )
                stream.echo(f" - Adding: {relpath}", verbosity=stream.DETAIL)

            fd, temp_name = tempfile.mkstemp(prefix="pkg-info")
            pkg_info = self.format_pkginfo(False).encode("utf-8")
            with open(fd, "wb") as f:
                f.write(pkg_info)
            tar.add(
                temp_name, arcname=os.path.join(tar_dir, "PKG-INFO"), recursive=False
            )
            stream.echo(" - Adding: PKG-INFO", verbosity=stream.DETAIL)
        finally:
            tar.close()

        stream.echo("- Built {}".format(stream.cyan(os.path.basename(target))))

        return target 
Example #4
Source File: package_tools.py    From thanks with Apache License 2.0 5 votes vote down vote up
def get_local_dist_metadata_filepath(dist):
    # Dist filename syntax
    # name ["-" version ["-py" pyver ["-" required_platform]]] "." ext
    # https://setuptools.readthedocs.io/en/latest/formats.html#filename-embedded-metadata

    def valid_component(component):
        return component[1]

    # Stop taking filename components at the first missing/invalid component
    filename_component = takewhile(valid_component, (
        ('',    pkg_resources.to_filename(pkg_resources.safe_name(dist.project_name))),
        ('-',   pkg_resources.to_filename(pkg_resources.safe_version(dist.version))),
        ('-py', dist.py_version),
        ('-',   dist.platform),
    ))
    filename = ''.join(chain(*filename_component))

    if isinstance(dist, pkg_resources.EggInfoDistribution):
        ext = 'egg-info'
        metadata_file = 'PKG-INFO'
    elif isinstance(dist, pkg_resources.DistInfoDistribution):
        ext = 'dist-info'
        metadata_file = 'METADATA'
    elif isinstance(dist, pkg_resources.Distribution):
        ext = os.path.join('egg', 'EGG-INFO')
        metadata_file = 'PKG-INFO'
    else:
        ext = None
        metadata_file = None

    filename = '{}.{}'.format(filename, ext)
    path = os.path.join(dist.location, filename, metadata_file)

    if ext:
        return path
    else:
        return None