Skip to content

Commit

Permalink
worky terminal manager
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Mar 2, 2024
1 parent 772ed38 commit 3b63f25
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/myrobotlab/process/NodeTerminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.myrobotlab.process;

import java.io.IOException;

import org.myrobotlab.service.TerminalManager;

public class NodeTerminal extends Terminal {

public NodeTerminal(TerminalManager service, String name) throws IOException {
super(service, name);
}

}
108 changes: 108 additions & 0 deletions src/main/java/org/myrobotlab/process/PythonTerminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.myrobotlab.process;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.myrobotlab.framework.Service;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.service.TerminalManager;
import org.myrobotlab.service.Runtime;
import org.slf4j.Logger;

public class PythonTerminal extends Terminal {

/**
* name of the venv
*/
protected String venvName = "venv";

public final static Logger log = LoggerFactory.getLogger(PythonTerminal.class);

public PythonTerminal(TerminalManager service, String name) throws IOException {
super(service, name);
}

@Override
public String getVersion() {
try {
processCommand(getScriptCmd("python --version"));
Service.sleep(300);
return outputCapture.toString();
} catch (Exception e) {
service.error(e);
}
return null;
}

public void installPipPackages(List<String> packages) {
String packagesString = String.join(" ", packages);
String command = "pip install " + packagesString;
processCommand(command + "\n");
}

public void installPipPackage(String string) {
// TODO Auto-generated method stub

}

public void activateVirtualEnv() {
if (isWindows()) {
processCommand(venvName + "\\Scripts\\activate");
} else {
// source is "bash"
// processCommand("source " + venvName + "/bin/activate");
// the posix way
processCommand(". " + venvName + "/bin/activate");
}
Service.sleep(300);
}

public void installVirtualEnv() {
installVirtualEnv(venvName);
}

public void installVirtualEnv(String venvName) {
this.venvName = venvName;
// processCommand(getScriptCmd("python -m venv " + venvName));
processCommand("python -m venv " + venvName);
Service.sleep(300);
}

public static void main(String[] args) {
try {
TerminalManager processor = (TerminalManager) Runtime.start("processor", "ManagedProcess");
PythonTerminal shell = new PythonTerminal(processor, "python");
// shell.setWorkspace(".." + File.separator + "webcam");
shell.start(".." + File.separator + "webcam");
shell.installVirtualEnv();
shell.activateVirtualEnv();
// shell.installPipPackage("");
shell.installPipPackages(Arrays.asList("aiortc aiohttp"));

shell.processCommand("python webcam.py");
System.out.println(shell.getPids().toString());

shell.terminate();

// Example usage
String directory = "../webcam";
String venvName = "venv";
String packageName = "package_name";
String pythonScript = "your_script.py";

// shell.setupAndRunPythonEnvironment(directory, venvName, packageName,
// pythonScript);

// Wait for the completion or handle accordingly
// shell.waitForCompletion();

// Terminate the shell if necessary
// shell.terminate();

} catch (IOException e) {
e.printStackTrace();
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/myrobotlab/process/Ros2Terminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.myrobotlab.process;

import java.io.IOException;

import org.myrobotlab.service.TerminalManager;

public class Ros2Terminal extends Terminal {

public Ros2Terminal(TerminalManager service, String name) throws IOException {
super(service, name);
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/myrobotlab/process/RosTerminal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.myrobotlab.process;

import java.io.IOException;

import org.myrobotlab.service.TerminalManager;

public class RosTerminal extends Terminal {

public RosTerminal(TerminalManager service, String name) throws IOException {
super(service, name);
}

}
Loading

0 comments on commit 3b63f25

Please sign in to comment.