-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from tdejager/feat/get-all-url-sources
feat: get all url sources
- Loading branch information
Showing
8 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import typing | ||
from typing import Any, Iterator, Mapping, NotRequired, TypedDict | ||
|
||
from .conditional_list import ConditionalList, visit_conditional_list | ||
|
||
OptionalUrlList = str | list[str] | None | ||
|
||
|
||
class Source(TypedDict): | ||
url: NotRequired[str] | ||
|
||
|
||
def get_all_url_sources(recipe: Mapping[Any, Any]) -> Iterator[str]: | ||
""" | ||
Get all url sources from the recipe. This can be from a list of sources, | ||
a single source, or conditional and its branches. | ||
Arguments | ||
--------- | ||
* `recipe` - The recipe to inspect. This should be a yaml object. | ||
Returns | ||
------- | ||
A list of sources. | ||
""" | ||
|
||
sources = recipe.get("source", None) | ||
sources = typing.cast(ConditionalList[Source], sources) | ||
|
||
# Try getting all url top-level sources | ||
if sources is not None: | ||
sources = visit_conditional_list(sources, None) | ||
for source in sources: | ||
if "url" in source: | ||
yield source["url"] | ||
|
||
outputs = recipe.get("outputs", None) | ||
if outputs is None: | ||
return | ||
|
||
outputs = visit_conditional_list(outputs, None) | ||
for output in outputs: | ||
sources = output.get("source", None) | ||
sources = typing.cast(ConditionalList[Source], sources) | ||
if sources is None: | ||
return | ||
sources = visit_conditional_list(sources, None) | ||
for source in sources: | ||
if "url" in source: | ||
yield source["url"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source: | ||
- if: windows | ||
then: | ||
- url: https://foo.com | ||
else: | ||
- url: https://bar.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source: | ||
- url: | ||
- "https://foo.com" | ||
- "https://bar.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source: | ||
- url: https://foo.com | ||
sha256: 1234567890abcdef | ||
- url: https://bar.com | ||
sha256: 1234567890abcdef |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
outputs: | ||
source: | ||
- if: foo | ||
then: | ||
- url: https://foo.com | ||
- if: bla | ||
then: | ||
- url: https://bar.com | ||
else: | ||
- url: https://baz.com | ||
- url: https://qux.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
source: | ||
- url: https://foo.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
from rattler_build_conda_compat.loader import load_yaml | ||
from rattler_build_conda_compat.recipe_sources import get_all_url_sources | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("partial_recipe", "expected_output"), | ||
[ | ||
("single_source.yaml", ["https://foo.com"]), | ||
("multiple_sources.yaml", ["https://foo.com", "https://bar.com"]), | ||
("if_then_source.yaml", ["https://foo.com", "https://bar.com"]), | ||
( | ||
"outputs_source.yaml", | ||
["https://foo.com", "https://bar.com", "https://baz.com", "https://qux.com"], | ||
), | ||
], | ||
) | ||
def test_recipe_sources(partial_recipe: str, expected_output: list[str]) -> None: | ||
"""Test that the recipe sources are correctly extracted from the recipe""" | ||
path = Path(f"{Path(__file__).parent}/data/{partial_recipe}") | ||
recipe = load_yaml(path.read_text()) | ||
assert list(get_all_url_sources(recipe)) == expected_output |