entry_point.py
118 lines
| 3.5 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 | """ | ||
# Standard library imports. | ||||
import logging | ||||
import atexit | ||||
MinRK
|
r3573 | import sys | ||
MinRK
|
r3550 | import os | ||
MinRK
|
r3575 | import stat | ||
MinRK
|
r3550 | import socket | ||
from subprocess import Popen, PIPE | ||||
MinRK
|
r3573 | from signal import signal, SIGINT, SIGABRT, SIGTERM | ||
try: | ||||
from signal import SIGKILL | ||||
except ImportError: | ||||
SIGKILL=None | ||||
MinRK
|
r3550 | |||
# System library imports. | ||||
import zmq | ||||
from zmq.log import handlers | ||||
# 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 | ||
def connect_logger(context, iface, root="ip", loglevel=logging.DEBUG): | ||||
MinRK
|
r3604 | logger = logging.getLogger() | ||
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) | ||||
def local_logger(loglevel=logging.DEBUG): | ||||
loglevel = integer_loglevel(loglevel) | ||||
logger = logging.getLogger() | ||||
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) | ||