-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstallkernel.java
More file actions
executable file
·637 lines (531 loc) · 25.6 KB
/
Copy pathinstallkernel.java
File metadata and controls
executable file
·637 lines (531 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 11+
//DEPS info.picocli:picocli:4.6.3
//DEPS com.fasterxml.jackson.core:jackson-databind:2.12.3
//JAVA_OPTIONS -Djava.awt.headless=true
//FILES ipc_proxy_kernel.py
import static java.lang.String.format;
import static java.lang.System.out;
import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.write;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "install-kernel", mixinStandardHelpOptions = true, version = "install-kernel 0.1",
description = "Installs JVM based Kernels that can be run via maven artifacts using JBang", showDefaultValues=true)
class installkernel implements Callable<Integer> {
private static final String CONNECTION_FILE_MARKER = "{connection_file}";
private static final String LANGUAGE = "java";
private static final String INTERRUPT_MODE = "message";
private enum Kernels {
IJAVA {
String shortName() { return "IJava"; }
String ga() { return "com.github.waikato.thirdparty:ijava"; }
String v() { return "1.3.0"; }
String info() { return "https://github.com/SpencerPark/IJava";}
Map<String, String> options(String compilerOptions, long timeout) {
return Map.of(
"IJAVA_COMPILER_OPTS",compilerOptions
);
}
},
JJAVA {
String shortName() { return "JJava"; }
String ga() { return "org.dflib.jjava:jjava-launcher"; }
String v() { return "1.0-a4"; }
String info() { return "https://github.com/dflib/jjava";}
Map<String, String> options(String compilerOptions, long timeout) {
return Map.of();
}
List<String> arguments() { return List.of(
"%{deps:org.dflib.jjava:jjava:" + v() + "@fatjar}", CONNECTION_FILE_MARKER); }
@Override
List<String> jvmArguments() {
return List.of();
}
},
JBANG {
String info() { return "https://github.com/jbangdev/jbang-jupyter"; }
String shortName() { return "Experimental JBang"; }
String scriptRef() { return "jbang-jupyter@jbangdev"; }
String ga() { return null; }
String javaVersion() { return "17+"; }
URI logo() { return URI.create("https://raw.githubusercontent.com/jbangdev/jbang/refs/heads/main/images/jbang_icon.png"); }
List<String> arguments() { return List.of(CONNECTION_FILE_MARKER); }
List<String> jvmArguments() { return List.of("--add-opens", "jdk.jshell/jdk.jshell=ALL-UNNAMED"); }
},
RAPAIO {
String info() { return "https://github.com/padreati/rapaio-jupyter-kernel"; }
String shortName() { return "Rapaio"; }
String ga() { return "io.github.padreati:rapaio-jupyter-kernel"; }
String v() { return "2.2.0@fatjar"; }
String javaVersion() { return "22"; }
List<String> modules() { return List.of("java.base", "jdk.incubator.vector"); }
Map<String, String> options(String compilerOptions, long timeout) {
return Map.of(
"RJK_COMPILER_OPTIONS",compilerOptions,
// "RJK_INIT_SCRIPT", "",
"RJK_TIMEOUT_MILLIS", ""+timeout);
}
},
GANYMEDE {
String info() { return "https://github.com/allen-ball/ganymede"; }
String shortName() { return "Ganymede"; }
String ga() { return "dev.hcf.ganymede:ganymede"; }
String v() { return "2.1.2.20230910"; }
String javaVersion() { return "11"; }
List<String> arguments() { return List.of(
//from Ganymede Install.java
"-f", CONNECTION_FILE_MARKER); }
// List<String> modules() { return List.of("java.base", "jdk.incubator.vector"); }
},
/*
ALMOND {
String info() { return "https://almond.sh/"; }
String shortName() { return "Almond"; }
String language() { return "scala"; }
String ga() { return "sh.almond:launcher_3"; }
String v() { return "0.14.0-RC14"; }
String javaVersion() { return "21"; }
String mainClass() { return null; }
List<String> arguments() { return List.of(
"--connection-file", CONNECTION_FILE_MARKER); }
},
*/
/**
* requires jbang 0.133 to get https://github.com/jbangdev/jbang/issues/1703 that supports
* %{deps:gav}
*/
KOTLIN {
String language() { return "kotlin"; }
String shortName() { return "Kotlin"; }
String ga() { return "org.jetbrains.kotlinx:kotlin-jupyter-kernel-shadowed"; }
String v() { return "0.12.0-339"; }
String javaVersion() { return "11"; }
String mainClass() { return "org.jetbrains.kotlinx.jupyter.IkotlinKt"; }
/*List<String> dependencies() {
return List.of("org.jetbrains.kotlinx:kotlin-jupyter-lib:0.12.0-85");
}*/
List<String> arguments() { return List.of(
"-cp=%{deps:org.jetbrains.kotlinx:kotlin-jupyter-lib:" + v() + "}", CONNECTION_FILE_MARKER); }
};
String shortName() { return name().substring(0, 1).toUpperCase() + name().substring(1); }
abstract String ga();
String v() { return "RELEASE"; }
String scriptRef() { return ga() + ":" + v(); }
String javaVersion() { return "11+"; }
String mainClass() { return null; }
String language() { return LANGUAGE;}
List<String> dependencies() { return List.of(); }
List<String> modules() { return List.of(); }
URI logo() { return null; }
List<String> jvmArguments() { return List.of(
"--add-opens", "java.base/jdk.internal.misc=ALL-UNNAMED",
"--illegal-access=permit"
// "-Djava.awt.headless=true",
// "-Djdk.disableLastUsageTracking=true"
);}
List<String> arguments() { return List.of(CONNECTION_FILE_MARKER); }
Map<String, String> options(String compilerOptions, long timeout) {
return Map.of();
}
String info() { return null; }
public String displayName() {
// doing this so it becomes "Kotlin (j!)" rather than "Kotlin (Kotlin/j!)"
if(language().toLowerCase().equals(shortName().toLowerCase()))
return language() + " (j!)";
else {
return language() + " (" + shortName() + "/j!)";
}
}
}
@Parameters(defaultValue = "jjava", description = "The kernel to install. Possible values: ${COMPLETION-CANDIDATES}")
Kernels kernel;
@Option(names = "--verbose")
boolean verbose;
@Option(names = "--force", description = "Whether to overwrite existing kernel.")
boolean force;
@Option(names = "--name")
String name;
String name() {
return name==null? kernel.displayName():name;
}
@Option(names = "--jupyter-kernel-dir", description = "The name of directory to install the kernel to. Defaults to OS specific location.")
String jupyterKernelDir;
@Option(names = "--kernel-dir", description = "The name of directory to install the kernel to. Defaults to <kernelname>")
String kernelDir;
String kernelDir() {
return kernelDir==null?kernel.name().toLowerCase():kernelDir;
}
@Option(names = "--debug", description = "Debug options to pass to JBang, i.e --debug address=5000? --debug suspend=n")
Map<String, String> debug = Map.of();
@Option(names = "--script-ref", description = "Override script reference to use for the kernel. Defaults to the kernel's script reference.")
Optional<String> scriptRef;
@Option(names="--enable-preview", description = "Whether to use preview versions of Java")
boolean preview;
@Option(names="--assertions", description = "Should assertions be enabled", defaultValue = "true")
boolean assertions;
@Option(names="--parameters", description = "Should parameter info be available at runtime", defaultValue = "true")
boolean parameters;
@Option(names="--java", description = "Java version to use for kernel. '17' means Java 17 only, '17+' means Java 17 or higher.")
String java;
String java() {
return java==null?kernel.javaVersion():java;
}
@Option(names="--timeout", defaultValue = "-1", description = "Timeout in milliseconds for kernel execution")
long timeout;
@Option(names="--ipc", defaultValue = "false", description = "Whether to install a proxy kernel that can be used to run kernel with IPC")
boolean useIPC;
@Option(names="--compiler-options", defaultValue = "", description = "Compiler options to pass to the kernel")
String compilerOptions;
String compilerOptions() {
String defaultOptions = Objects.toString(compilerOptions, "");
if(preview) {
defaultOptions = defaultOptions + "--enable-preview --source " + java().replace("+", "");
}
/* if(parameters) {
defaultOptions = defaultOptions + " --parameters";
}*/
return defaultOptions;
}
private enum OSName {
LINUX,
WINDOWS,
MAC,
SOLARIS
}
private OSName findOSName() {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
return OSName.WINDOWS;
} else if (os.contains("mac")) {
return OSName.MAC;
} else if (os.contains("sunos")) {
return OSName.SOLARIS;
} else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
return OSName.LINUX;
}
return null;
}
private String getUserHome() {
String home = System.getProperty("user.home");
if (home != null && home.endsWith("/")) {
home = home.substring(0, home.length() - 1);
}
return home;
}
private List<String> getInstallationPaths(OSName os) {
List<String> paths;
switch (os) {
case LINUX:
case SOLARIS:
paths = Arrays.asList(
getUserHome() + "/.local/share/jupyter/kernels",
"/usr/local/share/jupyter/kernels",
"/usr/share/jupyter/kernels"
);
break;
case MAC:
paths = Arrays.asList(
getUserHome() + "/Library/Jupyter/kernels",
"/usr/local/share/jupyter/kernels",
"/usr/share/jupyter/kernels"
);
break;
case WINDOWS:
paths = Arrays.asList(
System.getenv("APPDATA") + "/jupyter/kernels",
System.getenv("PROGRAMDATA") + "/jupyter/kernels"
);
break;
default:
paths = new ArrayList<>();
}
return paths;
}
/**
* Looks for command in jbang bin dir and then in PATH
* @param cmd
* @return
*/
private Path findCommand(String cmd) {
if ("jbang".equals(cmd) && System.getenv("JBANG_LAUNCH_CMD") != null) {
return Path.of(System.getenv("JBANG_LAUNCH_CMD"));
}
Path command = null;
List<String> paths = new ArrayList<>();
paths.add(System.getProperty("user.home") + "/.jbang/bin");
paths.addAll(Arrays.asList(System.getenv("PATH").split(File.pathSeparator)));
for (String path : paths) {
if (os.equals(OSName.WINDOWS)) {
command = Path.of(path, cmd + ".cmd");
if (exists(command) && Files.isExecutable(command)) {
break;
}
command = Path.of(path, cmd + ".ps1");
if (exists(command) && Files.isExecutable(command)) {
break;
}
}
command = Path.of(path, cmd);
if (exists(command) && Files.isExecutable(command)) {
break;
}
command = null;
}
return command;
}
public static void main(String... args) {
int exitCode = new CommandLine(new installkernel()).setCaseInsensitiveEnumValuesAllowed(true).execute(args);
System.exit(exitCode);
}
private KernelJson generateProxyKernelJson(KernelJson kernelJson) {
String proxyApp = loadResource("ipc_proxy_kernel.py");
String pycmd;
Path pythonCommand = findCommand("python");
if (pythonCommand == null) {
pythonCommand = findCommand("python3");
}
if (pythonCommand == null) {
throw new IllegalStateException("Python executable not found in PATH. Please ensure it is available before installing kernel.");
}
pycmd = pythonCommand.toAbsolutePath().toString();
KernelJson proxyKernel = new KernelJson(
List.of(pycmd,
"{{KERNEL_DIR}}/ipc_proxy_kernel.py",
CONNECTION_FILE_MARKER,
"--kernel="+kernelJson.kernelDir),
name(),
kernel.language(),
INTERRUPT_MODE,
Map.of(),
kernelDir(),
Map.of(Path.of("ipc_proxy_kernel.py"), proxyApp));
return proxyKernel;
}
KernelJson generateJavaKernelJson(String postfix) {
Path command = findCommand("jbang");
if (command == null) {
throw new IllegalStateException("jbang executable not found. Please ensure it is available before running install kernel.");
}
var commandList = new ArrayList<String>();
commandList.add(command.toAbsolutePath().toString());
commandList.add("--java");
commandList.add(java());
debug.forEach((key, value) -> {
commandList.add("-d" + key + "=" + value);
});
if(preview)
commandList.add("--enable-preview");
if(assertions) {
commandList.add("--ea");
}
if(kernel.mainClass()!=null) {
commandList.add("-m");
commandList.add(kernel.mainClass());
}
if(kernel.modules().size()>0) {
commandList.add("-R--add-modules");
commandList.add("-R" + String.join(",", kernel.modules()));
}
commandList.add("-R-ea");
kernel.jvmArguments().forEach(jvmArg -> {
commandList.add("-R" + jvmArg);
});
commandList.add(scriptRef.orElse(kernel.scriptRef()));
commandList.addAll(kernel.arguments());
KernelJson json = new KernelJson(commandList,
name() + postfix,
kernel.language(),
INTERRUPT_MODE,
kernel.options(compilerOptions(), timeout),
kernelDir() + postfix,
Map.of());
return json;
}
static OSName os;
@Override
public Integer call() throws Exception {
os = findOSName();
if (os == null) {
throw new RuntimeException("Operating system is not recognized. Installation failed.");
}
String postfix = "";
if(useIPC || "ipc".equals(System.getenv("COLAB_JUPYTER_TRANSPORT"))) {
useIPC = true;
postfix = "-tcp";
}
List<String> installationPath = null;
if(jupyterKernelDir!=null) {
installationPath = List.of(jupyterKernelDir);
} else {
installationPath = getInstallationPaths(os);
}
verbose("Considering " + String.join(",", installationPath));
if(!exists(Paths.get(installationPath.get(0)))) {
System.out.println("Creating " + installationPath.get(0));
Files.createDirectories(Paths.get(installationPath.get(0)));
// throw new IllegalStateException("Jupyter Kernel path " + installationPath.get(0) + " does not exist. Please ensure it is available before trying to install a kernel.");
}
KernelJson json = generateJavaKernelJson(postfix);
if(writeKernel(installationPath.get(0), json)) {
if(useIPC) {
json = generateProxyKernelJson(json);
writeKernel(installationPath.get(0), json);
}
if(kernel.info()!=null) {
out.println("For more information on this specific kernel: " + kernel.info());
}
out.println("\nBrought to you by https://github.com/jupyter-java");
return 0;
} else {
return 1;
}
}
private boolean writeKernel(String installationPath, KernelJson original) throws JsonProcessingException {
ObjectMapper objectMapper = setupObjectMapper();
var fullKernelDir = Paths.get(installationPath, original.kernelDir).toAbsolutePath().toString();
var output = Paths.get(fullKernelDir, "kernel.json");
if(!force) {
if(exists(output)) {
out.println("A kernel already installed to " + output + ". Use --force to overwrite.");
return false;
}
}
final var json = new KernelJson(original.argv.stream().map(arg -> arg.replace("{{KERNEL_DIR}}", fullKernelDir)).collect(Collectors.toList()),
original.displayName,
original.language,
original.interruptMode,
original.env,
original.kernelDir,
original.resources);
String jsonString = objectMapper.writeValueAsString(json);
verbose(format("Writing: %s\nto %s", jsonString, output));
try {
if (!exists(output.getParent())) {
createDirectories(output.getParent());
}
write(output, jsonString.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
out.println(json.displayName + " kernel installed to " + output);
if(kernel.logo()!=null) {
try {
URL logoUrl = kernel.logo().toURL();
java.awt.image.BufferedImage originalImage = ImageIO.read(logoUrl);
// Scale to 32x32
BufferedImage image32 = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g32 = image32.createGraphics();
g32.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g32.drawImage(originalImage, 0, 0, 32, 32, null);
g32.dispose();
// Scale to 64x64
BufferedImage image64 = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
Graphics2D g64 = image64.createGraphics();
g64.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g64.drawImage(originalImage, 0, 0, 64, 64, null);
g64.dispose();
// Save scaled images
Path logo32Path = Paths.get(installationPath, json.kernelDir, "logo-32x32.png");
Path logo64Path = Paths.get(installationPath, json.kernelDir, "logo-64x64.png");
ImageIO.write(image32, "PNG", logo32Path.toFile());
ImageIO.write(image64, "PNG", logo64Path.toFile());
out.println("32x32 logo saved to " + output.getParent().relativize(logo32Path));
out.println("64x64 logosaved to " + output.getParent().relativize(logo64Path));
} catch (IOException e) {
System.err.println("Could not download and save logo files: " + e.getMessage());
}
}
json.resources.forEach((path, content) -> {
try {
Path resource = Paths.get(installationPath, json.kernelDir, path.toString());
System.out.println(format("Additional file: %s", resource));
if (!exists(resource.getParent())) {
createDirectories(resource.getParent());
}
write(resource, content.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
System.err.println("Could not write resource " + path + " to " + output);
}
});
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private ObjectMapper setupObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);
objectMapper.setDefaultPrettyPrinter(prettyPrinter);
return objectMapper;
}
void verbose(String msg) {
if(verbose) {
out.println(msg);
}
}
public static String loadResource(String resourcePath) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
return reader.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new IllegalStateException("Could not load resource: " + resourcePath, e);
}
}
public class KernelJson {
public final List<String> argv;
public final String displayName;
public final String language;
public final String interruptMode;
public final Map<String, String> env;
@JsonIgnore public final String kernelDir;
@JsonIgnore public final Map<Path, String> resources;
public KernelJson(List<String> argv, String displayName, String language, String interruptMode, Map<String, String> env, String kernelDir, Map<Path, String> resources) {
this.argv = argv;
this.displayName = displayName;
this.language = language;
this.interruptMode = interruptMode;
this.env = env;
this.kernelDir = kernelDir;
this.resources = resources;
}
}
}