Skip to content
Snippets Groups Projects
Commit 350934db authored by Joel Cavat's avatar Joel Cavat
Browse files

Use better arguments for process exec

parent a92c9fc6
No related branches found
No related tags found
No related merge requests found
import io.javalin.Javalin; import io.javalin.Javalin;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.*;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import static java.lang.System.out; import static java.lang.System.out;
interface ExecutableCode {
Map<String, Serializable> execute();
}
interface DockerExecutableCode extends ExecutableCode {
String command();
Map<String, String> filesToExecute();
default void writeFile(String filename, String content) {
File file = new File(filename);
try (PrintWriter writer = new PrintWriter( new FileWriter(file, true) )) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
default void writeFiles(Map<String, String> files) {
files.forEach( this::writeFile );
}
default void deleteFiles(Map<String, String> files) {
files.keySet().forEach( filename -> new File(filename).delete() );
}
default Map<String, Serializable> executeFiles() {
Process cmdProc;
long elapsed = 0;
try {
long start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec( command() );
cmdProc.waitFor();
elapsed = System.currentTimeMillis() - start;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return Map.of("error", e.toString(), "exit", 1, "elapsed", elapsed);
}
String in = new BufferedReader( new InputStreamReader( cmdProc.getInputStream() )).lines().collect(Collectors.joining());
String err = new BufferedReader( new InputStreamReader( cmdProc.getErrorStream() )).lines().collect(Collectors.joining());
return Map.of("exit", err.isEmpty(), "output", in, "error", err, "time", elapsed);
}
default Map<String, Serializable> execute() {
writeFiles( filesToExecute() );
Map<String, Serializable> res = executeFiles();
deleteFiles( filesToExecute() );
return res;
}
}
class JavaDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public JavaDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String command() {
return "docker run --rm --mount type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/ freakency/java:1.0 ./java_test.sh";
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
class PythonDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public PythonDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String command() {
return "docker run --rm --mount type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/ freakency/python:3.0 python assert.py";
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
public class App { public class App {
public static void main(String[] args) { public static void main(String[] args) {
...@@ -97,10 +18,17 @@ public class App { ...@@ -97,10 +18,17 @@ public class App {
String userCode = res.getString("stream"); String userCode = res.getString("stream");
String assertion = res.getString("assert"); String assertion = res.getString("assert");
ExecutableCode ec;
ExecutableCode ec = language.equals("java") ? switch( language ) {
new JavaDockerCode(Map.of("share_docker_file/Kata.java", userCode, "share_docker_file/Main.java", assertion)) : case "java":
new PythonDockerCode(Map.of("share_docker_file/sample.py", userCode, "share_docker_file/assert.py", assertion)); ec = new JavaDockerCode(Map.of("share_docker_file/Kata.java", userCode, "share_docker_file/Main.java", assertion));
break;
case "python":
ec = new PythonDockerCode(Map.of("share_docker_file/sample.py", userCode, "share_docker_file/assert.py", assertion));
break;
default:
throw new IllegalArgumentException("Language unkown");
}
out.println(language); out.println(language);
out.println(userCode); out.println(userCode);
......
import java.io.Serializable;
import java.util.Map;
public interface CompilationService {
Map<String, Serializable> executeKata(String language, String userCode, String assertion);
}
import org.json.JSONObject;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class DockerCompilation implements CompilationService {
private Map<String, Map<String, String>> filenameDic = new HashMap<>();
DockerCompilation() {
String userDir = System.getProperty("user.dir");
Map<String, String> python = new HashMap<>();
python.put("filename", "share_docker_file/sample.py");
python.put("test", "share_docker_file/assert.py");
python.put("cmd", "docker run --rm --mount type=bind,source=" + userDir + "/share_docker_file,dst=/env/ freakency/python:3.0 python assert.py");
Map<String, String> java = new HashMap<>();
java.put("filename", "share_docker_file/kata.java");
java.put("test", "share_docker_file/Main.java");
java.put("cmd", "docker run --rm --mount type=bind,source=" + userDir + "/share_docker_file,dst=/env/ freakency/java:1.0 ./java_test.sh");
this.filenameDic.put("python", python);
this.filenameDic.put("java", java);
}
public Map<String, Serializable> executeKata(String language, String userCode, String assertion) {
String line;
String output = "";
String error = "";
Map<String, String> languages = this.filenameDic.get(language);
try (PrintWriter filenameWriter = new PrintWriter(languages.get("filename"), "UTF-8"); PrintWriter filenameTestWriter = new PrintWriter(languages.get("test"), "UTF-8")) {
filenameWriter.println(userCode);
filenameTestWriter.println(assertion);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
}
long start, elapsed = 0;
Process cmdProc = null;
try {
start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec(languages.get("cmd"));
cmdProc.waitFor();
elapsed = System.currentTimeMillis() - start;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// Create Array lists that will contain output and error values of the executed program
/**************************/
try (BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(cmdProc.getInputStream()));
BufferedReader stderrReader = new BufferedReader(new InputStreamReader(cmdProc.getErrorStream()))) {
while ((line = stdoutReader.readLine()) != null)
output += line + "\n";
while ((line = stderrReader.readLine()) != null)
error += line + "\n";
} catch (IOException e) {
e.printStackTrace();
}
/**************************/
Map<String, Serializable> json = new HashMap<>();
//exit value of the ran program
if (error.equals(""))
json.put("exit", 0);
else
json.put("exit", 1);
json.put("output", output);
json.put("error", error);
json.put("time", elapsed);
// For delete purpose, create a File object also targeting sample.py
File file = new File(languages.get("filename"));
file.delete();
file = new File(languages.get("test"));
file.delete();
return json;
}
}
import java.io.*;
import java.util.Map;
import java.util.stream.Collectors;
interface DockerExecutableCode extends ExecutableCode {
String[] command();
Map<String, String> filesToExecute();
default Map<String, Serializable> execute() {
writeFiles( filesToExecute() );
Map<String, Serializable> res = executeFiles();
deleteFiles( filesToExecute() );
return res;
}
default void writeFile(String filename, String content) {
File file = new File(filename);
try (PrintWriter writer = new PrintWriter( new FileWriter(file, false) )) {
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
}
}
default void writeFiles(Map<String, String> files) {
files.forEach( this::writeFile );
}
default void deleteFiles(Map<String, String> files) {
files.keySet().forEach( filename -> new File(filename).delete() );
}
default Map<String, Serializable> executeFiles() {
Process cmdProc;
long elapsed = 0;
try {
long start = System.currentTimeMillis();
cmdProc = Runtime.getRuntime().exec( command() );
cmdProc.waitFor();
elapsed = System.currentTimeMillis() - start;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return Map.of("error", e.toString(), "exit", 1, "elapsed", elapsed);
}
String in = new BufferedReader( new InputStreamReader( cmdProc.getInputStream() )).lines().collect(Collectors.joining());
String err = new BufferedReader( new InputStreamReader( cmdProc.getErrorStream() )).lines().collect(Collectors.joining());
return Map.of("exit", err.isEmpty() ? 0 : 1, "output", in, "error", err, "time", elapsed);
}
}
import java.io.Serializable;
import java.util.Map;
public interface ExecutableCode {
Map<String, Serializable> execute();
/*
enum Language {
Java, Python3
}
static ExecutableCode of(Language language, Map<String, String> filesWithContent ) {
switch (language) {
case Java:
return new JavaDockerCode(filesWithContent);
case Python3:
return new PythonDockerCode(filesWithContent);
default:
throw new IllegalArgumentException("Need a implementation for this language");
}
}
*/
}
import java.util.Map;
final class JavaDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public JavaDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String[] command() {
return new String[]{"docker", "run", "--rm", "--mount", "type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/", "freakency/java:1.0", "/bin/bash", "-c", "./java_test.sh"};
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
import java.util.Map;
final class PythonDockerCode implements DockerExecutableCode {
private Map<String, String> filesWithContent;
public PythonDockerCode(Map<String, String> filesWithContent) {
this.filesWithContent = filesWithContent;
}
public String[] command() {
return new String[]{"docker", "run", "--rm", "--mount", "type=bind,source=" + System.getProperty("user.dir") + "/share_docker_file,dst=/env/", "freakency/python:3.0", "/bin/bash", "-c", "python sample.py && python assert.py"};
}
public Map<String, String> filesToExecute() {
return this.filesWithContent;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment