diff options
author | James Ennis <james.ennis@codethink.co.uk> | 2019-08-15 09:27:02 +0100 |
---|---|---|
committer | James Ennis <james.ennis@codethink.co.uk> | 2019-08-27 12:12:13 +0100 |
commit | ef2015096ac039f541be5401a39cf25477afefca (patch) | |
tree | c13530df2af6251023c0b7e828819f2102a7ee90 /src/buildstream/_artifactelement.py | |
parent | a8d68c9d313252454a9f92e00b8758a0d2d674ef (diff) | |
download | buildstream-ef2015096ac039f541be5401a39cf25477afefca.tar.gz |
Load artifact refs the same way we load element names
Diffstat (limited to 'src/buildstream/_artifactelement.py')
-rw-r--r-- | src/buildstream/_artifactelement.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/buildstream/_artifactelement.py b/src/buildstream/_artifactelement.py index 35ef89574..cfd3c29c8 100644 --- a/src/buildstream/_artifactelement.py +++ b/src/buildstream/_artifactelement.py @@ -32,6 +32,9 @@ from .types import Scope # ref (str): The artifact ref # class ArtifactElement(Element): + + __instantiated_artifacts = {} # A hash of ArtifactElement by ref + def __init__(self, context, ref): _, element, key = verify_artifact_ref(ref) @@ -44,6 +47,52 @@ class ArtifactElement(Element): super().__init__(context, project, meta, plugin_conf) + # _new_from_artifact_ref(): + # + # Recursively instantiate a new ArtifactElement instance, and its + # dependencies from an artifact ref + # + # Args: + # ref (String): The artifact ref + # context (Context): The Context object + # task (Task): A task object to report progress to + # + # Returns: + # (ArtifactElement): A newly created Element instance + # + @classmethod + def _new_from_artifact_ref(cls, ref, context, task=None): + + if ref in cls.__instantiated_artifacts: + return cls.__instantiated_artifacts[ref] + + artifact_element = ArtifactElement(context, ref) + # XXX: We need to call update state as it is responsible for + # initialising an Element/ArtifactElement's Artifact (__artifact) + artifact_element._update_state() + cls.__instantiated_artifacts[ref] = artifact_element + + for dep_ref in artifact_element.get_dependency_refs(Scope.BUILD): + dependency = ArtifactElement._new_from_artifact_ref(dep_ref, context, task) + artifact_element._add_build_dependency(dependency) + + return artifact_element + + # _clear_artifact_refs_cache() + # + # Clear the internal artifact refs cache + # + # When loading ArtifactElements from artifact refs, we cache already + # instantiated ArtifactElements in order to not have to load the same + # ArtifactElements twice. This clears the cache. + # + # It should be called whenever we are done loading all artifacts in order + # to save memory. + # + @classmethod + def _clear_artifact_refs_cache(cls): + cls.__instantiated_artifacts = {} + # Override Element.get_artifact_name() def get_artifact_name(self, key=None): return self._ref |