Exemplo n.º 1
0
def make_install_requirement(name, version, extras, markers, constraint=False):
    """make_install_requirement Generates an :class:`~pip._internal.req.req_install.InstallRequirement`.

    Create an InstallRequirement from the supplied metadata.

    :param name: The requirement's name.
    :type name: str
    :param version: The requirement version (must be pinned).
    :type version: str.
    :param extras: The desired extras.
    :type extras: list[str]
    :param markers: The desired markers, without a preceding semicolon.
    :type markers: str
    :param constraint: Whether to flag the requirement as a constraint, defaults to False.
    :param constraint: bool, optional
    :return: A generated InstallRequirement
    :rtype: :class:`~pip._internal.req.req_install.InstallRequirement`
    """

    # If no extras are specified, the extras string is blank
    extras_string = ""
    if extras:
        # Sort extras for stability
        extras_string = "[{}]".format(",".join(sorted(extras)))

    if not markers:
        return InstallRequirement.from_line(str('{}{}=={}'.format(
            name, extras_string, version)),
                                            constraint=constraint)
    else:
        return InstallRequirement.from_line(str('{}{}=={}; {}'.format(
            name, extras_string, version, str(markers))),
                                            constraint=constraint)
Exemplo n.º 2
0
 def as_ireq(self):
     ireq_line = self.as_line(include_hashes=False)
     if self.editable or self.req.editable:
         if ireq_line.startswith("-e "):
             ireq_line = ireq_line[len("-e "):]
         ireq = InstallRequirement.from_editable(ireq_line)
     else:
         ireq = InstallRequirement.from_line(ireq_line)
     if not getattr(ireq, "req", None):
         ireq.req = self.req.req
     else:
         ireq.req.extras = self.req.req.extras
         ireq.req.marker = self.req.req.marker
     return ireq
Exemplo n.º 3
0
    def get_name(self):
        loc = self.path or self.uri
        if loc:
            self._uri_scheme = "path" if self.path else "uri"
        name = None
        if self.link and self.link.egg_fragment:
            return self.link.egg_fragment
        elif self.link and self.link.is_wheel:
            return Wheel(self.link.filename).name
        if (self._uri_scheme != "uri" and self.path and self.setup_path
                and self.setup_path.exists()):
            from setuptools.dist import distutils

            old_curdir = os.path.abspath(os.getcwd())
            try:
                os.chdir(str(self.setup_path.parent))
                dist = distutils.core.run_setup(self.setup_path.as_posix())
                name = dist.get_name()
            except (FileNotFoundError, IOError) as e:
                dist = None
            except Exception as e:
                from pip_shims.shims import InstallRequirement, make_abstract_dist

                try:
                    if not isinstance(Path, self.path):
                        _path = Path(self.path)
                    else:
                        _path = self.path
                    if self.editable:
                        _ireq = InstallRequirement.from_editable(
                            _path.as_uri())
                    else:
                        _ireq = InstallRequirement.from_line(_path.as_posix())
                    dist = make_abstract_dist(_ireq).get_dist()
                    name = dist.project_name
                except (TypeError, ValueError, AttributeError) as e:
                    dist = None
            finally:
                os.chdir(old_curdir)
        hashed_loc = hashlib.sha256(loc.encode("utf-8")).hexdigest()
        hashed_name = hashed_loc[-7:]
        if not name or name == "UNKNOWN":
            self._has_hashed_name = True
            name = hashed_name
        if self.link and not self._has_hashed_name:
            self.link = Link("{0}#egg={1}".format(self.link.url, name))
        return name