summaryrefslogtreecommitdiff
path: root/src/buildstream/_artifactproject.py
blob: b8153b06d02eb4b337aa2c00486c824a71357075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
#  Copyright (C) 2020 Codethink Limited
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#  Authors:
#        Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
#
from contextlib import suppress
from typing import TYPE_CHECKING

from ._project import Project
from ._context import Context
from ._loader import Loader

if TYPE_CHECKING:
    from typing import Dict


# ArtifactProject()
#
# A project instance to be used as the project for an ArtifactElement.
#
# This is basically a simplified Project implementation which ensures that
# we do not accidentally infer any data from a possibly present local project
# when processing an ArtifactElement.
#
# Args:
#    project_name: The name of this project
#
class ArtifactProject(Project):

    __loaded_artifact_projects = {}  # type: Dict[str, ArtifactProject]

    def __init__(self, project_name: str, context: Context):

        #
        # Chain up to the Project constructor, and allow it to initialize
        # without loading anything
        #
        super().__init__(None, context, search_for_project=False)

        # Fill in some necessities
        #
        self.name = project_name
        self.element_path = ""  # This needs to be set to avoid Loader crashes
        self.loader = Loader(self)

    # get_artifact_project():
    #
    # Gets a reference to an ArtifactProject for the given
    # project name, possibly instantiating one if needed.
    #
    # Args:
    #    project_name: The project name
    #    context: The Context
    #
    # Returns:
    #    An ArtifactProject with the given project_name
    #
    @classmethod
    def get_artifact_project(cls, project_name: str, context: Context) -> "ArtifactProject":
        with suppress(KeyError):
            return cls.__loaded_artifact_projects[project_name]

        project = cls(project_name, context)
        cls.__loaded_artifact_projects[project_name] = project
        return project

    # clear_project_cache():
    #
    # Clears the cache of loaded projects, this can be called directly
    # after completing a full load.
    #
    @classmethod
    def clear_project_cache(cls):
        cls.__loaded_artifact_projects = {}