aboutsummaryrefslogtreecommitdiff
blob: 295a1e3533c3472b474e06c8ed7e5465f9206957 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright 2007-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

__all__ = [
    "SETPREFIX",
    "get_boolean",
    "SetConfigError",
    "SetConfig",
    "load_default_config",
]

import portage
from portage import os
from portage import load_mod
from portage import _unicode_decode
from portage import _unicode_encode
from portage import _encodings
from portage.const import USER_CONFIG_PATH, GLOBAL_CONFIG_PATH
from portage.const import VCS_DIRS
from portage.const import _ENABLE_SET_CONFIG
from portage.exception import PackageSetNotFound
from portage.localization import _
from portage.util import writemsg_level
from portage.util.configparser import (
    SafeConfigParser,
    NoOptionError,
    ParsingError,
    read_configs,
)

SETPREFIX = "@"


def get_boolean(options, name, default):
    if not name in options:
        return default
    if options[name].lower() in ("1", "yes", "on", "true"):
        return True
    if options[name].lower() in ("0", "no", "off", "false"):
        return False
    raise SetConfigError(
        _("invalid value '%(value)s' for option '%(option)s'")
        % {"value": options[name], "option": name}
    )


class SetConfigError(Exception):
    pass


class SetConfig:
    def __init__(self, paths, settings, trees):
        self._parser = SafeConfigParser(
            defaults={
                "EPREFIX": settings["EPREFIX"],
                "EROOT": settings["EROOT"],
                "PORTAGE_CONFIGROOT": settings["PORTAGE_CONFIGROOT"],
                "ROOT": settings["ROOT"],
            }
        )

        if _ENABLE_SET_CONFIG:
            read_configs(self._parser, paths)
        else:
            self._create_default_config()

        self.errors = []
        self.psets = {}
        self.trees = trees
        self.settings = settings
        self._parsed = False
        self.active = []

    def _create_default_config(self):
        """
        Create a default hardcoded set configuration for a portage version
        that does not support set configuration files. This is only used
        in the current branch of portage if _ENABLE_SET_CONFIG is False.
        Even if it's not used in this branch, keep it here in order to
        minimize the diff between branches.

                [world]
                class = portage.sets.base.DummyPackageSet
                packages = @selected @system

                [selected]
                class = portage.sets.files.WorldSelectedSet

                [system]
                class = portage.sets.profiles.PackagesSystemSet

        """
        parser = self._parser

        parser.remove_section("world")
        parser.add_section("world")
        parser.set("world", "class", "portage.sets.base.DummyPackageSet")
        parser.set("world", "packages", "@profile @selected @system")

        parser.remove_section("profile")
        parser.add_section("profile")
        parser.set(
            "profile", "class", "portage.sets.ProfilePackageSet.ProfilePackageSet"
        )

        parser.remove_section("selected")
        parser.add_section("selected")
        parser.set("selected", "class", "portage.sets.files.WorldSelectedSet")

        parser.remove_section("selected-packages")
        parser.add_section("selected-packages")
        parser.set(
            "selected-packages", "class", "portage.sets.files.WorldSelectedPackagesSet"
        )

        parser.remove_section("selected-sets")
        parser.add_section("selected-sets")
        parser.set("selected-sets", "class", "portage.sets.files.WorldSelectedSetsSet")

        parser.remove_section("system")
        parser.add_section("system")
        parser.set("system", "class", "portage.sets.profiles.PackagesSystemSet")

        parser.remove_section("security")
        parser.add_section("security")
        parser.set("security", "class", "portage.sets.security.NewAffectedSet")

        parser.remove_section("usersets")
        parser.add_section("usersets")
        parser.set("usersets", "class", "portage.sets.files.StaticFileSet")
        parser.set("usersets", "multiset", "true")
        parser.set("usersets", "directory", "%(PORTAGE_CONFIGROOT)setc/portage/sets")
        parser.set("usersets", "world-candidate", "true")

        parser.remove_section("live-rebuild")
        parser.add_section("live-rebuild")
        parser.set("live-rebuild", "class", "portage.sets.dbapi.VariableSet")
        parser.set("live-rebuild", "variable", "PROPERTIES")
        parser.set("live-rebuild", "includes", "live")

        parser.remove_section("deprecated-live-rebuild")
        parser.add_section("deprecated-live-rebuild")
        parser.set("deprecated-live-rebuild", "class", "portage.sets.dbapi.VariableSet")
        parser.set("deprecated-live-rebuild", "variable", "INHERITED")
        parser.set(
            "deprecated-live-rebuild",
            "includes",
            " ".join(sorted(portage.const.LIVE_ECLASSES)),
        )

        parser.remove_section("module-rebuild")
        parser.add_section("module-rebuild")
        parser.set("module-rebuild", "class", "portage.sets.dbapi.OwnerSet")
        parser.set("module-rebuild", "files", "/lib/modules")

        parser.remove_section("preserved-rebuild")
        parser.add_section("preserved-rebuild")
        parser.set(
            "preserved-rebuild",
            "class",
            "portage.sets.libs.PreservedLibraryConsumerSet",
        )

        parser.remove_section("x11-module-rebuild")
        parser.add_section("x11-module-rebuild")
        parser.set("x11-module-rebuild", "class", "portage.sets.dbapi.OwnerSet")
        parser.set("x11-module-rebuild", "files", "/usr/lib*/xorg/modules")
        parser.set("x11-module-rebuild", "exclude-files", "/usr/bin/Xorg")

    def update(self, setname, options):
        parser = self._parser
        self.errors = []
        if not setname in self.psets:
            options["name"] = setname
            options["world-candidate"] = "False"

            # for the unlikely case that there is already a section with the requested setname
            import random

            while setname in parser.sections():
                setname = "%08d" % random.randint(0, 10**10)

            parser.add_section(setname)
            for k, v in options.items():
                parser.set(setname, k, v)
        else:
            section = self.psets[setname].creator
            if parser.has_option(section, "multiset") and parser.getboolean(
                section, "multiset"
            ):
                self.errors.append(
                    _(
                        "Invalid request to reconfigure set '%(set)s' generated "
                        "by multiset section '%(section)s'"
                    )
                    % {"set": setname, "section": section}
                )
                return
            for k, v in options.items():
                parser.set(section, k, v)
        self._parse(update=True)

    def _parse(self, update=False):
        if self._parsed and not update:
            return
        parser = self._parser
        for sname in parser.sections():
            # find classname for current section, default to file based sets
            if not parser.has_option(sname, "class"):
                classname = "portage._sets.files.StaticFileSet"
            else:
                classname = parser.get(sname, "class")

            if classname.startswith("portage.sets."):
                # The module has been made private, but we still support
                # the previous namespace for sets.conf entries.
                classname = classname.replace("sets", "_sets", 1)

            # try to import the specified class
            try:
                setclass = load_mod(classname)
            except (ImportError, AttributeError):
                try:
                    setclass = load_mod("portage._sets." + classname)
                except (ImportError, AttributeError):
                    self.errors.append(
                        _("Could not import '%(class)s' for section " "'%(section)s'")
                        % {"class": classname, "section": sname}
                    )
                    continue
            # prepare option dict for the current section
            optdict = {}
            for oname in parser.options(sname):
                optdict[oname] = parser.get(sname, oname)

            # create single or multiple instances of the given class depending on configuration
            if parser.has_option(sname, "multiset") and parser.getboolean(
                sname, "multiset"
            ):
                if hasattr(setclass, "multiBuilder"):
                    newsets = {}
                    try:
                        newsets = setclass.multiBuilder(
                            optdict, self.settings, self.trees
                        )
                    except SetConfigError as e:
                        self.errors.append(
                            _("Configuration error in section '%s': %s")
                            % (sname, str(e))
                        )
                        continue
                    for x in newsets:
                        if x in self.psets and not update:
                            self.errors.append(
                                _("Redefinition of set '%s' (sections: '%s', '%s')")
                                % (x, self.psets[x].creator, sname)
                            )
                        newsets[x].creator = sname
                        if parser.has_option(
                            sname, "world-candidate"
                        ) and parser.getboolean(sname, "world-candidate"):
                            newsets[x].world_candidate = True
                    self.psets.update(newsets)
                else:
                    self.errors.append(
                        _(
                            "Section '%(section)s' is configured as multiset, but '%(class)s' "
                            "doesn't support that configuration"
                        )
                        % {"section": sname, "class": classname}
                    )
                    continue
            else:
                try:
                    setname = parser.get(sname, "name")
                except NoOptionError:
                    setname = sname
                if setname in self.psets and not update:
                    self.errors.append(
                        _("Redefinition of set '%s' (sections: '%s', '%s')")
                        % (setname, self.psets[setname].creator, sname)
                    )
                if hasattr(setclass, "singleBuilder"):
                    try:
                        self.psets[setname] = setclass.singleBuilder(
                            optdict, self.settings, self.trees
                        )
                        self.psets[setname].creator = sname
                        if parser.has_option(
                            sname, "world-candidate"
                        ) and parser.getboolean(sname, "world-candidate"):
                            self.psets[setname].world_candidate = True
                    except SetConfigError as e:
                        self.errors.append(
                            _("Configuration error in section '%s': %s")
                            % (sname, str(e))
                        )
                        continue
                else:
                    self.errors.append(
                        _(
                            "'%(class)s' does not support individual set creation, section '%(section)s' "
                            "must be configured as multiset"
                        )
                        % {"class": classname, "section": sname}
                    )
                    continue
        self._parsed = True

    def getSets(self):
        self._parse()
        return self.psets.copy()

    def getSetAtoms(self, setname, ignorelist=None):
        """
        This raises PackageSetNotFound if the give setname does not exist.
        """
        self._parse()
        try:
            myset = self.psets[setname]
        except KeyError:
            raise PackageSetNotFound(setname)
        myatoms = myset.getAtoms()

        if ignorelist is None:
            ignorelist = set()

        ignorelist.add(setname)
        for n in myset.getNonAtoms():
            if n.startswith(SETPREFIX):
                s = n[len(SETPREFIX) :]
                if s in self.psets:
                    if s not in ignorelist:
                        myatoms.update(self.getSetAtoms(s, ignorelist=ignorelist))
                else:
                    raise PackageSetNotFound(s)

        return myatoms


