Skip to content

Commit

Permalink
Separate kernel script
Browse files Browse the repository at this point in the history
Kernel should be started with bin/iruby_kernel instead of lib/kernel.rb
  • Loading branch information
munshkr committed Jun 24, 2013
1 parent e54208c commit 75c79a9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 71 deletions.
96 changes: 96 additions & 0 deletions bin/iruby_kernel
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env ruby

USAGE = <<-EOS
This script is not intended to be used manually.
To start an IPython Notebook server with IRuby, first create a profile with
`iruby_profile`, then use `ipython notebook` to start the server:
$ iruby_profile --create
$ ipython notebook --profile=iruby_default
Read more at http://ipython.org/ipython-doc/stable/interactive/htmlnotebook.html
EOS

def start_kernel!(config_path, boot_file=nil)
#ENV["BUNDLE_GEM"] = "/home/munshkr/overol-parsers/Gemfile"
#ENV["APP_ROOT"] = "/home/munshkr/overol-parsers"

require boot_file if boot_file
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'kernel.rb'))

configfile = File.read(config_path)
config = JSON.parse(configfile)

c = ZMQ::Context.new

shell_port = config['shell_port']
pub_port = config['iopub_port']
hb_port = config['hb_port']

ip = '127.0.0.1'
connection = ('tcp://%s' % ip) + ':%i'
shell_conn = connection % shell_port
pub_conn = connection % pub_port
hb_conn = connection % hb_port

$stdout.puts "Starting the kernel..."
$stdout.puts "On:",shell_conn, pub_conn, hb_conn

session = Session.new('kernel')

reply_socket = c.socket(ZMQ::XREP)
reply_socket.bind(shell_conn)

pub_socket = c.socket(ZMQ::PUB)
pub_socket.bind(pub_conn)

hb_thread = Thread.new do
hb_socket = c.socket(ZMQ::REP)
hb_socket.bind(hb_conn)
ZMQ::Device.new(ZMQ::FORWARDER, hb_socket, hb_socket)
end

stdout = OutStream.new(session, pub_socket, 'stdout')
#stderr = OutStream.new(session, pub_socket, 'stderr')
old_stdout = STDOUT
$stdout = stdout
#$stderr = stderr


kernel = RKernel.new(session, reply_socket, pub_socket)
display_hook = DisplayHook.new(kernel, session, pub_socket)
$displayhook = display_hook

# For debugging convenience, put sleep and a string in the namespace, so we
# have them every time we start.
#kernel.user_ns['sleep'] = sleep
#kernel.user_ns['s'] = 'Test string'

old_stdout.puts "Use Ctrl-\\ (NOT Ctrl-C!) to terminate."
kernel.start(display_hook)
end

def main
if ARGV.size == 0 || ARGV.size > 2
puts USAGE
exit(1)
end
config_path, boot_file = ARGV

if !File.exists?(config_path)
puts "Config file '#{config_path}' does not exist"
exit(2)
end

if boot_file && !File.exists?(boot_file)
puts "File '#{boot_file}' does not exist"
exit(3)
end

start_kernel!(config_path, boot_file)
end


main if __FILE__ == $0
69 changes: 0 additions & 69 deletions lib/kernel.rb
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env ruby

require 'ffi-rzmq'
require 'json'
require 'ostruct'
Expand Down Expand Up @@ -160,70 +158,3 @@ def format_exception(name, value, backtrace)
tb
end
end

def main(configfile_path)
# read configfile
# get the following from it:
# - shell_port
# - iopub_port
# - stdin_port
# - hb_port
# - ip
# - key

configfile = File.read(configfile_path)
config = JSON.parse(configfile)

c = ZMQ::Context.new

shell_port = config['shell_port']
pub_port = config['iopub_port']
hb_port = config['hb_port']

ip = '127.0.0.1'
connection = ('tcp://%s' % ip) + ':%i'
shell_conn = connection % shell_port
pub_conn = connection % pub_port
hb_conn = connection % hb_port

$stdout.puts "Starting the kernel..."
$stdout.puts "On:",shell_conn, pub_conn, hb_conn

session = Session.new('kernel')

reply_socket = c.socket(ZMQ::XREP)
reply_socket.bind(shell_conn)

pub_socket = c.socket(ZMQ::PUB)
pub_socket.bind(pub_conn)

hb_thread = Thread.new do
hb_socket = c.socket(ZMQ::REP)
hb_socket.bind(hb_conn)
ZMQ::Device.new(ZMQ::FORWARDER, hb_socket, hb_socket)
end

stdout = OutStream.new(session, pub_socket, 'stdout')
#stderr = OutStream.new(session, pub_socket, 'stderr')
old_stdout = STDOUT
$stdout = stdout
#$stderr = stderr


kernel = RKernel.new(session, reply_socket, pub_socket)
display_hook = DisplayHook.new(kernel, session, pub_socket)
$displayhook = display_hook

# For debugging convenience, put sleep and a string in the namespace, so we
# have them every time we start.
#kernel.user_ns['sleep'] = sleep
#kernel.user_ns['s'] = 'Test string'

old_stdout.puts "Use Ctrl-\\ (NOT Ctrl-C!) to terminate."
kernel.start(display_hook)
end


if __FILE__ == $0
main(ARGV[0])
end
5 changes: 3 additions & 2 deletions lib/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ class Profile
attr_accessor :iruby_name

NAME_PREFIX = "iruby_"
IRUBY_KERNEL_PATH = File.join(File.dirname(__FILE__), '..', 'bin', 'iruby_kernel')
IPYTHON_PROFILE_PATH = File.join(Dir.home, '.config', 'ipython')

# FIXME These should be stored as ERB files
PROFILE_CONFIG = {}
PROFILE_CONFIG['ipython_notebook_config.py'] = <<-PYTHON
## IRuby custom configuration
iruby_kernel_path = '#{File.absolute_path(File.join(File.dirname(__FILE__), 'kernel.rb'))}'
c.KernelManager.kernel_cmd = ['ruby', iruby_kernel_path, '{connection_file}']
iruby_kernel_path = '#{File.expand_path(IRUBY_KERNEL_PATH)}'
c.KernelManager.kernel_cmd = [iruby_kernel_path, '{connection_file}']
c.Session.key = ''
PYTHON

Expand Down

0 comments on commit 75c79a9

Please sign in to comment.