When killed by signal, return 128 + signum.
[portage.git] / bin / emerge
1 #!/usr/bin/python
2 # Copyright 2006-2011 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 from __future__ import print_function
6
7 import signal
8 import sys
9 # This block ensures that ^C interrupts are handled quietly.
10 try:
11
12         def exithandler(signum,frame):
13                 signal.signal(signal.SIGINT, signal.SIG_IGN)
14                 signal.signal(signal.SIGTERM, signal.SIG_IGN)
15                 sys.exit(128 + signum)
16
17         signal.signal(signal.SIGINT, exithandler)
18         signal.signal(signal.SIGTERM, exithandler)
19         # Prevent "[Errno 32] Broken pipe" exceptions when
20         # writing to a pipe.
21         signal.signal(signal.SIGPIPE, signal.SIG_DFL)
22
23 except KeyboardInterrupt:
24         sys.exit(128 + signal.SIGINT)
25
26 def debug_signal(signum, frame):
27         import pdb
28         pdb.set_trace()
29 signal.signal(signal.SIGUSR1, debug_signal)
30
31 try:
32         from _emerge.main import emerge_main
33 except ImportError:
34         from os import path as osp
35         import sys
36         sys.path.insert(0, osp.join(osp.dirname(osp.dirname(osp.realpath(__file__))), "pym"))
37         from _emerge.main import emerge_main
38
39 if __name__ == "__main__":
40         import sys
41         from portage.exception import ParseError, PermissionDenied
42         try:
43                 retval = emerge_main()
44         except PermissionDenied as e:
45                 sys.stderr.write("Permission denied: '%s'\n" % str(e))
46                 sys.exit(e.errno)
47         except ParseError as e:
48                 sys.stderr.write("%s\n" % str(e))
49                 sys.exit(1)
50         except SystemExit:
51                 raise
52         except Exception:
53                 # If an unexpected exception occurs then we don't want the mod_echo
54                 # output to obscure the traceback, so dump the mod_echo output before
55                 # showing the traceback.
56                 import traceback
57                 tb_str = traceback.format_exc()
58                 try:
59                         from portage.elog import mod_echo
60                 except ImportError:
61                         pass
62                 else:
63                         mod_echo.finalize()
64                 sys.stderr.write(tb_str)
65                 sys.exit(1)
66         sys.exit(retval)