Python pkg_resources.safe_name() Examples

The following are 30 code examples of pkg_resources.safe_name(). 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: model.py    From guildai with Apache License 2.0 6 votes vote down vote up
def _init_project_name(guildfile):
        """Returns a project name for a guildfile distribution.

        Guildfile distribution project names are of the format:

            '.guildfile.' + ESCAPED_GUILDFILE_PATH

        ESCAPED_GUILDFILE_PATH is a 'safe' project name (i.e. will not be
        modified in a call to `pkg_resources.safe_name`) that, when
        unescaped using `unescape_project_name`, is the relative path of
        the directory containing the guildfile. The modefile name itself
        (e.g. 'guild.yml') is not contained in the path.

        Guildfile paths are relative to the current working directory
        (i.e. the value of os.getcwd() at the time they are generated) and
        always start with '.'.
        """
        pkg_path = os.path.relpath(guildfile.dir, config.cwd())
        if pkg_path[0] != ".":
            pkg_path = os.path.join(".", pkg_path)
        safe_path = escape_project_name(pkg_path)
        return ".guildfile.%s" % safe_path 
Example #2
Source File: utils.py    From requirementslib with MIT License 6 votes vote down vote up
def get_name_variants(pkg):
    # type: (STRING_TYPE) -> Set[STRING_TYPE]
    """
    Given a packager name, get the variants of its name for both the canonicalized
    and "safe" forms.

    :param AnyStr pkg: The package to lookup
    :returns: A list of names.
    :rtype: Set
    """

    if not isinstance(pkg, six.string_types):
        raise TypeError("must provide a string to derive package names")
    from pkg_resources import safe_name
    from packaging.utils import canonicalize_name

    pkg = pkg.lower()
    names = {safe_name(pkg), canonicalize_name(pkg), pkg.replace("-", "_")}
    return names 
Example #3
Source File: resolver.py    From pipenv with MIT License 6 votes vote down vote up
def find_site_path(pkg, site_dir=None):
    import pkg_resources
    if site_dir is not None:
        site_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    working_set = pkg_resources.WorkingSet([site_dir] + sys.path[:])
    for dist in working_set:
        root = dist.location
        base_name = dist.project_name if dist.project_name else dist.key
        name = None
        if "top_level.txt" in dist.metadata_listdir(""):
            name = next(iter([l.strip() for l in dist.get_metadata_lines("top_level.txt") if l is not None]), None)
        if name is None:
            name = pkg_resources.safe_name(base_name).replace("-", "_")
        if not any(pkg == _ for _ in [base_name, name]):
            continue
        path_options = [name, "{0}.py".format(name)]
        path_options = [os.path.join(root, p) for p in path_options if p is not None]
        path = next(iter(p for p in path_options if os.path.exists(p)), None)
        if path is not None:
            return (dist, path)
    return (None, None) 
Example #4
Source File: utils.py    From pipenv with MIT License 6 votes vote down vote up
def get_name_variants(pkg):
    # type: (STRING_TYPE) -> Set[STRING_TYPE]
    """
    Given a packager name, get the variants of its name for both the canonicalized
    and "safe" forms.

    :param AnyStr pkg: The package to lookup
    :returns: A list of names.
    :rtype: Set
    """

    if not isinstance(pkg, six.string_types):
        raise TypeError("must provide a string to derive package names")
    from pkg_resources import safe_name
    from packaging.utils import canonicalize_name

    pkg = pkg.lower()
    names = {safe_name(pkg), canonicalize_name(pkg), pkg.replace("-", "_")}
    return names 
Example #5
Source File: bdist_wheel.py    From android_universal with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #6
Source File: shell.py    From quark with Apache License 2.0 5 votes vote down vote up
def get_pip_pkg(name, stage=None, command="pip"):
    try:
        output = call(command, "show", pkg_resources.safe_name(name), stage=stage, errok=True)
        for line in output.split("\n"):
            if line.startswith("Location: "):
                return os.path.join(line.split(": ")[1], name)
    except ShellError:
        return None 
Example #7
Source File: dist.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #8
Source File: bdist_wheel.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #9
Source File: name_convertor.py    From pyp2rpm with MIT License 5 votes vote down vote up
def rpm_name(self, name, python_version=settings.DEFAULT_PYTHON_VERSION,
                 pkg_name=False):
        if pkg_name:
            return super(AutoProvidesNameConvertor, self).rpm_name(
                name, python_version)
        canonical_name = safe_name(name).lower()
        return "python{0}dist({1})".format(python_version, canonical_name) 
Example #10
Source File: bdist_wheel.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #11
Source File: dist.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #12
Source File: dist.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #13
Source File: parameters.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def substitute(target_relpath):
    filename = os.path.basename(target_relpath)
    name, ext = os.path.splitext(filename)
    wheel_distribution_name, package_version, python_tag, _, _ = name.split('-')

    if not wheel_distribution_name.startswith('datadog_'):
        raise NonDatadogPackage(wheel_distribution_name)

    standard_distribution_name = safe_name(wheel_distribution_name)

    # These names are the exceptions. In this case, the wheel distribution name
    # matches exactly the directory name of the check on GitHub.
    if wheel_distribution_name in EXCEPTIONS:
        package_github_dir = wheel_distribution_name
    # FIXME: This is the only other package at the time of writing (Sep 7 2018)
    # that does not replace `-` with `_`.
    elif wheel_distribution_name == 'datadog_go_metro':
        package_github_dir = 'go-metro'
    # Otherwise, the prefix of the wheel distribution name is expected to be
    # "datadog-", and the directory name of the check on GitHub is expected not
    # have this prefix.
    else:
        package_github_dir = wheel_distribution_name[8:]

    return {
        'package_version': package_version,
        'package_github_dir': package_github_dir,
        'python_tag': python_tag,
        'standard_distribution_name': standard_distribution_name,
        'wheel_distribution_name': wheel_distribution_name,
    } 
Example #14
Source File: dist.py    From Flask with Apache License 2.0 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #15
Source File: bdist_wheel.py    From keras-lambda with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #16
Source File: dist.py    From android_universal with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #17
Source File: dist.py    From Flask with Apache License 2.0 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #18
Source File: bdist_wheel.py    From syntheticmass with Apache License 2.0 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #19
Source File: dist.py    From setuptools with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #20
Source File: dist.py    From datafari with Apache License 2.0 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #21
Source File: bdist_wheel.py    From Ansible with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #22
Source File: dist.py    From Ansible with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #23
Source File: bdist_wheel.py    From ImageFusion with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #24
Source File: dist.py    From ImageFusion with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #25
Source File: bdist_wheel.py    From rules_pip with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #26
Source File: dist.py    From rules_pip with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #27
Source File: bdist_wheel.py    From planespotter with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #28
Source File: dist.py    From planespotter with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist 
Example #29
Source File: bdist_wheel.py    From Flask-P2P with MIT License 5 votes vote down vote up
def safer_name(name):
    return safe_name(name).replace('-', '_') 
Example #30
Source File: dist.py    From Flask-P2P with MIT License 5 votes vote down vote up
def patch_missing_pkg_info(self, attrs):
        # Fake up a replacement for the data that would normally come from
        # PKG-INFO, but which might not yet be built if this is a fresh
        # checkout.
        #
        if not attrs or 'name' not in attrs or 'version' not in attrs:
            return
        key = pkg_resources.safe_name(str(attrs['name'])).lower()
        dist = pkg_resources.working_set.by_key.get(key)
        if dist is not None and not dist.has_metadata('PKG-INFO'):
            dist._version = pkg_resources.safe_version(str(attrs['version']))
            self._patched_dist = dist