summaryrefslogtreecommitdiff
path: root/src/buildstream/_loader/loadcontext.py
blob: 65b7e9157f7541d28c3333f9a52ddc9fd07c0d36 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
#  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 .._exceptions import LoadError
from ..exceptions import LoadErrorReason


# LoaderContext()
#
# An object to keep track of overall context during the load process.
#
# Args:
#    context (Context): The invocation context
#
class LoadContext:
    def __init__(self, context):

        # Keep track of global context required throughout the recursive load
        self.context = context
        self.rewritable = False
        self.fetch_subprojects = None
        self.task = None

        # A table of all Loaders, indexed by project name
        self._loaders = {}

    # set_rewritable()
    #
    # Sets whether the projects are to be loaded in a rewritable fashion,
    # this is used for tracking and is slightly more expensive in load time.
    #
    # Args:
    #   task (Task): The task to report progress on
    #
    def set_rewritable(self, rewritable):
        self.rewritable = rewritable

    # set_task()
    #
    # Sets the task for progress reporting.
    #
    # Args:
    #   task (Task): The task to report progress on
    #
    def set_task(self, task):
        self.task = task

    # set_fetch_subprojects()
    #
    # Sets the task for progress reporting.
    #
    # Args:
    #   task (callable): The callable for loading subprojects
    #
    def set_fetch_subprojects(self, fetch_subprojects):
        self.fetch_subprojects = fetch_subprojects

    # register_loader()
    #
    # Registers a new loader in the load context, possibly
    # raising an error in the case of a conflict
    #
    # Args:
    #    loader (Loader): The Loader object to register into context
    #
    # Raises:
    #    (LoadError): A CONFLICTING_JUNCTION LoadError in the case of a conflict
    #
    def register_loader(self, loader):
        project = loader.project
        existing_loader = self._loaders.get(project.name, None)

        if existing_loader:

            assert project.junction is not None

            if existing_loader.project.junction:
                # The existing provenance can be None even if there is a junction, this
                # can happen when specifying a full element path on the command line.
                #
                provenance_str = ""
                if existing_loader.provenance:
                    provenance_str = ": {}".format(existing_loader.provenance)

                detail = "Project '{}' was already loaded by junction '{}'{}".format(
                    project.name, existing_loader.project.junction._get_full_name(), provenance_str
                )
            else:
                detail = "Project '{}' is also the toplevel project".format(project.name)

            raise LoadError(
                "{}: Error loading project '{}' from junction: {}".format(
                    loader.provenance, project.name, project.junction._get_full_name()
                ),
                LoadErrorReason.CONFLICTING_JUNCTION,
                detail=detail,
            )
        self._loaders[project.name] = loader