iruby 0.7.4 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/iruby DELETED
@@ -1,5 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift File.expand_path(__dir__ + '/../lib')
4
- require 'iruby/command'
5
- IRuby::Command.new(ARGV).run
data/lib/iruby/command.rb DELETED
@@ -1,190 +0,0 @@
1
- require 'iruby'
2
-
3
- module IRuby
4
- class Command
5
- def initialize(args)
6
- @args = args
7
-
8
- @ipython_dir = File.expand_path("~/.ipython")
9
- @kernel_dir = resolve_kernelspec_dir.freeze
10
- @kernel_file = File.join(@kernel_dir, 'kernel.json').freeze
11
- @iruby_path = File.expand_path $0
12
- end
13
-
14
- attr_reader :ipython_dir, :kernel_dir, :kernel_file
15
-
16
- def ipython_kernel_dir
17
- File.join(File.expand_path(@ipython_dir), 'kernels', 'ruby')
18
- end
19
-
20
- def run
21
- case @args.first
22
- when 'version', '-v', '--version'
23
- require 'iruby/version'
24
- puts "IRuby #{IRuby::VERSION}, Ruby #{RUBY_VERSION}"
25
- when 'help', '-h', '--help'
26
- print_help
27
- when 'register'
28
- force_p = @args.include?('--force')
29
- if registered_iruby_path && !force_p
30
- STDERR.puts "#{@kernel_file} already exists!\nUse --force to force a register."
31
- exit 1
32
- end
33
- register_kernel(force_p)
34
- when 'unregister'
35
- unregister_kernel
36
- when 'kernel'
37
- run_kernel
38
- else
39
- run_ipython
40
- end
41
- end
42
-
43
- private
44
-
45
- def resolve_kernelspec_dir
46
- if ENV.has_key?('JUPYTER_DATA_DIR')
47
- if ENV.has_key?('IPYTHONDIR')
48
- warn 'both JUPYTER_DATA_DIR and IPYTHONDIR are supplied; IPYTHONDIR is ignored.'
49
- end
50
- if @args.find {|x| /\A--ipython-dir=/ =~ x }
51
- warn 'both JUPYTER_DATA_DIR and --ipython-dir are supplied; --ipython-dir is ignored.'
52
- end
53
- jupyter_data_dir = ENV['JUPYTER_DATA_DIR']
54
- return File.join(jupyter_data_dir, 'kernels', 'ruby')
55
- end
56
-
57
- if ENV.has_key?('IPYTHONDIR')
58
- warn 'IPYTHONDIR is deprecated. Use JUPYTER_DATA_DIR instead.'
59
- ipython_dir = ENV['IPYTHONDIR']
60
- end
61
-
62
- @args.each do |arg|
63
- next unless /\A--ipython-dir=(.*)\Z/ =~ arg
64
- ipython_dir = Regexp.last_match[1]
65
- warn '--ipython-dir is deprecated. Use JUPYTER_DATA_DIR environment variable instead.'
66
- break
67
- end
68
-
69
- if ipython_dir
70
- @ipython_dir = ipython_dir
71
- ipython_kernel_dir
72
- else
73
- File.join(Jupyter.kernelspec_dir, 'ruby')
74
- end
75
- end
76
-
77
- def print_help
78
- puts %{
79
- Usage:
80
- iruby register Register IRuby kernel in #{@kernel_file}.
81
- iruby unregister Unregister IRuby kernel.
82
- iruby console Launch the IRuby terminal-based console.
83
- iruby notebook Launch the IRuby HTML notebook server.
84
- ... Same as IPython.
85
-
86
- Please note that IRuby accepts the same parameters as IPython.
87
- Try `ipython help` for more information.
88
- }
89
- end
90
-
91
- def run_kernel
92
- IRuby.logger = MultiLogger.new(*Logger.new(STDOUT))
93
- STDOUT.sync = true # FIXME: This can make the integration test.
94
-
95
- @args.reject! {|arg| arg =~ /\A--log=(.*)\Z/ && IRuby.logger.loggers << Logger.new($1) }
96
- IRuby.logger.level = @args.delete('--debug') ? Logger::DEBUG : Logger::INFO
97
-
98
- raise(ArgumentError, 'Not enough arguments to the kernel') if @args.size < 2 || @args.size > 4
99
- config_file, boot_file, working_dir = @args[1..-1]
100
- Dir.chdir(working_dir) if working_dir
101
-
102
- require boot_file if boot_file
103
- check_bundler {|e| IRuby.logger.warn "Could not load bundler: #{e.message}" }
104
-
105
- require 'iruby'
106
- Kernel.new(config_file).run
107
- rescue Exception => e
108
- IRuby.logger.fatal "Kernel died: #{e.message}\n#{e.backtrace.join("\n")}"
109
- raise
110
- end
111
-
112
- def check_version
113
- required = '3.0.0'
114
- version = `ipython --version`.chomp
115
- if version < required
116
- STDERR.puts "Your IPython version #{version} is too old, at least #{required} is required"
117
- exit 1
118
- end
119
- end
120
-
121
- def run_ipython
122
- # If no command is given, we use the console to launch the whole 0MQ-client-server stack
123
- @args = %w(console) + @args if @args.first.to_s !~ /\A\w/
124
- @args += %w(--kernel ruby) if %w(console qtconsole).include? @args.first
125
-
126
- check_version
127
- check_registered_kernel
128
- check_bundler {|e| STDERR.puts "Could not load bundler: #{e.message}" }
129
-
130
- Process.exec('ipython', *@args)
131
- end
132
-
133
- def check_registered_kernel
134
- if (kernel = registered_iruby_path)
135
- STDERR.puts "#{@iruby_path} differs from registered path #{registered_iruby_path}.
136
- This might not work. Run 'iruby register --force' to fix it." if @iruby_path != kernel
137
- else
138
- register_kernel
139
- end
140
- end
141
-
142
- def check_bundler
143
- require 'bundler'
144
- raise %q{iruby is missing from Gemfile. This might not work.
145
- Add `gem 'iruby'` to your Gemfile to fix it.} unless Bundler.definition.specs.any? {|s| s.name == 'iruby' }
146
- Bundler.setup
147
- rescue LoadError
148
- rescue Exception => e
149
- yield(e)
150
- end
151
-
152
- def register_kernel(force_p=false)
153
- if force_p
154
- unregister_kernel_in_ipython_dir
155
- else
156
- return unless check_existing_kernel_in_ipython_dir
157
- end
158
- FileUtils.mkpath(@kernel_dir)
159
- unless RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin/
160
- File.write(@kernel_file, MultiJson.dump(argv: [ @iruby_path, 'kernel', '{connection_file}' ],
161
- display_name: "Ruby #{RUBY_VERSION}", language: 'ruby'))
162
- else
163
- ruby_path, iruby_path = [RbConfig.ruby, @iruby_path].map{|path| path.gsub('/', '\\\\')}
164
- File.write(@kernel_file, MultiJson.dump(argv: [ ruby_path, iruby_path, 'kernel', '{connection_file}' ],
165
- display_name: "Ruby #{RUBY_VERSION}", language: 'ruby'))
166
- end
167
-
168
- FileUtils.copy(Dir[File.join(__dir__, 'assets', '*')], @kernel_dir) rescue nil
169
- end
170
-
171
- def check_existing_kernel_in_ipython_dir
172
- return true unless File.file?(File.join(ipython_kernel_dir, 'kernel.json'))
173
- warn "IRuby kernel file already exists in the deprecated IPython's data directory."
174
- warn "Using --force, you can replace the old kernel file with the new one in Jupyter's data directory."
175
- false
176
- end
177
-
178
- def registered_iruby_path
179
- File.exist?(@kernel_file) && MultiJson.load(File.read(@kernel_file))['argv'].first
180
- end
181
-
182
- def unregister_kernel
183
- FileUtils.rm_rf(@kernel_dir)
184
- end
185
-
186
- def unregister_kernel_in_ipython_dir
187
- FileUtils.rm_rf(ipython_kernel_dir)
188
- end
189
- end
190
- end
@@ -1,207 +0,0 @@
1
- require 'iruby/command'
2
-
3
- module IRubyTest
4
- class CommandTest < TestBase
5
- def test_with_empty_argv
6
- with_env('JUPYTER_DATA_DIR' => nil,
7
- 'IPYTHONDIR' => nil) do
8
- assert_output(nil, /\A\z/) do
9
- @command = IRuby::Command.new([])
10
- kernel_dir = File.join(IRuby::Jupyter.kernelspec_dir, 'ruby')
11
- assert_equal(kernel_dir, @command.kernel_dir)
12
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
13
- end
14
- end
15
- end
16
-
17
- def test_with_JUPYTER_DATA_DIR
18
- Dir.mktmpdir do |tmpdir|
19
- with_env('JUPYTER_DATA_DIR' => tmpdir,
20
- 'IPYTHONDIR' => nil) do
21
- assert_output(nil, /\A\z/) do
22
- @command = IRuby::Command.new([])
23
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
24
- assert_equal(kernel_dir, @command.kernel_dir)
25
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
26
- end
27
- end
28
- end
29
- end
30
-
31
- def test_with_IPYTHONDIR
32
- Dir.mktmpdir do |tmpdir|
33
- with_env('JUPYTER_DATA_DIR' => nil,
34
- 'IPYTHONDIR' => tmpdir) do
35
- assert_output(nil, /IPYTHONDIR is deprecated\. Use JUPYTER_DATA_DIR instead\./) do
36
- @command = IRuby::Command.new([])
37
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
38
- assert_equal(kernel_dir, @command.kernel_dir)
39
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
40
- end
41
- end
42
- end
43
- end
44
-
45
- def test_with_JUPYTER_DATA_DIR_and_IPYTHONDIR
46
- Dir.mktmpdir do |tmpdir|
47
- Dir.mktmpdir do |tmpdir2|
48
- with_env('JUPYTER_DATA_DIR' => tmpdir,
49
- 'IPYTHONDIR' => tmpdir2) do
50
- assert_output(nil, /both JUPYTER_DATA_DIR and IPYTHONDIR are supplied; IPYTHONDIR is ignored\./) do
51
- @command = IRuby::Command.new([])
52
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
53
- assert_equal(kernel_dir, @command.kernel_dir)
54
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
55
- end
56
- end
57
- end
58
- end
59
- end
60
-
61
- def test_with_ipython_dir_option
62
- Dir.mktmpdir do |tmpdir|
63
- with_env('JUPYTER_DATA_DIR' => nil,
64
- 'IPYTHONDIR' => nil) do
65
- assert_output(nil, /--ipython-dir is deprecated\. Use JUPYTER_DATA_DIR environment variable instead\./) do
66
- @command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir}])
67
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
68
- assert_equal(kernel_dir, @command.kernel_dir)
69
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
70
- end
71
- end
72
- end
73
- end
74
-
75
- def test_with_JUPYTER_DATA_DIR_and_ipython_dir_option
76
- Dir.mktmpdir do |tmpdir|
77
- Dir.mktmpdir do |tmpdir2|
78
- with_env('JUPYTER_DATA_DIR' => tmpdir,
79
- 'IPYTHONDIR' => nil) do
80
- assert_output(nil, /both JUPYTER_DATA_DIR and --ipython-dir are supplied; --ipython-dir is ignored\./) do
81
- @command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir2}])
82
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
83
- assert_equal(kernel_dir, @command.kernel_dir)
84
- assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
85
- end
86
- end
87
- end
88
- end
89
- end
90
-
91
- def test_register_when_there_is_kernel_in_ipython_dir
92
- Dir.mktmpdir do |tmpdir|
93
- Dir.mktmpdir do |tmpdir2|
94
- with_env('JUPYTER_DATA_DIR' => nil,
95
- 'IPYTHONDIR' => nil,
96
- 'HOME' => tmpdir2) do
97
- ignore_warning do
98
- @command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
99
- assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
100
- @command.run
101
- assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
102
- end
103
- end
104
-
105
- with_env('JUPYTER_DATA_DIR' => nil,
106
- 'IPYTHONDIR' => nil,
107
- 'HOME' => tmpdir2) do
108
- @command = IRuby::Command.new(["register"])
109
- assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
110
- @command.run
111
- end
112
- assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
113
- refute(File.file?(@command.kernel_file))
114
-
115
- @command = IRuby::Command.new(["register", "--force"])
116
- assert_output(nil, nil) do
117
- @command.run
118
- end
119
- refute(File.exist?(@command.ipython_kernel_dir))
120
- assert(File.file?(@command.kernel_file))
121
- end
122
- end
123
- end
124
- end
125
-
126
- def test_register_and_unregister_with_JUPYTER_DATA_DIR
127
- Dir.mktmpdir do |tmpdir|
128
- Dir.mktmpdir do |tmpdir2|
129
- with_env('JUPYTER_DATA_DIR' => tmpdir,
130
- 'IPYTHONDIR' => nil,
131
- 'HOME' => tmpdir2) do
132
- assert_output(nil, nil) do
133
- @command = IRuby::Command.new(['register'])
134
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
135
- kernel_file = File.join(kernel_dir, 'kernel.json')
136
- assert(!File.file?(kernel_file))
137
-
138
- @command.run
139
- assert(File.file?(kernel_file))
140
-
141
- @command = IRuby::Command.new(['unregister'])
142
- @command.run
143
- assert(!File.file?(kernel_file))
144
- end
145
- end
146
- end
147
- end
148
- end
149
-
150
- def test_register_and_unregister_with_JUPYTER_DATA_DIR_when_there_is_kernel_in_ipython_dir
151
- Dir.mktmpdir do |tmpdir|
152
- Dir.mktmpdir do |tmpdir2|
153
- with_env('HOME' => tmpdir2) do
154
- ignore_warning do
155
- @command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
156
- assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
157
- @command.run
158
- assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
159
- end
160
- end
161
-
162
- with_env('JUPYTER_DATA_DIR' => tmpdir,
163
- 'IPYTHONDIR' => nil,
164
- 'HOME' => tmpdir2) do
165
- @command = IRuby::Command.new(["register"])
166
- assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
167
- @command.run
168
- end
169
- assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
170
- refute(File.file?(@command.kernel_file))
171
-
172
- @command = IRuby::Command.new(["register", "--force"])
173
- assert_output(nil, nil) do
174
- @command.run
175
- end
176
- refute(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
177
- assert(File.file?(@command.kernel_file))
178
- end
179
- end
180
- end
181
- end
182
-
183
- def test_register_and_unregister_with_IPYTHONDIR
184
- Dir.mktmpdir do |tmpdir|
185
- Dir.mktmpdir do |tmpdir2|
186
- with_env('JUPYTER_DATA_DIR' => nil,
187
- 'IPYTHONDIR' => tmpdir,
188
- 'HOME' => tmpdir2) do
189
- ignore_warning do
190
- @command = IRuby::Command.new(['register'])
191
- kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
192
- kernel_file = File.join(kernel_dir, 'kernel.json')
193
- assert(!File.file?(kernel_file))
194
-
195
- @command.run
196
- assert(File.file?(kernel_file))
197
-
198
- @command = IRuby::Command.new(['unregister'])
199
- @command.run
200
- assert(!File.file?(kernel_file))
201
- end
202
- end
203
- end
204
- end
205
- end
206
- end
207
- end