File: snap_image.py

package info (click to toggle)
bst-external 0.29.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 664 kB
  • sloc: python: 3,936; makefile: 53
file content (102 lines) | stat: -rw-r--r-- 3,676 bytes parent folder | download | duplicates (2)
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
#
#  Copyright (C) 2018, 2019 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:
#         Valentin David <valentin.david@codethink.co.uk>


import collections.abc
import os
from ruamel import yaml
from buildstream import Element, ElementError, Scope

class SnapImageElement(Element):

    def configure(self, node):
        self.node_validate(node, [
            'directory', 'include', 'exclude', 'metadata',
            'include-orphans'
        ])
        self.directory = self.node_subst_member(node, 'directory')
        self.include = self.node_get_member(node, list, 'include')
        self.exclude = self.node_get_member(node, list, 'exclude')
        self.include_orphans = self.node_get_member(node, bool, 'include-orphans')
        self.metadata = self._clean_meta_data(node.get('metadata'))

    def _clean_meta_data(self, node):
        ret = {}
        for k, v in node.items():
            if not k.startswith('__bst'):
                if isinstance(v, collections.abc.Mapping):
                    ret[k] = self._clean_meta_data(v)
                elif isinstance(v, list):
                    ret[k] = self.node_subst_list(node, k)
                else:
                    ret[k] = self.node_subst_member(node, k)
        return ret

    def preflight(self):
        runtime_deps = list(self.dependencies(Scope.RUN, recurse=False))
        if runtime_deps:
            raise ElementError("{}: Only build type dependencies supported by flatpak_image elements"
                               .format(self))

        sources = list(self.sources())
        if sources:
            raise ElementError("{}: flatpak_image elements may not have sources".format(self))

    def get_unique_key(self):
        key = {}
        key['directory'] = self.directory
        key['include'] = sorted(self.include)
        key['exclude'] = sorted(self.exclude)
        key['include-orphans'] = self.include_orphans
        key['metadata'] = self.metadata
        key['version'] = 7
        return key

    def configure_sandbox(self, sandbox):
        pass

    def stage(self, sandbox):
        pass

    def assemble(self, sandbox):
        basedir = sandbox.get_directory()

        reldirectory = os.path.relpath(self.directory, os.sep)
        rootdir = os.path.join(basedir, reldirectory)

        metadir = os.path.join(rootdir, 'meta')
        metadata = os.path.join(metadir, 'snap.yaml')

        with self.timed_activity("Creating snap image", silent_nested=True):
            self.stage_dependency_artifacts(sandbox,
                                            Scope.BUILD,
                                            include=self.include,
                                            exclude=self.exclude,
                                            orphans=self.include_orphans)

            os.makedirs(metadir, exist_ok=True)

            with open(metadata, 'w') as file:
                yaml.dump(self.metadata, file)


        return os.path.join(os.sep, reldirectory)

def setup():
    return SnapImageElement