Skip to content
Snippets Groups Projects
Commit 2876b1dc authored by dgelessus's avatar dgelessus
Browse files

Add options to install the kernel spec into the user directory

This can be used when the sys.prefix is not writable, for example when
using the system Python without a virtual environment.
parent 7e88da14
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ This is a [Jupyter](https://jupyter.org/) kernel for the [ProB animator and mode ...@@ -21,6 +21,7 @@ This is a [Jupyter](https://jupyter.org/) kernel for the [ProB animator and mode
1. Download the latest version of the kernel [here](https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel/blob/master/releases/prob2-jupyter-kernel-1.0.0-all.jar). 1. Download the latest version of the kernel [here](https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel/blob/master/releases/prob2-jupyter-kernel-1.0.0-all.jar).
2. If Jupyter is installed in a virtual environment, activate it. 2. If Jupyter is installed in a virtual environment, activate it.
3. Run `java -jar <jarfile> install` to install the kernel. (`<jarfile>` is the name of the jar file that you just downloaded.) 3. Run `java -jar <jarfile> install` to install the kernel. (`<jarfile>` is the name of the jar file that you just downloaded.)
* If you get a permission error when installing the kernel spec, add the option `--user` after `install`. This will install the kernel spec into your user home instead of the Python install directory (which may not be writable).
* This assumes that Jupyter can be called using the command `jupyter`. To use a different command in place of `jupyter`, pass it as an argument after `install`, e. g. `java -jar <jarfile> install /path/to/jupyter`. * This assumes that Jupyter can be called using the command `jupyter`. To use a different command in place of `jupyter`, pass it as an argument after `install`, e. g. `java -jar <jarfile> install /path/to/jupyter`.
* To use a different ProB home directory than the default, pass `-Dprob.home=/path/to/prob/home` before the `-jar` option. (The path must be absolute.) * To use a different ProB home directory than the default, pass `-Dprob.home=/path/to/prob/home` before the `-jar` option. (The path must be absolute.)
4. (Optional) The jar file can be deleted after installation. 4. (Optional) The jar file can be deleted after installation.
...@@ -32,6 +33,7 @@ To update the kernel, follow the same instructions with the new jar file. ...@@ -32,6 +33,7 @@ To update the kernel, follow the same instructions with the new jar file.
1. Clone this repository (`git clone https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel.git`) or download an archive from [the repository page](https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel). 1. Clone this repository (`git clone https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel.git`) or download an archive from [the repository page](https://gitlab.cs.uni-duesseldorf.de/dgelessus/prob2-jupyter-kernel).
2. If Jupyter is installed in a virtual environment, activate it. 2. If Jupyter is installed in a virtual environment, activate it.
3. In the root directory of the repository, run `./gradlew installKernelSpec`. 3. In the root directory of the repository, run `./gradlew installKernelSpec`.
* If you get a permission error when installing the kernel spec, pass `-PkernelspecUserInstall=true` to the `./gradlew` command. This will install the kernel spec into your user home instead of the Python install directory (which may not be writable).
* This assumes that Jupyter can be called using the command `jupyter`. To use a different command in place of `jupyter`, you can pass `-PjupyterCommand=/path/to/jupyter` to the `./gradlew` command. * This assumes that Jupyter can be called using the command `jupyter`. To use a different command in place of `jupyter`, you can pass `-PjupyterCommand=/path/to/jupyter` to the `./gradlew` command.
* To use a different ProB home directory than the default, pass `-PprobHome=/path/to/prob/home` to the `./gradlew` command. (The path must be absolute.) * To use a different ProB home directory than the default, pass `-PprobHome=/path/to/prob/home` to the `./gradlew` command. (The path must be absolute.)
......
...@@ -79,5 +79,11 @@ task createKernelSpec(type: JavaExec) { ...@@ -79,5 +79,11 @@ task createKernelSpec(type: JavaExec) {
task installKernelSpec(type: Exec) { task installKernelSpec(type: Exec) {
dependsOn = [createKernelSpec] dependsOn = [createKernelSpec]
executable = project.hasProperty("jupyterCommand") ? project.jupyterCommand : "jupyter" executable = project.hasProperty("jupyterCommand") ? project.jupyterCommand : "jupyter"
args = ["kernelspec", "install", "--sys-prefix", "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()] final installFlag
if (project.hasProperty("kernelspecUserInstall") && project.kernelspecUserInstall == "true") {
installFlag = "--user"
} else {
installFlag = "--sys-prefix"
}
args = ["kernelspec", "install", installFlag, "--name=prob2", KERNEL_SPEC_OUTPUT_PATH.toString()]
} }
...@@ -124,11 +124,11 @@ public final class Main { ...@@ -124,11 +124,11 @@ public final class Main {
System.out.println("Kernel spec created"); System.out.println("Kernel spec created");
} }
private static void installKernelSpec(final String jupyterCommand, final Path kernelSpecDir) { private static void installKernelSpec(final String jupyterCommand, final boolean userInstall, final Path kernelSpecDir) {
System.out.println("Installing kernel spec..."); System.out.println("Installing kernel spec...");
System.out.println("Jupyter command: " + jupyterCommand); System.out.println("Jupyter command: " + jupyterCommand);
System.out.println("Kernel spec directory: " + kernelSpecDir); System.out.println("Kernel spec directory: " + kernelSpecDir);
final ProcessBuilder pb = new ProcessBuilder(jupyterCommand, "kernelspec", "install", "--sys-prefix", "--name=prob2", kernelSpecDir.toString()); final ProcessBuilder pb = new ProcessBuilder(jupyterCommand, "kernelspec", "install", userInstall ? "--user" : "--sys-prefix", "--name=prob2", kernelSpecDir.toString());
pb.redirectInput(ProcessBuilder.Redirect.INHERIT); pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT); pb.redirectError(ProcessBuilder.Redirect.INHERIT);
...@@ -156,14 +156,14 @@ public final class Main { ...@@ -156,14 +156,14 @@ public final class Main {
System.out.println("Kernel spec installed"); System.out.println("Kernel spec installed");
} }
private static void install(final String jupyterCommand) { private static void install(final String jupyterCommand, final boolean userInstall) {
final Path jarPath = getJarPath(); final Path jarPath = getJarPath();
final Path destPath = getDestPath(jarPath); final Path destPath = getDestPath(jarPath);
copyJar(jarPath, destPath); copyJar(jarPath, destPath);
try { try {
final Path kernelSpecDir = Files.createTempDirectory("prob2kernelspec"); final Path kernelSpecDir = Files.createTempDirectory("prob2kernelspec");
createKernelSpec(destPath, kernelSpecDir); createKernelSpec(destPath, kernelSpecDir);
installKernelSpec(jupyterCommand, kernelSpecDir); installKernelSpec(jupyterCommand, userInstall, kernelSpecDir);
try (final Stream<Path> contents = Files.list(kernelSpecDir)) { try (final Stream<Path> contents = Files.list(kernelSpecDir)) {
contents.forEach(path -> { contents.forEach(path -> {
try { try {
...@@ -199,9 +199,10 @@ public final class Main { ...@@ -199,9 +199,10 @@ public final class Main {
public static void main(final String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException { public static void main(final String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
if (args.length < 1 || args.length == 1 && "--help".equals(args[0])) { if (args.length < 1 || args.length == 1 && "--help".equals(args[0])) {
System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install [JUPYTER] | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]"); System.err.println("Usage: java -jar prob2-jupyter-kernel-all.jar [--help | install [--user] [JUPYTER] | createKernelSpec KERNELSPECDIR | run CONNECTIONFILE]");
System.err.println("--help: Prints this information."); System.err.println("--help: Prints this information.");
System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in Jupyter."); System.err.println("install: Copies the kernel into the ProB home directory, and installs the kernel in Jupyter.");
System.err.println("\tBy default the kernel spec is installed into the sys.prefix of Jupyter's Python installation. If the sys.prefix is not writable (for example when using the system Python installation instead of a virtual environment), the --user flag can be used to install the kernel only for the current user.");
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("\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("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("\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.");
...@@ -211,12 +212,24 @@ public final class Main { ...@@ -211,12 +212,24 @@ public final class Main {
} }
switch (args[0]) { switch (args[0]) {
case "install": case "install":
final boolean userInstall = args.length > 1 && "--user".equals(args[1]);
final String jupyterCommand;
if (userInstall) {
if (args.length > 3) {
System.err.println("install --user expects at most one argument, not " + (args.length-2));
System.err.println("Use --help for more info.");
throw die(2);
}
jupyterCommand = args.length > 2 ? args[2] : "jupyter";
} else {
if (args.length > 2) { if (args.length > 2) {
System.err.println("install expects at most one argument, not " + (args.length-1)); System.err.println("install expects at most one argument, not " + (args.length-1));
System.err.println("Use --help for more info."); System.err.println("Use --help for more info.");
throw die(2); throw die(2);
} }
install(args.length > 1 ? args[1] : "jupyter"); jupyterCommand = args.length > 1 ? args[1] : "jupyter";
}
install(jupyterCommand, userInstall);
break; break;
case "createKernelSpec": case "createKernelSpec":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment