Skip to content

Commit

Permalink
Manually pulling the systems shell magic
Browse files Browse the repository at this point in the history
  • Loading branch information
franceme authored Jan 21, 2024
1 parent a03ad77 commit 6b0455d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion docs/magics.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ The cell magic is designed to make it very simple to copy and paste from any REA
<artifactId>jupyter-jvm-basekernel</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
```
```

### exec

Allow user to execute system program in jupyter notebook.

###### Line magic

* **aliases**: `system`
* **arguments**:
* _varargs_ list of external program and its command line arguments to be executed
2 changes: 2 additions & 0 deletions src/main/java/io/github/spencerpark/ijava/JavaKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.spencerpark.ijava.execution.*;
import io.github.spencerpark.ijava.magics.ClasspathMagics;
import io.github.spencerpark.ijava.magics.ExecMagics;
import io.github.spencerpark.ijava.magics.MavenResolver;
import io.github.spencerpark.jupyter.kernel.BaseKernel;
import io.github.spencerpark.jupyter.kernel.LanguageInfo;
Expand Down Expand Up @@ -95,6 +96,7 @@ public JavaKernel() {
this.magics.registerMagics(this.mavenResolver);
this.magics.registerMagics(new ClasspathMagics(this::addToClasspath));
this.magics.registerMagics(new Load(List.of(".jsh", ".jshell", ".java", ".ijava"), this::eval));
this.magics.registerMagics(new ExecMagics());

this.languageInfo = new LanguageInfo.Builder("Java")
.version(Runtime.version().toString())
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/io/github/spencerpark/ijava/magics/ExecMagics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Spencer Park
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.spencerpark.ijava.magics;

import io.github.spencerpark.jupyter.kernel.magic.registry.LineMagic;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

public class ExecMagics {

@LineMagic(aliases = "system")
public void exec(List<String> args) throws Exception {
ProcessBuilder pb = new ProcessBuilder();
pb.command(args);
pb.redirectErrorStream(true);
Process process = pb.start();

try (Scanner scanner = new Scanner(process.getInputStream(), StandardCharsets.UTF_8)) {
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
}
}
}

0 comments on commit 6b0455d

Please sign in to comment.