entry_point.py
125 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
MinRK
|
r3550 | """ Defines helper functions for creating kernel entry points and process | ||
launchers. | ||||
MinRK
|
r3604 | |||
************ | ||||
NOTE: Most of this module has been deprecated by moving to Configurables | ||||
************ | ||||
MinRK
|
r3550 | """ | ||
MinRK
|
r3660 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2010-2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
MinRK
|
r3550 | |||
# Standard library imports. | ||||
import atexit | ||||
MinRK
|
r3631 | import logging | ||
MinRK
|
r3550 | import os | ||
MinRK
|
r3575 | import stat | ||
MinRK
|
r3550 | import socket | ||
MinRK
|
r3631 | import sys | ||
MinRK
|
r3573 | from signal import signal, SIGINT, SIGABRT, SIGTERM | ||
MinRK
|
r3631 | from subprocess import Popen, PIPE | ||
MinRK
|
r3573 | try: | ||
from signal import SIGKILL | ||||
except ImportError: | ||||
SIGKILL=None | ||||
MinRK
|
r3550 | |||
# System library imports. | ||||
import zmq | ||||
from zmq.log import handlers | ||||
MinRK
|
r3642 | |||
MinRK
|
r3550 | # Local imports. | ||
from IPython.core.ultratb import FormattedTB | ||||
from IPython.external.argparse import ArgumentParser | ||||
MinRK
|
r3603 | from IPython.zmq.log import EnginePUBHandler | ||
MinRK
|
r3550 | |||
MinRK
|
r3604 | _random_ports = set() | ||
MinRK
|
r3550 | def select_random_ports(n): | ||
MinRK
|
r3575 | """Selects and return n random ports that are available.""" | ||
MinRK
|
r3550 | ports = [] | ||
for i in xrange(n): | ||||
sock = socket.socket() | ||||
sock.bind(('', 0)) | ||||
MinRK
|
r3604 | while sock.getsockname()[1] in _random_ports: | ||
sock.close() | ||||
sock = socket.socket() | ||||
sock.bind(('', 0)) | ||||
MinRK
|
r3550 | ports.append(sock) | ||
for i, sock in enumerate(ports): | ||||
port = sock.getsockname()[1] | ||||
sock.close() | ||||
ports[i] = port | ||||
MinRK
|
r3604 | _random_ports.add(port) | ||
MinRK
|
r3550 | return ports | ||
MinRK
|
r3552 | |||
MinRK
|
r3573 | def signal_children(children): | ||
MinRK
|
r3575 | """Relay interupt/term signals to children, for more solid process cleanup.""" | ||
MinRK
|
r3573 | def terminate_children(sig, frame): | ||
MinRK
|
r3604 | logging.critical("Got signal %i, terminating children..."%sig) | ||
MinRK
|
r3573 | for child in children: | ||
child.terminate() | ||||
MinRK
|
r3604 | |||
sys.exit(sig != SIGINT) | ||||
MinRK
|
r3573 | # sys.exit(sig) | ||
for sig in (SIGINT, SIGABRT, SIGTERM): | ||||
signal(sig, terminate_children) | ||||
MinRK
|
r3550 | |||
MinRK
|
r3575 | def generate_exec_key(keyfile): | ||
import uuid | ||||
newkey = str(uuid.uuid4()) | ||||
with open(keyfile, 'w') as f: | ||||
# f.write('ipython-key ') | ||||
MinRK
|
r3604 | f.write(newkey+'\n') | ||
MinRK
|
r3575 | # set user-only RW permissions (0600) | ||
# this will have no effect on Windows | ||||
os.chmod(keyfile, stat.S_IRUSR|stat.S_IWUSR) | ||||
MinRK
|
r3550 | |||
MinRK
|
r3603 | def integer_loglevel(loglevel): | ||
MinRK
|
r3602 | try: | ||
loglevel = int(loglevel) | ||||
except ValueError: | ||||
if isinstance(loglevel, str): | ||||
loglevel = getattr(logging, loglevel) | ||||
MinRK
|
r3603 | return loglevel | ||
MinRK
|
r3610 | def connect_logger(logname, context, iface, root="ip", loglevel=logging.DEBUG): | ||
logger = logging.getLogger(logname) | ||||
MinRK
|
r3604 | if any([isinstance(h, handlers.PUBHandler) for h in logger.handlers]): | ||
# don't add a second PUBHandler | ||||
return | ||||
MinRK
|
r3603 | loglevel = integer_loglevel(loglevel) | ||
MinRK
|
r3550 | lsock = context.socket(zmq.PUB) | ||
lsock.connect(iface) | ||||
handler = handlers.PUBHandler(lsock) | ||||
handler.setLevel(loglevel) | ||||
handler.root_topic = root | ||||
MinRK
|
r3603 | logger.addHandler(handler) | ||
logger.setLevel(loglevel) | ||||
def connect_engine_logger(context, iface, engine, loglevel=logging.DEBUG): | ||||
logger = logging.getLogger() | ||||
MinRK
|
r3604 | if any([isinstance(h, handlers.PUBHandler) for h in logger.handlers]): | ||
# don't add a second PUBHandler | ||||
return | ||||
MinRK
|
r3603 | loglevel = integer_loglevel(loglevel) | ||
lsock = context.socket(zmq.PUB) | ||||
lsock.connect(iface) | ||||
handler = EnginePUBHandler(engine, lsock) | ||||
handler.setLevel(loglevel) | ||||
logger.addHandler(handler) | ||||
logger.setLevel(loglevel) | ||||
MinRK
|
r3610 | def local_logger(logname, loglevel=logging.DEBUG): | ||
MinRK
|
r3603 | loglevel = integer_loglevel(loglevel) | ||
MinRK
|
r3610 | logger = logging.getLogger(logname) | ||
MinRK
|
r3604 | if any([isinstance(h, logging.StreamHandler) for h in logger.handlers]): | ||
# don't add a second StreamHandler | ||||
MinRK
|
r3603 | return | ||
handler = logging.StreamHandler() | ||||
handler.setLevel(loglevel) | ||||
MinRK
|
r3550 | logger.addHandler(handler) | ||
MinRK
|
r3603 | logger.setLevel(loglevel) | ||