Skip to content
Snippets Groups Projects
Commit d7be924a authored by Daniel's avatar Daniel
Browse files

WIP: server-side scripting gets a home directory.

parent 415bced4
No related branches found
No related tags found
No related merge requests found
...@@ -56,6 +56,7 @@ public class ServerSideScriptingCaller { ...@@ -56,6 +56,7 @@ public class ServerSideScriptingCaller {
private ScriptingUtils utils; private ScriptingUtils utils;
private List<FileProperties> files; private List<FileProperties> files;
private File workingDir; private File workingDir;
private File tmpHome;
private File sharedDir; private File sharedDir;
private Object authToken; private Object authToken;
private Map<String, String> env; private Map<String, String> env;
...@@ -112,6 +113,7 @@ public class ServerSideScriptingCaller { ...@@ -112,6 +113,7 @@ public class ServerSideScriptingCaller {
this.authToken = authToken; this.authToken = authToken;
this.env = env; this.env = env;
this.workingDir = workingDir; this.workingDir = workingDir;
this.tmpHome = new File(getTmpWorkingDir(), "temphome");
this.stdOutFile = utils.getStdOutFile(workingDir); this.stdOutFile = utils.getStdOutFile(workingDir);
this.stdErrFile = utils.getStdErrFile(workingDir); this.stdErrFile = utils.getStdErrFile(workingDir);
} }
...@@ -124,6 +126,7 @@ public class ServerSideScriptingCaller { ...@@ -124,6 +126,7 @@ public class ServerSideScriptingCaller {
createWorkingDir(); createWorkingDir();
putFilesInWorkingDir(files); putFilesInWorkingDir(files);
createSharedDir(); createSharedDir();
createTmpHomeDir();
updateEnvironment(); updateEnvironment();
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -173,27 +176,44 @@ public class ServerSideScriptingCaller { ...@@ -173,27 +176,44 @@ public class ServerSideScriptingCaller {
} }
// create working dir // create working dir
getTmpWorkingDir().mkdirs(); if (!getTmpWorkingDir().mkdirs()) {
throw new Exception("Creating the temporary working dir failed.");
} }
/** Creates a temporary directory for shareing by the script and sets `sharedDir` accordingly. */
void createSharedDir() throws Exception {
sharedDir = new File(FileSystem.assertDir(null));
} }
/** Sets environment variables for the script, according to the current state of the caller. */ /**
void updateEnvironment() throws Exception { * Create a writable "home like" directory, needed for some Python libs, e.g. caosdb-pylib.
env.put("SHARED_DIR", sharedDir.toString()); *
// Writable "home like" directory, needed for some Python libs, e.g. caosdb-pylib.) * <p>The temporary working directory must exist when this method is called.
File tmpHome = new File(getTmpWorkingDir(), "temphome"); */
if (!tmpHome.mkdir()) { void createTmpHomeDir() throws Exception {
if (!getTmpWorkingDir().exists()) {
throw new Exception(
"The temp working dir ("
+ getTmpWorkingDir().toString()
+ ") must exist before the home dir can be created.");
}
if (getTmpHomeDir().exists()) {
throw new Exception("The home directory must be non-existing when the caller is invoked.");
}
if (!this.tmpHome.mkdir()) {
throw new Exception("Creating the temporary home dir failed."); throw new Exception("Creating the temporary home dir failed.");
} }
try { try {
Files.copy(Paths.get("~/.pycaosdb.ini"), tmpHome.toPath()); Files.copy(Paths.get("~/.pycaosdb.ini"), this.tmpHome.toPath());
} catch (IOException e) { } catch (IOException e) {
System.out.println("No pycaosdb.info found"); System.out.println("No pycaosdb.info found");
} }
}
/** Creates a temporary directory for sharing by the script and sets `sharedDir` accordingly. */
void createSharedDir() throws Exception {
sharedDir = new File(FileSystem.assertDir(null));
}
/** Sets environment variables for the script, according to the current state of the caller. */
void updateEnvironment() throws Exception {
env.put("SHARED_DIR", sharedDir.toString());
env.put("HOME", tmpHome.toString()); env.put("HOME", tmpHome.toString());
} }
...@@ -276,6 +296,10 @@ public class ServerSideScriptingCaller { ...@@ -276,6 +296,10 @@ public class ServerSideScriptingCaller {
return this.workingDir; return this.workingDir;
} }
File getTmpHomeDir() {
return this.tmpHome;
}
File getSharedDir() { File getSharedDir() {
return this.sharedDir; return this.sharedDir;
} }
......
...@@ -492,6 +492,9 @@ public class TestServerSideScriptingCaller extends CaosDBTestClass { ...@@ -492,6 +492,9 @@ public class TestServerSideScriptingCaller extends CaosDBTestClass {
public void putFilesInWorkingDir(final Collection<FileProperties> files) public void putFilesInWorkingDir(final Collection<FileProperties> files)
throws FileNotFoundException, CaosDBException {} throws FileNotFoundException, CaosDBException {}
@Override
public void createTmpHomeDir() throws Exception {}
@Override @Override
public int callScript() throws IOException { public int callScript() throws IOException {
throw new IOException(); throw new IOException();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment