2 # Copyright 2006-2014 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
5 from __future__ import print_function
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
18 def exithandler(signum, _frame):
19 signal.signal(signal.SIGTERM, signal.SIG_IGN)
20 sys.exit(128 + signum)
22 signal.signal(signal.SIGTERM, exithandler)
23 # Prevent "[Errno 32] Broken pipe" exceptions when
25 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
27 def debug_signal(_signum, _frame):
31 if platform.python_implementation() == 'Jython':
32 debug_signum = signal.SIGUSR2 # bug #424259
34 debug_signum = signal.SIGUSR1
36 signal.signal(debug_signum, debug_signal)
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)
43 portage._internal_caller = True
44 portage._disable_legacy_globals()
45 from _emerge.main import emerge_main
47 if __name__ == "__main__":
48 from portage.exception import ParseError, PermissionDenied
50 retval = emerge_main()
51 except PermissionDenied as e:
52 sys.stderr.write("Permission denied: '%s'\n" % str(e))
54 except ParseError as e:
55 sys.stderr.write("%s\n" % str(e))
57 except (KeyboardInterrupt, SystemExit):
60 # If an unexpected exception occurs then we don't want the
61 # mod_echo output to obscure the traceback, so dump the
62 # mod_echo output before showing the traceback.
64 tb_str = traceback.format_exc()
66 from portage.elog import mod_echo
71 sys.stderr.write(tb_str)
75 except KeyboardInterrupt:
76 sys.stderr.write("\n\nExiting on signal %(signal)s\n" %
77 {"signal": signal.SIGINT})
79 sys.exit(128 + signal.SIGINT)