emerge: handle KeyboardInterrupt, not SIGINT
[portage.git] / bin / emerge
1 #!/usr/bin/python
2 # Copyright 2006-2012 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 from __future__ import print_function
6
7 import platform
8 import signal
9 import sys
10
11 # This block ensures that ^C interrupts are handled quietly. We handle
12 # KeyboardInterrupt instead of installing a SIGINT handler, since
13 # exiting from signal handlers intermittently causes python to ignore
14 # the SystemExit exception with a message like this:
15 # Exception SystemExit: 130 in <function remove at 0x7fd2146c1320> ignored
16 try:
17
18         def exithandler(signum,frame):
19                 signal.signal(signal.SIGTERM, signal.SIG_IGN)
20                 sys.exit(128 + signum)
21
22         signal.signal(signal.SIGTERM, exithandler)
23         # Prevent "[Errno 32] Broken pipe" exceptions when
24         # writing to a pipe.
25         signal.signal(signal.SIGPIPE, signal.SIG_DFL)
26
27         def debug_signal(signum, frame):
28                 import pdb
29                 pdb.set_trace()
30
31         if platform.python_implementation() == 'Jython':
32                 debug_signum = signal.SIGUSR2 # bug #424259
33         else:
34                 debug_signum = signal.SIGUSR1
35
36         signal.signal(debug_signum, debug_signal)
37
38         from os import path as osp
39         pym_path = osp.join(osp.dirname(osp.dirname(
40                 osp.realpath(__file__))), "pym")
41         sys.path.insert(0, pym_path)
42         from _emerge.main import emerge_main
43
44         if __name__ == "__main__":
45                 import sys
46                 from portage.exception import ParseError, PermissionDenied
47                 try:
48                         retval = emerge_main()
49                 except PermissionDenied as e:
50                         sys.stderr.write("Permission denied: '%s'\n" % str(e))
51                         sys.exit(e.errno)
52                 except ParseError as e:
53                         sys.stderr.write("%s\n" % str(e))
54                         sys.exit(1)
55                 except (KeyboardInterrupt, SystemExit):
56                         raise
57                 except Exception:
58                         # If an unexpected exception occurs then we don't want the
59                         # mod_echo output to obscure the traceback, so dump the
60                         # mod_echo output before showing the traceback.
61                         import traceback
62                         tb_str = traceback.format_exc()
63                         try:
64                                 from portage.elog import mod_echo
65                         except ImportError:
66                                 pass
67                         else:
68                                 mod_echo.finalize()
69                         sys.stderr.write(tb_str)
70                         sys.exit(1)
71                 sys.exit(retval)
72
73 except KeyboardInterrupt:
74         sys.stderr.write("\n\nExiting on signal %(signal)s\n" %
75                 {"signal": signal.SIGINT})
76         sys.stderr.flush()
77         sys.exit(128 + signal.SIGINT)