a2km 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/a2km +284 -0
  3. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 76ed2ec1339c1487ce7d6a76409351704bcb7189
4
+ data.tar.gz: 27373e1bc5980983ae56f0ef4bbe81fae6d7f6cc
5
+ SHA512:
6
+ metadata.gz: 135ca36a33606c8222ab3f835a6801dbe0c16e82c43e5b29c8f59af338a9d9cf5215de70ba9bc95fbbc64339f0d10ec26ce3e0b49e0a857eee9b3c94640f6ac9
7
+ data.tar.gz: 7dc5323e9b547e13e1d0f8fc0afdab70cf0421dc1fd898bc4158c91be6349588970884826c1e3858708d18ca99d3e7fb0bab923924f0ba67d84a02248ce34de2
data/bin/a2km ADDED
@@ -0,0 +1,284 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+
5
+ require 'rubygems'
6
+ require 'commander'
7
+ require 'highline'
8
+
9
+
10
+ class AssitantToTheKernelManager
11
+ include Commander::Methods
12
+
13
+ def initialize
14
+ @kernels = nil
15
+ end
16
+
17
+ def kernels
18
+ if @kernels.nil?
19
+ js = JSON.parse `jupyter kernelspec list --json`
20
+ @kernels = js['kernelspecs']
21
+ end
22
+ @kernels
23
+ end
24
+
25
+ def kernel_dirs
26
+ paths = JSON.parse `jupyter --paths --json`
27
+ paths['data'].map do |p|
28
+ p + '/' + 'kernels'
29
+ end
30
+ end
31
+
32
+ def user_kernel_dir
33
+ kernel_dirs.first
34
+ end
35
+
36
+ def get_kernel(name)
37
+ "Get a kernel by name. Die with message if not found."
38
+ if not kernels.has_key? name
39
+ STDERR.puts "No such kernel: #{name}"
40
+ STDERR.puts "Found kernels: #{kernels.keys.sort.join(' ')}"
41
+ exit(-1)
42
+ end
43
+ kernels[name]
44
+ end
45
+
46
+ def kernel_json_path(name)
47
+ "Return path to a kernel's kernel.json"
48
+ kernel = get_kernel(name)
49
+ File.join(kernel['resources_dir'], 'kernel.json')
50
+ end
51
+
52
+ def get_kernel_json(name)
53
+ "Return kernel.json contents for a given kernelspec"
54
+ File.open(kernel_json_path(name)) do |f|
55
+ return JSON.parse f.read
56
+ end
57
+ end
58
+
59
+ def save_kernel_json(name, data)
60
+ "save new kernel JSON"
61
+ File.open(kernel_json_path(name), 'w') do |f|
62
+ f.write JSON.pretty_generate(data)
63
+ end
64
+ end
65
+
66
+ def run
67
+ program :name, 'Assistant to the KernelManager'
68
+ program :version, '0.0.0'
69
+ program :description, 'Work with Jupyter kernelspecs'
70
+
71
+ command :rename do |c|
72
+ c.syntax = 'a2km rename <from> <to>'
73
+ c.description = 'renames kernelspec FROM to TO'
74
+ c.action do |args, options|
75
+ from = args.shift
76
+ to = args.shift
77
+ kernel = get_kernel(from)
78
+ src = kernel['resources_dir']
79
+
80
+ dst = File.join File.dirname(src), to
81
+ if File.exists? dst
82
+ STDERR.puts "Destination already exists: #{dst}"
83
+ exit(-1)
84
+ end
85
+
86
+ puts "Moving #{src} → #{dst}"
87
+ FileUtils.mv src, dst
88
+ end
89
+ end
90
+
91
+ command :show do |c|
92
+ c.syntax = 'a2km show <spec>'
93
+ c.description = 'Show info about a kernelspec'
94
+ c.action do |args, options|
95
+ name = args.first
96
+ kernel = get_kernel(name)
97
+ spec = kernel['spec']
98
+ puts "Kernel: #{name} (#{spec['display_name']})"
99
+ puts " path: #{kernel['resources_dir']}"
100
+ puts " argv: #{spec['argv'].join(' ')}"
101
+ end
102
+ end
103
+
104
+ command :locate do |c|
105
+ c.syntax = 'a2km locate <spec>'
106
+ c.description = 'Print the path of a kernelspec'
107
+ c.action do |args, options|
108
+ puts get_kernel(args.first)['resources_dir']
109
+ end
110
+ end
111
+
112
+ command :"add-argv" do |c|
113
+ c.syntax = 'a2km add-argv <spec> <arg1> [arg2] ...'
114
+ c.description = 'Add argument(s) to a kernelspec launch command'
115
+ c.action do |args, options|
116
+ name = args.shift
117
+ kernelspec = get_kernel_json(name)
118
+ kernelspec['argv'] += args
119
+ save_kernel_json(name, kernelspec)
120
+ puts "New argv: #{kernelspec['argv'].join(' ')}"
121
+ end
122
+ end
123
+
124
+ command :"rm-argv" do |c|
125
+ c.syntax = 'a2km rm-argv <spec> <arg1> [arg2] ...'
126
+ c.summary = 'Remove arguments from a kernelspec launch command'
127
+ c.description = c.summary + ". To remove args starting with '-'," \
128
+ " use '--'."
129
+ c.examples = [
130
+ "a2km rm-argv myspec x",
131
+ "a2km rm-argv myspec -- --debug",
132
+ ]
133
+ c.action do |args, options|
134
+ name = args.shift
135
+ to_remove = args
136
+ kernelspec = get_kernel_json(name)
137
+ argv = kernelspec['argv']
138
+ argv.reject! { |arg| to_remove.include? arg }
139
+ save_kernel_json(name, kernelspec)
140
+ puts "New argv: #{argv.join(' ')}"
141
+ end
142
+ end
143
+
144
+ command :"add-env" do |c|
145
+ c.syntax = 'a2km add-env <spec> <key=value> [key=value] ...'
146
+ c.description = 'Add environment variables to a kernelspec.' \
147
+ ' If no value is given, the value from the current env is used.'
148
+ c.action do |args, options|
149
+ name = args.shift
150
+ spec = get_kernel_json(name)
151
+ if not spec.has_key? 'env'
152
+ spec['env'] = {}
153
+ end
154
+ env = spec['env']
155
+ args.each do |arg|
156
+ key_value = arg.split('=', 2)
157
+ key = key_value.first
158
+ if key_value.length == 2
159
+ value = key_value[1]
160
+ else
161
+ value = ENV[key]
162
+ end
163
+ env[key] = value
164
+ end
165
+ save_kernel_json(name, spec)
166
+ puts "New env: #{env}"
167
+ end
168
+ end
169
+
170
+ command :"rm-env" do |c|
171
+ c.syntax = 'a2km rm-env <spec> <key> [key] ...'
172
+ c.description = 'Remove environment variables from a kernelspec.'
173
+ c.action do |args, options|
174
+ name = args.shift
175
+ spec = get_kernel_json(name)
176
+ if not spec.has_key? 'env'
177
+ spec['env'] = {}
178
+ end
179
+ env = spec['env']
180
+ args.each do |arg|
181
+ env.delete(arg)
182
+ end
183
+ save_kernel_json(name, spec)
184
+ puts "New env: #{env}"
185
+ end
186
+ end
187
+
188
+ command :rm do |c|
189
+ c.syntax = 'a2km rm <spec>'
190
+ c.description = 'Remove a kernelspec'
191
+ c.option '-f', "Force removal (skip confirmation)"
192
+ c.action do |args, options|
193
+ name = args.shift
194
+ if not kernels.has_key? name
195
+ STDERR.puts "No such kernel: #{name}"
196
+ STDERR.puts "Found kernels: #{kernels.keys.sort.join(' ')}"
197
+ exit(-1)
198
+ end
199
+ path = kernels[name]['resources_dir']
200
+ if options.f or not HighLine.agree("Permanently delete #{path}? (yes/no)")
201
+ STDERR.puts "Aborting."
202
+ exit(-1)
203
+ end
204
+ puts "Removing #{path}"
205
+ FileUtils.rm_r path
206
+ end
207
+ end
208
+
209
+ command :set do |c|
210
+ c.syntax = 'a2km set <name> <key> <value>'
211
+ c.description = 'Set a value in the kernelspec'
212
+ c.examples = [
213
+ 'a2km set python3 display_name "My Python 3"'
214
+ ]
215
+ c.action do |args, options|
216
+ name = args.shift
217
+ key = args.shift
218
+ value = args.shift
219
+ if value.nil?
220
+ STDERR.puts c.syntax
221
+ exit(-1)
222
+ end
223
+ puts "Setting #{name}.#{key} = '#{value}'"
224
+ spec = get_kernel_json(name)
225
+ spec[key] = value
226
+ save_kernel_json(name, spec)
227
+ end
228
+ end
229
+
230
+ command :clone do |c|
231
+ c.syntax = 'a2km clone <from> <to> [display_name]'
232
+ c.summary = 'Clone a kernelspec'
233
+ c.description = 'Clones kernelspec FROM to TO'
234
+ c.option '--user', 'Force clone to be in the user directory' \
235
+ ' (default is to use the same directory as FROM)'
236
+ c.action do |args, options|
237
+ options.default :user => false
238
+
239
+ if args.length < 2 or args.length > 3
240
+ STDERR.puts "Must specify FROM and TO"
241
+ exit(-1)
242
+ end
243
+ from_name = args.shift
244
+ to_name = args.shift
245
+ if args.length > 0
246
+ display_name = args.shift
247
+ else
248
+ display_name = to_name
249
+ end
250
+
251
+ from = get_kernel(from_name)
252
+ from = kernels[from_name]
253
+ src = from['resources_dir']
254
+
255
+ if options.user?
256
+ dst_dir = user_kernel_dir
257
+ makedirs(dst_dir)
258
+ else
259
+ dst_dir = File.dirname src
260
+ end
261
+ dst = File.join dst_dir, to_name
262
+ if File.exists? dst
263
+ STDERR.puts "Destination already exists: #{dst}"
264
+ exit(-1)
265
+ end
266
+
267
+ puts "Cloning #{src} → #{dst}"
268
+ FileUtils.cp_r src, dst
269
+ kernel_json = File.join(dst, 'kernel.json')
270
+ kernelspec = File.open(kernel_json) do |f|
271
+ JSON.parse f.read
272
+ end
273
+ kernelspec['display_name'] = display_name
274
+ File.open(kernel_json, 'w') do |f|
275
+ f.write JSON.pretty_generate(kernelspec)
276
+ end
277
+ end
278
+ end
279
+
280
+ run!
281
+ end
282
+ end
283
+
284
+ AssitantToTheKernelManager.new.run
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: a2km
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Min RK
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Working with Juptyer kernels
14
+ email: benjaminrk@gmail.com
15
+ executables:
16
+ - a2km
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/a2km
21
+ homepage: http://github.com/minrk/a2km
22
+ licenses:
23
+ - BSD
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements:
40
+ - commander
41
+ - highline
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.5
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Assistant to the Kernel Manager
47
+ test_files: []
48
+ has_rdoc: