aboutsummaryrefslogtreecommitdiff
blob: b102e7d6a5b02eb7bcff5a871082754ebbc21e85 (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
#!/usr/bin/env python

import configparser
import copy
import os
import portage
import re

from grs.Constants import CONST

class WorldConf():
    """ doc here
        more doc
    """

    @staticmethod
    def install():
        """ doc here
            more doc
        """
        config = configparser.RawConfigParser(delimiters=':', allow_no_value=True, comment_prefixes=None)
        config.read(CONST.WORLD_CONFIG)
        for s in config.sections():
            for (directory, value) in config[s].items():
                p_slot_atom = re.sub('[/:]', '_', s)
                dpath = os.path.join(CONST.PORTAGE_CONFIGDIR, directory)
                fpath = os.path.join(dpath, p_slot_atom)
                os.makedirs(dpath, mode=0o755, exist_ok=True)
                with open(fpath, 'w') as g:
                    g.write('%s\n' % value)


    @staticmethod
    def clean():
        """ doc here
            more doc
        """
        portdb = portage.db[portage.root]["porttree"].dbapi
        vardb  = portage.db[portage.root]["vartree"].dbapi

        uninstalled = portdb.cp_all()
        for p in vardb.cp_all():
            try:
                uninstalled.remove(p)
            except ValueError:
                print('%s installed on local system, but not in portage repo anymore.' % p)

        slot_atoms = []
        for p in uninstalled:
            cpv = portdb.cp_list(p)[0]
            slotvar = portdb.aux_get(cpv, ['SLOT'])[0]
            try:
                m = re.search('(.+?)\/(.+)', slotvar)
                slot = m.group(1)
            except AttributeError:
                slot = slotvar
            slot_atoms.append(re.sub('[/:]', '_', '%s:%s' % (p, slot)))

        for dirpath, dirnames, filenames in os.walk(CONST.PORTAGE_CONFIGDIR):
            # Only look at select files and directories.
            # TODO: This needs to be expanded.
            if not os.path.basename(dirpath) in ['env', 'package.env', \
                    'package.accept_keywords', 'package.use', \
                    'package.mask', 'package.unmask']:
                continue

            for f in filenames:
                fpath = os.path.realpath(os.path.join(dirpath, f))
                if f in slot_atoms:
                    os.remove(fpath)