Ejemplo n.º 1
0
def filter_requirements_with_extras(
    requirment_lines: List[Union[str, Dict[str, Union[str, List[str]]]]],
    extras: Sequence[str],
) -> List[str]:
    result = []
    extras_in_meta = []
    for req in requirment_lines:
        if isinstance(req, dict):
            if req.get("extra"):
                extras_in_meta.append(req["extra"])
            if not req.get("extra") or req.get("extra") in extras:
                marker = f"; {req['environment']}" if req.get(
                    "environment") else ""
                result.extend(f"{line}{marker}"
                              for line in req.get("requires", []))
        else:
            _r = parse_requirement(req)
            if not _r.marker:
                result.append(req)
            else:
                elements, rest = split_marker_extras(_r.marker)
                extras_in_meta.extend(e for e in elements)
                _r.marker = rest
                if not elements or set(extras) & set(elements):
                    result.append(_r.as_line())

    extras_not_found = [e for e in extras if e not in extras_in_meta]
    if extras_not_found:
        warnings.warn(ExtrasError(extras_not_found), stacklevel=2)

    return result
Ejemplo n.º 2
0
def get_requirements_from_dist(dist: EggInfoDistribution,
                               extras: Sequence[str]) -> List[str]:
    """Get requirements of a distribution, with given extras."""
    extras_in_metadata = []
    result = []
    dep_map = dist._build_dep_map()
    for extra, reqs in dep_map.items():
        reqs = [Requirement.from_pkg_requirement(r) for r in reqs]
        if not extra:
            # requirements without extras are always required.
            result.extend(r.as_line() for r in reqs)
        else:
            new_extra, _, marker = extra.partition(":")
            extras_in_metadata.append(new_extra.strip())
            # Only include requirements that match one of extras.
            if not new_extra.strip() or safe_extra(
                    new_extra.strip()) in extras:
                marker = Marker(marker) if marker else None
                for r in reqs:
                    r.marker = marker
                    result.append(r.as_line())
    extras_not_found = [e for e in extras if e not in extras_in_metadata]
    if extras_not_found:
        warnings.warn(ExtrasError(extras_not_found))
    return result
Ejemplo n.º 3
0
 def get_dependencies_from_metadata(self) -> list[str]:
     """Get the dependencies of a candidate from metadata."""
     extras = self.req.extras or ()
     metadata = self.get_metadata()
     if self.req.editable:
         if not metadata:
             return []
         return get_requirements_from_dist(self.ireq.get_dist(), extras)
     elif hasattr(metadata, "install_requires"):
         requires = metadata.install_requires or []  # type: ignore
         extras_not_found = set()
         for extra in extras:
             try:
                 requires.extend(
                     (metadata.extras_require or {})[extra]  # type: ignore
                 )
             except KeyError:
                 extras_not_found.add(extra)
         if extras_not_found:
             warnings.warn(ExtrasError(sorted(extras_not_found)))
         return sorted(set(requires))
     else:
         return filter_requirements_with_extras(
             metadata.run_requires, extras  # type: ignore
         )
Ejemplo n.º 4
0
def filter_requirements_with_extras(
    requirement_lines: list[str],
    extras: Sequence[str],
) -> list[str]:
    result: list[str] = []
    extras_in_meta: list[str] = []
    for req in requirement_lines:
        _r = parse_requirement(req)
        if not _r.marker:
            result.append(req)
        else:
            elements, rest = split_marker_extras(_r.marker)
            extras_in_meta.extend(elements)
            _r.marker = Marker(str(rest)) if rest else None
            if not elements or set(extras) & set(elements):
                result.append(_r.as_line())

    extras_not_found = [e for e in extras if e not in extras_in_meta]
    if extras_not_found:
        warnings.warn(ExtrasError(extras_not_found))

    return result