From 343b37af6cc6b5cddd501ee336972f6bd67ab51a Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Thu, 9 Aug 2018 11:29:14 +0200
Subject: [PATCH] Use jupyter instead of python3 -m jupyter to install the
 kernel spec

The standard Jupyter command is always called "jupyter" regardless of
Python version, so this way the default is more likely to work. If the
user's Jupyter command is different, they will more likely know its
name than what the underlying Python interpreter is.
---
 build.gradle                             |  4 ++--
 src/main/java/de/prob2/jupyter/Main.java | 22 +++++++++++-----------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/build.gradle b/build.gradle
index 1e1966c..54c56ee 100644
--- a/build.gradle
+++ b/build.gradle
@@ -76,6 +76,6 @@ task createKernelSpec(type: JavaExec) {
 
 task installKernelSpec(type: Exec) {
 	dependsOn = [createKernelSpec]
-	executable = project.hasProperty("pythonInterpreter") ? project.pythonInterpreter : "python3"
-	args = ["-m", "jupyter", "kernelspec", "install", "--sys-prefix", "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()]
+	executable = project.hasProperty("jupyterCommand") ? project.jupyterCommand : "jupyter"
+	args = ["kernelspec", "install", "--sys-prefix", "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()]
 }
diff --git a/src/main/java/de/prob2/jupyter/Main.java b/src/main/java/de/prob2/jupyter/Main.java
index c0aa311..fcdc13c 100644
--- a/src/main/java/de/prob2/jupyter/Main.java
+++ b/src/main/java/de/prob2/jupyter/Main.java
@@ -124,11 +124,11 @@ public final class Main {
 		System.out.println("Kernel spec created");
 	}
 	
-	private static void installKernelSpec(final Path pythonInterpreter, final Path kernelSpecDir) {
+	private static void installKernelSpec(final String jupyterCommand, final Path kernelSpecDir) {
 		System.out.println("Installing kernel spec...");
-		System.out.println("Python interpreter: " + pythonInterpreter);
+		System.out.println("Jupyter command: " + jupyterCommand);
 		System.out.println("Kernel spec directory: " + kernelSpecDir);
-		final ProcessBuilder pb = new ProcessBuilder(pythonInterpreter.toString(), "-m", "jupyter", "kernelspec", "install", "--sys-prefix", "--name=prob2", kernelSpecDir.toString());
+		final ProcessBuilder pb = new ProcessBuilder(jupyterCommand, "kernelspec", "install", "--sys-prefix", "--name=prob2", kernelSpecDir.toString());
 		pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
 		pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
 		pb.redirectError(ProcessBuilder.Redirect.INHERIT);
@@ -156,14 +156,14 @@ public final class Main {
 		System.out.println("Kernel spec installed");
 	}
 	
-	private static void install(final Path pythonInterpreter) {
+	private static void install(final String jupyterCommand) {
 		final Path jarPath = getJarPath();
 		final Path destPath = getDestPath(jarPath);
 		copyJar(jarPath, destPath);
 		try {
 			final Path kernelSpecDir = Files.createTempDirectory("prob2kernelspec");
 			createKernelSpec(destPath, kernelSpecDir);
-			installKernelSpec(pythonInterpreter, kernelSpecDir);
+			installKernelSpec(jupyterCommand, kernelSpecDir);
 			try (final Stream<Path> contents = Files.list(kernelSpecDir)) {
 				contents.forEach(path -> {
 					try {
@@ -199,10 +199,10 @@ public final class Main {
 	
 	public static void main(final String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
 		if (args.length < 1 || args.length == 1 && "--help".equals(args[0])) {
-			System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install PYTHONINTERPRETER | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]");
+			System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install [JUPYTER] | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]");
 			System.err.println("--help: Prints this information.");
-			System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in the given Python interpreter.");
-			System.err.println("\tIf you're not sure which Python interpreter is used by your Jupyter installation, open a Python notebook and run \"import sys; print(sys.executable)\".");
+			System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in Jupyter.");
+			System.err.println("\tBy default the command \"jupyter\" is used to install the kernel. If the kernel should be installed in a different Jupyter installation, a different Jupyter command can be passed as an argument to the install command.");
 			System.err.println("createKernelSpec: Creates a Jupyter kernel spec for this jar file at the given location.");
 			System.err.println("\tThis option is for advanced users or developers, who don't want the jar file to be copied, or who want to install the kernel spec manually.");
 			System.err.println("run: Runs the kernel using the given connection file.");
@@ -211,12 +211,12 @@ public final class Main {
 		}
 		switch (args[0]) {
 			case "install":
-				if (args.length != 2) {
-					System.err.println("install expects exactly one argument, not " + (args.length-1));
+				if (args.length > 2) {
+					System.err.println("install expects at most one argument, not " + (args.length-1));
 					System.err.println("Use --help for more info.");
 					throw die(2);
 				}
-				install(Paths.get(args[1]));
+				install(args.length > 1 ? args[1] : "jupyter");
 				break;
 			
 			case "createKernelSpec":
-- 
GitLab