def load_default_config(settings, trees):
    if not _ENABLE_SET_CONFIG:
        return SetConfig(None, settings, trees)

    global_config_path = GLOBAL_CONFIG_PATH
    if portage.const.EPREFIX:
        global_config_path = os.path.join(
            portage.const.EPREFIX, GLOBAL_CONFIG_PATH.lstrip(os.sep)
        )
    vcs_dirs = [_unicode_encode(x, encoding=_encodings["fs"]) for x in VCS_DIRS]

    def _getfiles():
        sets_config_paths = [
            os.path.join(global_config_path, "sets"),
            *(
                os.path.join(repo.location, "sets.conf")
                for repo in trees["porttree"].dbapi.repositories
            ),
            os.path.join(settings["PORTAGE_CONFIGROOT"], USER_CONFIG_PATH, "sets.conf"),
        ]

        dot = "."
        tilde = "~"
        if not portage.utf8_mode:
            dot = _unicode_encode(dot)
            tilde = _unicode_encode(tilde)

        for sets_config_path in sets_config_paths:
            if os.path.isdir(sets_config_path):
                for path, dirs, files in os.walk(sets_config_path):
                    dirs.sort()
                    files.sort()
                    for d in dirs:
                        if d in vcs_dirs or d.startswith(dot) or d.endswith(tilde):
                            dirs.remove(d)
                    for f in files:
                        if not f.startswith(dot) and not f.endswith(tilde):
                            yield os.path.join(path, f)
            elif os.path.isfile(sets_config_path):
                yield sets_config_path

    return SetConfig(_getfiles(), settings, trees)