This is an automated email from the ASF dual-hosted git repository. akitouni pushed a commit to branch abderrahim/buildstream-mirrors-merge in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 6cc6afc605a83d4e3337a944fd0af2341c99e798 Author: Tristan van Berkom <[email protected]> AuthorDate: Sun Feb 25 16:07:49 2024 +0900 Source/SourceMirror: Extend API with extra_data --- src/buildstream/source.py | 25 +++++++++++++++++-------- src/buildstream/sourcemirror.py | 14 ++++++++++++-- tests/frontend/project/sourcemirrors/mirror.py | 13 ++++++++++++- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/buildstream/source.py b/src/buildstream/source.py index b9911bba8..9ca413645 100644 --- a/src/buildstream/source.py +++ b/src/buildstream/source.py @@ -217,7 +217,7 @@ Class Reference import os from contextlib import contextmanager -from typing import Iterable, Iterator, Optional, Tuple, TYPE_CHECKING +from typing import Iterable, Iterator, Optional, Tuple, Dict, Any, Set, TYPE_CHECKING from . import _yaml, utils from .node import MappingNode @@ -235,8 +235,6 @@ from ._variables import Variables from .sourcemirror import SourceMirror if TYPE_CHECKING: - from typing import Any, Dict, Set - # pylint: disable=cyclic-import from ._context import Context from ._project import Project @@ -340,7 +338,7 @@ class Source(Plugin): """ # The defaults from the project - __defaults = None # type: Optional[Dict[str, Any]] + __defaults: Optional[Dict[str, Any]] = None BST_REQUIRES_PREVIOUS_SOURCES_TRACK = False """Whether access to previous sources is required during track @@ -415,7 +413,7 @@ class Source(Plugin): self.__alias_override = alias_override # Tuple of alias and its override to use instead self.__expected_alias = None # The primary alias # Set of marked download URLs - self.__marked_urls = set() # type: Set[str] + self.__marked_urls: Set[str] = set() # The active SourceMirror in context of fetch/track self.__active_mirror: Optional[SourceMirror] = active_mirror @@ -702,7 +700,13 @@ class Source(Plugin): return self.__mirror_directory def translate_url( - self, url: str, *, alias_override: Optional[str] = None, primary: bool = True, suffix: Optional[str] = None + self, + url: str, + *, + alias_override: Optional[str] = None, + primary: bool = True, + suffix: Optional[str] = None, + extra_data: Optional[Dict[str, Any]] = None, ) -> str: """Translates the given url which may be specified with an alias into a fully qualified url. @@ -712,6 +716,7 @@ class Source(Plugin): alias_override: Optionally, an URI to override the alias with. primary: Whether this is the primary URL for the source. suffix: an optional suffix to append to the URL (*Since: 2.2*) + extra_data: Additional data provided by :class:`SourceMirror <buildstream.sourcemirror.SourceMirror>` (*Since: 2.2*) Returns: The fully qualified URL, with aliases resolved @@ -774,9 +779,13 @@ class Source(Plugin): # Delegate the URL translation to the SourceMirror plugin # return self.__active_mirror.translate_url( - project.name, url_alias, project_alias_url, alias_override, url_body + project_name=project.name, + alias=url_alias, + alias_url=project_alias_url, + alias_substitute_url=alias_override, + source_url=url_body, + extra_data=extra_data, ) - else: return project.translate_url(url, first_pass=self.__first_pass) diff --git a/src/buildstream/sourcemirror.py b/src/buildstream/sourcemirror.py index a535fe982..2d2ea8405 100644 --- a/src/buildstream/sourcemirror.py +++ b/src/buildstream/sourcemirror.py @@ -45,7 +45,7 @@ Class Reference --------------- """ -from typing import Optional, Dict, List, TYPE_CHECKING +from typing import Optional, Dict, List, Any, TYPE_CHECKING from .node import MappingNode, SequenceNode from .plugin import Plugin @@ -120,16 +120,26 @@ class SourceMirror(Plugin): # Public API # ########################################################## def translate_url( - self, project_name: str, alias: str, alias_url: str, alias_substitute_url: Optional[str], source_url: str + self, + *, + project_name: str, + alias: str, + alias_url: str, + alias_substitute_url: Optional[str], + source_url: str, + extra_data: Optional[Dict[str, Any]], ) -> str: """Produce an alternative url for `url` for the given alias. + This method implements the behavior of :func:`Source.translate_url() <buildstream.source.Source.translate_url>`. + Args: project_name: The name of the project this URL comes from alias: The alias to translate for alias_url: The default URL configured for this alias in the originating project alias_substitute_url: The alias substitute URL configured in the mirror configuration, or None source_url: The URL as specified by original source YAML, excluding the alias + extra_data: An optional extra dictionary to return additional data """ # # Default implementation behaves in the same way we behaved before diff --git a/tests/frontend/project/sourcemirrors/mirror.py b/tests/frontend/project/sourcemirrors/mirror.py index 99b16e84f..211c736ef 100644 --- a/tests/frontend/project/sourcemirrors/mirror.py +++ b/tests/frontend/project/sourcemirrors/mirror.py @@ -1,3 +1,5 @@ +from typing import Optional, Dict, Any + from buildstream import SourceMirror, MappingNode @@ -17,7 +19,16 @@ class Sample(SourceMirror): for alias_name, url_list in aliases.items(): self.aliases[alias_name] = url_list.as_str_list() - def translate_url(self, project_name, alias, alias_url, alias_substitute_url, source_url): + def translate_url( + self, + *, + project_name: str, + alias: str, + alias_url: str, + alias_substitute_url: Optional[str], + source_url: str, + extra_data: Optional[Dict[str, Any]], + ) -> str: return self.aliases[alias][0] + source_url
