summaryrefslogtreecommitdiff
path: root/src/buildstream/_elementsources.py
blob: c1ffc091917356e0a0fee525e31a7dcccf927493 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#
#  Copyright (C) 2016-2018 Codethink Limited
#  Copyright (C) 2017-2020 Bloomberg Finance LP
#
#  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/>.

from typing import TYPE_CHECKING, Iterator

from ._context import Context

from .storage._casbaseddirectory import CasBasedDirectory

if TYPE_CHECKING:
    from typing import List

    from .source import Source

# An ElementSources object represents the combined sources of an element.
class ElementSources:
    def __init__(self, context: Context):

        self._context = context
        self._sources = []  # type: List[Source]
        self.vdir = None  # Directory with staged sources
        self._sourcecache = context.sourcecache  # Source cache
        self._is_resolved = False  # Whether the source is fully resolved or not
        self._cached = None  # If the sources are known to be successfully cached in CAS

        # the index of the last source in this element that requires previous
        # sources for staging
        self._last_source_requires_previous_idx = None

    # add_source():
    #
    # Append source to this list of element sources.
    #
    # Args:
    #   source (Source): The source to add
    #
    def add_source(self, source):
        self._sources.append(source)

    # sources():
    #
    # A generator function to enumerate the element sources
    #
    # Yields:
    #   Source: The individual sources
    #
    def sources(self) -> Iterator["Source"]:
        for source in self._sources:
            yield source

    # track():
    #
    # Calls track() on the Element sources
    #
    # Raises:
    #    SourceError: If one of the element sources has an error
    #
    # Returns:
    #    (list): A list of Source object ids and their new references
    #
    def track(self, workspace):
        refs = []
        for index, source in enumerate(self._sources):
            old_ref = source.get_ref()
            new_ref = source._track(self._sources[0:index])
            refs.append((source._unique_id, new_ref))

            # Complimentary warning that the new ref will be unused.
            if old_ref != new_ref and workspace:
                detail = (
                    "This source has an open workspace.\n"
                    + "To start using the new reference, please close the existing workspace."
                )
                source.warn("Updated reference will be ignored as source has open workspace", detail=detail)

        return refs

    # stage():
    #
    # Stage the element sources to a directory
    #
    # Returns:
    #     (:class:`.storage.Directory`): A virtual directory object to stage sources into.
    #
    def stage(self):
        # Assert sources are cached
        assert self.cached()

        self.vdir = CasBasedDirectory(self._context.get_cascache())

        if self._sources:
            # find last required source
            last_required_previous_idx = self._last_source_requires_previous()

            for source in self._sources[last_required_previous_idx:]:
                source_dir = self._sourcecache.export(source)
                self.vdir.import_files(source_dir)

        return self.vdir

    # fetch_done()
    #
    # Indicates that fetching the sources for this element has been done.
    #
    # Args:
    #   fetched_original (bool): Whether the original sources had been asked (and fetched) or not
    #
    def fetch_done(self, fetched_original):
        self._cached = True

        for source in self._sources:
            source._fetch_done(fetched_original)

    # push()
    #
    # Push the element's sources.
    #
    # Returns:
    #   (bool): True if the remote was updated, False if it already existed
    #           and no updated was required
    #
    def push(self):
        pushed = False

        for source in self.sources():
            if self._sourcecache.push(source):
                pushed = True

        return pushed

    # init_workspace():
    #
    # Initialises a new workspace from the element sources.
    #
    # Args:
    #   directory (str): Path of the workspace to init
    #
    def init_workspace(self, directory: str):
        for source in self.sources():
            source._init_workspace(directory)

    # fetch():
    #
    # Fetch the element sources.
    #
    # Raises:
    #    SourceError: If one of the element sources has an error
    #
    def fetch(self, fetch_original=False):
        previous_sources = []
        fetch_needed = False

        if self._sources and not fetch_original:
            for source in self._sources:
                if self._sourcecache.contains(source):
                    continue

                # try and fetch from source cache
                if not source._is_cached() and self._sourcecache.has_fetch_remotes():
                    if self._sourcecache.pull(source):
                        continue

                fetch_needed = True

        # We need to fetch original sources
        if fetch_needed or fetch_original:
            for source in self.sources():
                if not source._is_cached():
                    source._fetch(previous_sources)
                previous_sources.append(source)

            self._cache_sources()

    # get_unique_key():
    #
    # Return something which uniquely identifies the combined sources of the
    # element.
    #
    # Returns:
    #    (str, list, dict): A string, list or dictionary as unique identifier
    #
    def get_unique_key(self):
        result = []

        for source in self._sources:
            result.append({"key": source._get_unique_key(), "name": source.get_kind()})

        return result

    # cached():
    #
    # Check if the element sources are cached in CAS, generating the source
    # cache keys if needed.
    #
    # Returns:
    #    (bool): True if the element sources are cached
    #
    def cached(self):
        if self._cached is not None:
            return self._cached

        sourcecache = self._sourcecache

        # Go through sources we'll cache generating keys
        for ix, source in enumerate(self._sources):
            if not source._key:
                if source.BST_REQUIRES_PREVIOUS_SOURCES_STAGE:
                    source._generate_key(self._sources[:ix])
                else:
                    source._generate_key([])

        # Check all sources are in source cache
        for source in self._sources:
            if not sourcecache.contains(source):
                return False

        self._cached = True
        return True

    # is_resolved():
    #
    # Get whether all sources of the element are resolved
    #
    # Returns:
    #    (bool): True if all element sources are resolved
    #
    def is_resolved(self):
        return self._is_resolved

    # cached_original():
    #
    # Get whether all the sources of the element have their own cached
    # copy of their sources.
    #
    # Returns:
    #    (bool): True if all element sources have the original sources cached
    #
    def cached_original(self):
        return all(source._is_cached() for source in self._sources)

    # update_resolved_state():
    #
    # Updates source's resolved state
    #
    # An element's source state must be resolved before it may compute
    # cache keys, because the source's ref, whether defined in yaml or
    # from the workspace, is a component of the element's cache keys.
    #
    def update_resolved_state(self):
        for source in self._sources:
            if not source.is_resolved():
                break
        else:
            self._is_resolved = True

    # preflight():
    #
    # A internal wrapper for calling the abstract preflight() method on
    # the element and its sources.
    #
    def preflight(self):
        # Ensure that the first source does not need access to previous sources
        if self._sources and self._sources[0]._requires_previous_sources():
            from .element import ElementError  # pylint: disable=cyclic-import

            raise ElementError(
                "{}: {} cannot be the first source of an element "
                "as it requires access to previous sources".format(self, self._sources[0])
            )

        # Preflight the sources
        for source in self.sources():
            source._preflight()

    # _cache_sources():
    #
    # Caches the sources into the local CAS
    #
    def _cache_sources(self):
        if self._sources and not self.cached():
            last_requires_previous = 0
            # commit all other sources by themselves
            for idx, source in enumerate(self._sources):
                if source.BST_REQUIRES_PREVIOUS_SOURCES_STAGE:
                    self._sourcecache.commit(source, self._sources[last_requires_previous:idx])
                    last_requires_previous = idx
                else:
                    self._sourcecache.commit(source, [])

    # _last_source_requires_previous
    #
    # This is the last source that requires previous sources to be cached.
    # Sources listed after this will be cached separately.
    #
    # Returns:
    #    (int): index of last source that requires previous sources
    #
    def _last_source_requires_previous(self):
        if self._last_source_requires_previous_idx is None:
            last_requires_previous = 0
            for idx, source in enumerate(self._sources):
                if source.BST_REQUIRES_PREVIOUS_SOURCES_STAGE:
                    last_requires_previous = idx
            self._last_source_requires_previous_idx = last_requires_previous
        return self._last_source_requires_previous_idx