aboutsummaryrefslogtreecommitdiff
blob: fefbe6edbc51cbe9af5f26ad12f298da377b7a29 (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
# Copyright 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import portage
from portage import os
from portage.util import shlex_split, varexpand

# default clean command from make.globals
## PORTAGE_LOGDIR_CLEAN = 'find "${PORTAGE_LOGDIR}" -type f ! -name "summary.log*" -mtime +7 -delete'

ERROR_MESSAGES = {
    78: "PORTAGE_LOGDIR variable not set or PORTAGE_LOGDIR not a directory.",
    127: "PORTAGE_LOGDIR_CLEAN command not found.",
}


class CleanLogs:
    short_desc = "Clean PORTAGE_LOGDIR logs"

    @staticmethod
    def name():
        return "logs"

    def can_progressbar(self, func):
        return False

    def check(self, **kwargs):
        options = kwargs.get("options", None)
        if options:
            options["pretend"] = True
        return self.clean(**kwargs)

    def clean(self, **kwargs):
        """Log directory cleaning function

        @param **kwargs: optional dictionary of values used in this function are:
                settings: portage settings instance: defaults to portage.settings
                        "PORTAGE_LOGDIR": directory to clean
                        "PORTAGE_LOGDIR_CLEAN": command for cleaning the logs.
                options: dict:
                        'NUM': int: number of days
                        'pretend': boolean
        """
        num_of_days = None
        pretend = False

        # convoluted, I know, but portage.settings does not exist in
        # kwargs.get() when called from _emerge.main.clean_logs()
        settings = kwargs.get("settings", getattr(portage, "settings", {}))

        options = kwargs.get("options", None)
        if options:
            num_of_days = options.get("NUM", None)
            pretend = options.get("pretend", False)

        clean_cmd = settings.get("PORTAGE_LOGDIR_CLEAN")
        if clean_cmd:
            clean_cmd = shlex_split(clean_cmd)
            if "-mtime" in clean_cmd and num_of_days is not None:
                if num_of_days == 0:
                    i = clean_cmd.index("-mtime")
                    clean_cmd.remove("-mtime")
                    clean_cmd.pop(i)
                else:
                    clean_cmd[clean_cmd.index("-mtime") + 1] = f"+{str(num_of_days)}"
            if pretend:
                if "-delete" in clean_cmd:
                    clean_cmd.remove("-delete")

        if not clean_cmd:
            return (True, None)
        rval = self._clean_logs(clean_cmd, settings)
        errors = self._convert_errors(rval)
        if errors:
            return (False, errors)
        return (True, None)

    @staticmethod
    def _clean_logs(clean_cmd, settings):
        logdir = settings.get("PORTAGE_LOGDIR")
        if logdir is None or not os.path.isdir(logdir):
            return 78

        variables = {"PORTAGE_LOGDIR": logdir}
        cmd = [varexpand(x, mydict=variables) for x in clean_cmd]

        try:
            rval = portage.process.spawn(cmd, env=os.environ)
        except portage.exception.CommandNotFound:
            rval = 127
        return rval

    @staticmethod
    def _convert_errors(rval):
        msg = []
        if rval != os.EX_OK:
            if rval in ERROR_MESSAGES:
                msg.append(ERROR_MESSAGES[rval])
            else:
                msg.append(f"PORTAGE_LOGDIR_CLEAN command returned {rval}")
            msg.append(
                "See the make.conf(5) man page for "
                "PORTAGE_LOGDIR_CLEAN usage instructions."
            )
        return msg