Esempio 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
Esempio n. 2
0
def filter_requirements_with_extras(
    project_name: str,
    requirement_lines: list[str],
    extras: Sequence[str],
    include_default: bool = False,
) -> list[str]:
    """Filter the requirements with extras.
    If extras are given, return those with matching extra markers.
    Otherwise, return those without extra markers.
    """
    result: list[str] = []
    extras_in_meta: set[str] = set()
    for req in requirement_lines:
        _r = parse_requirement(req)
        if _r.marker:
            req_extras, rest = split_marker_extras(str(_r.marker))
            if req_extras:
                extras_in_meta.update(req_extras)
                _r.marker = Marker(rest) if rest else None
        else:
            req_extras = set()
        if (req_extras and not req_extras.isdisjoint(extras)
                or not req_extras and (include_default or not extras)):
            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(ExtrasWarning(project_name, extras_not_found))

    return result
Esempio n. 3
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
Esempio n. 4
0
def test_split_marker_extras(original, extras, rest):
    result = split_marker_extras(Marker(original))
    assert result == (extras, Marker(rest) if rest else None)
Esempio n. 5
0
def test_split_marker_extras(original, extras, rest):
    result = split_marker_extras(original)
    assert result == (extras, rest)