aboutsummaryrefslogtreecommitdiff
blob: da925e1dad13bff37015c52534167150c3ade686 (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
#!/usr/bin/env python
# Copyright 2005-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

"""System health checks and maintenance utilities.
"""

import os
import signal

# For compatibility with Python < 3.8
raise_signal = getattr(
    signal, "raise_signal", lambda signum: os.kill(os.getpid(), signum)
)


# Inherit from KeyboardInterrupt to avoid a traceback from asyncio.
class SignalInterrupt(KeyboardInterrupt):
    def __init__(self, signum):
        self.signum = signum


try:

    def signal_interrupt(signum, _frame):
        raise SignalInterrupt(signum)

    def debug_signal(_signum, _frame):
        import pdb

        pdb.set_trace()

    # Prevent "[Errno 32] Broken pipe" exceptions when writing to a pipe.
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    signal.signal(signal.SIGTERM, signal_interrupt)
    signal.signal(signal.SIGUSR1, debug_signal)

    import sys
    import errno
    from os import path as osp

    if osp.isfile(
        osp.join(
            osp.dirname(osp.dirname(osp.realpath(__file__))), ".portage_not_installed"
        )
    ):
        sys.path.insert(
            0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "lib")
        )
    import portage

    portage._internal_caller = True
    from portage.emaint.main import emaint_main
    from portage.util._eventloop.global_event_loop import global_event_loop

    if __name__ == "__main__":
        try:
            emaint_main(sys.argv[1:])
        except OSError as e:
            if e.errno == errno.EACCES:
                print("\nemaint: Need superuser access")
                sys.exit(1)
            else:
                raise
        finally:
            # Only close the event loop for __main__,
            # since outside of __main__ it would close the
            # event loop for child processes when using
            # the multiprocessing spawn start method.
            global_event_loop().close()

except KeyboardInterrupt as e:
    # Prevent traceback on ^C
    signum = getattr(e, "signum", signal.SIGINT)
    signal.signal(signum, signal.SIG_DFL)
    raise_signal(signum)