summaryrefslogtreecommitdiff
path: root/buildstream/project.py
blob: b9c181b7c0de115b8f609bd12cc528a7ca901250 (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
#!/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>

"""Loaded Project Configuration

The :class:`.Project` object holds all of the project settings from
the project configuration file including the project directory it
was loaded from.

The project configuration file should be named ``project.yaml`` and
be located at the project root. It holds information such as Source
aliases relevant for the sources used in the given project as well as
overrides for the configuration of element types used in the project.

The default BuildStream project configuration is included here for reference:
  .. literalinclude:: ../../buildstream/data/projectconfig.yaml
"""

import os
from . import _site
from . import _yaml
from . import LoadError, LoadErrorReason
from .utils import node_items


# The separator we use for user specified aliases
_ALIAS_SEPARATOR = ':'


class Project():
    """Project Configuration

    Args:
       directory (string): The project directory

    Raises:
       :class:`.LoadError`
    """
    def __init__(self, directory):

        self.name = None
        """str: The project name"""

        self.directory = directory
        """str: The project directory"""

        self.environment = {}
        """dict: The base sandbox environment"""

        self.devices = []
        """list: List of device descriptions required for the sandbox"""

        self._elements = {}  # Element specific configurations
        self._aliases = {}   # Aliases dictionary

        self._load()

    def translate_url(self, url):
        """Translates the given url which may be specified with an alias
        into a fully qualified url.

        Args:
           url (str): A url, which may be using an alias

        Returns:
           str: The fully qualified url, with aliases resolved

        This method is provided for :class:`.Source` objects to resolve
        fully qualified urls based on the shorthand which is allowed
        to be specified in the YAML
        """
        if url and _ALIAS_SEPARATOR in url:
            url_alias, url_body = url.split(_ALIAS_SEPARATOR, 1)
            alias_url = self._aliases.get(url_alias)
            if alias_url:
                url = alias_url + url_body

        return url

    def _load(self):

        projectfile = os.path.join(self.directory, "project.yaml")

        config = _yaml.load(_site.default_project_config)
        project_conf = _yaml.load(projectfile)
        _yaml.composite(config, project_conf, typesafe=True)

        # The project name
        self.name = _yaml.node_get(config, str, 'name')

        # Load sandbox configuration
        sandbox_node = _yaml.node_get(config, dict, 'sandbox')
        self.environment = _yaml.node_get(sandbox_node, dict, 'environment')
        self.devices = _yaml.node_get(sandbox_node, list, 'devices')

        # Aliases & Element configurations
        self._elements = _yaml.node_get(config, dict, 'elements', default_value={})
        self._aliases = _yaml.node_get(config, dict, 'aliases', default_value={})