aboutsummaryrefslogtreecommitdiff
blob: 31c0df5a82c82e63af15400013d5f6d087df3a41 (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
# Copyright(c) 2010, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2 or higher

"""Provides access to Portage sets api"""

__docformat__ = "epytext"

import portage

try:
    # Per commit 25d8427b3b29cbcee97279186983dae818495f8f in portage,
    # portage.sets is renamed to portage._sets.
    import portage._sets

    _sets_available = True
    SETPREFIX = portage._sets.SETPREFIX
except ImportError:
    _sets_available = False
    SETPREFIX = "@"

from gentoolkit import errors
from gentoolkit.atom import Atom


_set_config = None


def _init_set_config():
    global _set_config
    if _set_config is None:
        _set_config = portage._sets.load_default_config(
            portage.settings, portage.db[portage.root]
        )


def get_available_sets():
    """Returns all available sets."""

    if _sets_available:
        _init_set_config()
        return _set_config.getSets()
    return {}


def get_set_atoms(setname):
    """Return atoms belonging to the given set

    @type setname: string
    @param setname: Name of the set
    @rtype list
    @return: List of atoms in the given set
    """

    if _sets_available:
        _init_set_config()
        try:
            return {Atom(str(x)) for x in _set_config.getSetAtoms(setname)}
        except portage._sets.PackageSetNotFound:
            raise errors.GentoolkitSetNotFound(setname)
    raise errors.GentoolkitSetNotFound(setname)


# vim: set ts=4 sw=4 tw=79: