aboutsummaryrefslogtreecommitdiff
blob: 950878ce5061bf4c7302dc23d4c8c1a818f651c1 (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
# Copyright 2010-2020 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import locale
import logging
import time

from portage import os, _unicode_decode
from portage.exception import PortageException
from portage.localization import _
from portage.output import EOutput
from portage.util import grabfile, writemsg_level


def have_english_locale():
    lang, enc = locale.getdefaultlocale()
    if lang is not None:
        lang = lang.lower()
        lang = lang.split("_", 1)[0]
    return lang is None or lang in ("c", "en")


def whenago(seconds):
    sec = int(seconds)
    mins = 0
    days = 0
    hrs = 0
    years = 0
    out = []

    if sec > 60:
        mins = sec // 60
        sec = sec % 60
    if mins > 60:
        hrs = mins // 60
        mins = mins % 60
    if hrs > 24:
        days = hrs // 24
        hrs = hrs % 24
    if days > 365:
        years = days // 365
        days = days % 365

    if years:
        out.append("%dy " % years)
    if days:
        out.append("%dd " % days)
    if hrs:
        out.append("%dh " % hrs)
    if mins:
        out.append("%dm " % mins)
    if sec:
        out.append("%ds " % sec)

    return "".join(out).strip()


def old_tree_timestamp_warn(portdir, settings):
    unixtime = time.time()
    default_warnsync = 30

    timestamp_file = os.path.join(portdir, "metadata/timestamp.x")
    try:
        lastsync = grabfile(timestamp_file)
    except PortageException:
        return False

    if not lastsync:
        return False

    lastsync = lastsync[0].split()
    if not lastsync:
        return False

    try:
        lastsync = int(lastsync[0])
    except ValueError:
        return False

    var_name = "PORTAGE_SYNC_STALE"
    try:
        warnsync = float(settings.get(var_name, default_warnsync))
    except ValueError:
        writemsg_level(
            f"!!! {var_name} contains non-numeric value: {settings[var_name]}\n",
            level=logging.ERROR,
            noiselevel=-1,
        )
        return False

    if warnsync <= 0:
        return False

    if (unixtime - 86400 * warnsync) > lastsync:
        out = EOutput()
        if have_english_locale():
            out.ewarn(f"Last emerge --sync was {whenago(unixtime - lastsync)} ago.")
        else:
            out.ewarn(
                _("Last emerge --sync was %s.")
                % _unicode_decode(time.strftime("%c", time.localtime(lastsync)))
            )
        return True
    return False