1#
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
5from oe.manifest import Manifest
6import re
7
8class PkgManifest(Manifest):
9    """
10    Returns a dictionary object with mip and mlp packages.
11    """
12    def _split_multilib(self, pkg_list):
13        pkgs = dict()
14
15        for pkg in pkg_list.split():
16            pkg_type = self.PKG_TYPE_MUST_INSTALL
17
18            ml_variants = self.d.getVar('MULTILIB_VARIANTS').split()
19
20            for ml_variant in ml_variants:
21                if pkg.startswith(ml_variant + '-'):
22                    pkg_type = self.PKG_TYPE_MULTILIB
23
24            if not pkg_type in pkgs:
25                pkgs[pkg_type] = pkg
26            else:
27                pkgs[pkg_type] += " " + pkg
28
29        return pkgs
30
31    def create_initial(self):
32        pkgs = dict()
33
34        with open(self.initial_manifest, "w+") as manifest:
35            manifest.write(self.initial_manifest_file_header)
36
37            for var in self.var_maps[self.manifest_type]:
38                if var in self.vars_to_split:
39                    split_pkgs = self._split_multilib(self.d.getVar(var))
40                    if split_pkgs is not None:
41                        pkgs = dict(list(pkgs.items()) + list(split_pkgs.items()))
42                else:
43                    pkg_list = self.d.getVar(var)
44                    if pkg_list is not None:
45                        pkgs[self.var_maps[self.manifest_type][var]] = self.d.getVar(var)
46
47            for pkg_type in sorted(pkgs):
48                for pkg in sorted(pkgs[pkg_type].split()):
49                    manifest.write("%s,%s\n" % (pkg_type, pkg))
50
51    def create_final(self):
52        pass
53
54    def create_full(self, pm):
55        if not os.path.exists(self.initial_manifest):
56            self.create_initial()
57
58        initial_manifest = self.parse_initial_manifest()
59        pkgs_to_install = list()
60        for pkg_type in initial_manifest:
61            pkgs_to_install += initial_manifest[pkg_type]
62        if len(pkgs_to_install) == 0:
63            return
64
65        output = pm.dummy_install(pkgs_to_install).decode('utf-8')
66
67        with open(self.full_manifest, 'w+') as manifest:
68            pkg_re = re.compile('^Installing ([^ ]+) [^ ].*')
69            for line in set(output.split('\n')):
70                m = pkg_re.match(line)
71                if m:
72                    manifest.write(m.group(1) + '\n')
73
74        return
75