summaryrefslogtreecommitdiff
path: root/buildstream/source.py
blob: 7353aea692bfe6e111f1deb327bd785086e10150 (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
#!/usr/bin/env python3
#
#  Copyright (C) 2016 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>

import os

from . import _yaml
from . import ImplError
from . import Plugin


class Source(Plugin):
    """Source()

    Base Source class.

    All Sources derive from this class, this interface defines how
    the core will be interacting with Sources.
    """
    def __init__(self, display_name, context, project, meta):
        provenance = _yaml.node_get_provenance(meta.config)
        super().__init__(display_name, context, project, provenance, "source")

        self.__directory = meta.directory               # Staging relative directory
        self.__origin_node = meta.origin_node           # YAML node this Source was loaded from
        self.__origin_toplevel = meta.origin_toplevel   # Toplevel YAML node for the file
        self.__origin_filename = meta.origin_filename   # Filename of the file the source was loaded from
        self.__consistent = None

        self.configure(meta.config)

    def get_mirror_directory(self):
        """Fetches the directory where this source should store things

        Returns:
           (str): The directory belonging to this source
        """

        # Create the directory if it doesnt exist
        context = self.get_context()
        directory = os.path.join(context.sourcedir, self.get_kind())
        os.makedirs(directory, exist_ok=True)
        return directory

    def consistent(self):
        """Report whether the source has a resolved reference

        Returns:
           (bool): True if the source has a reference

        Before building, every source must have an exact reference,
        although it is not an error to load a project which contains
        sources that do not have references, they can be fetched
        later with :func:`~buildstream.source.Source.refresh`
        """
        raise ImplError("Source plugin '%s' does not implement consistent()" % self.get_kind())

    def refresh(self, node):
        """Refresh specific source references

        Args:
           node (dict): The same dictionary which was previously passed
                        to :func:`~buildstream.source.Source.configure`

        Returns:
           (bool): True if the refresh resulted in any update or change

        Raises:
           :class:`.SourceError`

        Sources which implement some revision control system should
        implement this by updating the commit reference from a symbolic
        tracking branch or tag. The commit reference should be updated
        internally on the given Source object and also in the passed *node*
        parameter so that a user's project may optionally be updated
        with the new reference.

        Sources which implement a tarball or file should implement this
        by updating an sha256 sum.

        Implementors should raise :class:`.SourceError` if some error is
        encountered while attempting to refresh.
        """
        raise ImplError("Source plugin '%s' does not implement refresh()" % self.get_kind())

    def fetch(self):
        """Fetch remote sources and mirror them locally, ensuring at least
        that the specific reference is cached locally.

        Raises:
           :class:`.SourceError`

        Implementors should raise :class:`.SourceError` if the there is some
        network error or if the source reference could not be matched.
        """
        raise ImplError("Source plugin '%s' does not implement fetch()" % self.get_kind())

    def stage(self, directory):
        """Stage the sources to a directory

        Args:
           directory (str): Path to stage the source

        Raises:
           :class:`.SourceError`

        Implementors should assume that *directory* already exists
        and stage already cached sources to the passed directory.

        Implementors should raise :class:`.SourceError` when encountering
        some system error.
        """
        raise ImplError("Source plugin '%s' does not implement stage()" % self.get_kind())

    #############################################################
    #            Private Methods used in BuildStream            #
    #############################################################

    # Wrapper for consistent() api which caches the result, we
    # know we're consistent after a successful refresh
    #
    def _consistent(self):

        if self.__consistent is None:
            self.__consistent = self.consistent()

        return self.__consistent

    # Wrapper for refresh()
    #
    def _refresh(self, node):

        changed = self.refresh(node)

        # It's consistent unless it reported an error
        self.__consistent = True

        return changed