Commit b6f6e1ad authored by Lückemeyer's avatar Lückemeyer
Browse files

added directory support for unittests and student submissions. kept legacy mode.

parent 8aabe7e6
Pipeline #9077 passed with stage
......@@ -18,6 +18,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jakarta.servlet.annotation.MultipartConfig;
/**
......@@ -63,10 +65,20 @@ public class TaskUpload {
case "text/plain":
LOG.debug("textfile uploaded, searching for dta config");
// find URI in config file
Matcher config = RegexUtil.findStudentConfig(taskFileRef.getInputStream());
String subDir="";
Matcher config = RegexUtil.extractConfig(taskFileRef.getInputStream(), Pattern.compile(RegexUtil.DTA_SUBMISSIONCONFIGREGEX));
if(config==null) {
config = RegexUtil.extractConfig(taskFileRef.getInputStream(), Pattern.compile(RegexUtil.SUBMISSIONCONFIGREGEX));
if(config==null)
{
throw new RuntimeException("couldn't find repo config for student submission clone");
}
}
else {
subDir=config.group(4);
}
LOG.debug("calling repo clone");
jGitUtil.cloneRepository(config, srcPath.toAbsolutePath().toString());
jGitUtil.cloneRepository(config, srcPath.toAbsolutePath().toString(), subDir);
break;
case "application/zip":
......
package de.hftstuttgart.dtabackend.rest.v1.unittest;
import de.hftstuttgart.dtabackend.utils.JGitUtil;
import de.hftstuttgart.dtabackend.utils.RegexUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.env.Environment;
......@@ -28,9 +30,6 @@ import java.util.regex.Pattern;
public class UnitTestUpload {
private static final Logger LOG = LogManager.getLogger(UnitTestUpload.class);
public final static String TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$";
public final static String SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)$";
private final JGitUtil jGitUtil;
private final String assignmentBasePath;
......@@ -55,48 +54,31 @@ public class UnitTestUpload {
) throws IOException {
LOG.info("received new assignment");
File file = Paths.get(
this.assignmentBasePath,
assignmentId + ".txt")
.toFile();
File file = Paths.get(this.assignmentBasePath, assignmentId + ".txt").toFile();
file.mkdirs();
// save assignment config
unitTestFileRef.transferTo(file);
LOG.debug(String.format("saved config file to: %s", file.getAbsolutePath()));
Pattern pattern = Pattern.compile(TESTCONFIGREGEX);
Matcher config = null;
LOG.debug("reading test configuration file");
// open saved config in a try-with
try (BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(file)))) {
String line;
// search for a URI while none is found and there are lines left
while (config == null && (line = br.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
LOG.debug(String.format("found dta test line: %s", line));
config = matcher;
}
}
} catch (IOException e) {
LOG.error("Error while reading repo config", e);
String subDir="";
Pattern pattern = Pattern.compile(RegexUtil.DTA_TESTCONFIGREGEX);
Matcher config = RegexUtil.extractConfig(new FileInputStream(file), pattern);
if (config == null) {
pattern=Pattern.compile(RegexUtil.TESTCONFIGREGEX);
config = RegexUtil.extractConfig(new FileInputStream(file), pattern);
if(config==null)
{
throw new RuntimeException("couldn't find repo config for unittest clone");
}
}
finally {
if (config == null) {
throw new RuntimeException("couldn't find repo config for unittest clone");
}
else {
subDir=config.group(4);
}
LOG.debug("calling test repo clone");
// cloning assignment repo to persistent space
jGitUtil.cloneRepository(
config,
Paths.get(this.assignmentBasePath, assignmentId).toAbsolutePath().toString());
jGitUtil.cloneRepository(config, Paths.get(this.assignmentBasePath, assignmentId).toAbsolutePath().toString(), subDir);
LOG.info(String.format("stored new assignment: %s", file.getAbsolutePath()));
}
......
......@@ -20,6 +20,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Component
public class ExecuteTestUtil {
......@@ -60,31 +61,34 @@ public class ExecuteTestUtil {
// clone stored test to tmpdir
LOG.debug("copying pre-downloaded unitttest repo");
FileUtil.copyFolder(
Paths.get(
assignmentBasePath,
assignmentId
),
testPath
);
Paths.get(assignmentBasePath, assignmentId),
testPath);
LOG.debug("copy test config");
Files.copy(
Paths.get(
assignmentBasePath,
assignmentId + ".txt"
),
Paths.get(
workDirectory.toAbsolutePath().toString(),
"config.txt"
)
);
Paths.get(assignmentBasePath, assignmentId + ".txt"),
Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt"));
Files.createDirectory(resultPath);
LOG.info("reading test config");
Matcher config = RegexUtil.findProfessorConfig(
new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()));
Matcher config = RegexUtil.extractConfig(
new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()), Pattern.compile(RegexUtil.DTA_TESTCONFIGREGEX));
String image="";
if(config==null)
{
config = RegexUtil.extractConfig(
new FileInputStream(Paths.get(workDirectory.toAbsolutePath().toString(), "config.txt").toFile()), Pattern.compile(RegexUtil.TESTCONFIGREGEX));
if(config==null)
{
throw new RuntimeException("couldn't find repo config for unittest image extraction");
}
image=config.group(4);
}
else
{
image=config.group(5);
}
// define the paths to mount as Binds from Host to the test-container
Path testPathHost = Paths.get(
testTmpPathHost.toAbsolutePath().toString(),
......@@ -104,7 +108,7 @@ public class ExecuteTestUtil {
// start test-container with professor given image and bind mounts for test, submission and result
dockerUtil.runContainer(
config.group(4),
image,
new Bind(testPathHost.toAbsolutePath().toString(), new Volume("/data/test")),
new Bind(srcPathHost.toAbsolutePath().toString(), new Volume("/data/src")),
new Bind(resultPathHost.toAbsolutePath().toString(), new Volume("/data/result"))
......
......@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
@Component
......@@ -19,7 +20,7 @@ public class JGitUtil {
public JGitUtil() {}
public void cloneRepository(Matcher config, String targetPath) {
public void cloneRepository(Matcher config, String targetPath, String subDir) {
LOG.debug(String.format("cloning repository: %s", config.group(1)));
File targetDirectory = new File(targetPath);
......@@ -28,46 +29,44 @@ public class JGitUtil {
FileSystemUtils.deleteRecursively(targetDirectory);
}
//create companion checkout dir "targetPath"+"_checkout"
File checkoutDirectory = new File(targetPath+"_checkout");
if (targetDirectory.exists()) {
LOG.debug("clone checkout directory existing yet, deleting now");
FileSystemUtils.deleteRecursively(checkoutDirectory);
File checkoutDirectory = targetDirectory;
//if an optional directory parameter was given
if(subDir!="")
{
//create companion checkout dir "targetPath"+"_checkout"
checkoutDirectory = new File(targetPath+"_checkout");
if (targetDirectory.exists()) {
LOG.debug("clone checkout directory existing yet, deleting now");
FileSystemUtils.deleteRecursively(checkoutDirectory);
}
}
try {
//check group(1) for possible directory
//if(!config.group(1).endsWith(".git"))
//cut off the directory part
//pos=instr(".git/")
//cloneURI=config.group(1).substr(1, pos+3)
//else
//cloneURI=config.group(1)
LOG.debug("preparing clone");
CloneCommand cloneCommand = Git.cloneRepository()
.setDirectory(checkoutDirectory)
//.setURI(cloneURI)
.setURI(config.group(1));
if (!config.group(2).equals("none") && !config.group(3).equals("none")) {
LOG.debug("setting credentials");
cloneCommand.setCredentialsProvider(
new UsernamePasswordCredentialsProvider(config.group(2), config.group(3)));
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(config.group(2), config.group(3)));
}
LOG.debug("cloning...");
cloneCommand.call()
.close();
//copy appropriate path from checkout directory to target directory
//if(!config.group(1).endsWith(".git"))
//copy checkout+config.group(1).substr(pos+4) to target directory
//else
//copy checkout directory to target directory directly
cloneCommand.call().close();
//if an optional directory parameter was given
if(subDir!="")
{
//copy appropriate path from checkout directory to target directory
FileSystemUtils.copyRecursively(targetDirectory, new File(checkoutDirectory+subDir));
}
}
catch (IOException e) {
LOG.error(String.format("Error while cloning from %s: could not copy to unit test dir", config.group(1)), e);
}
catch (GitAPIException e) {
LOG.error(String.format("Error while cloning from %s", config.group(1)), e);
LOG.error(String.format("Error while cloning from %s: could not read from Git", config.group(1)), e);
}
LOG.debug(String.format("cloned from %s to %s", config.group(1), targetDirectory));
......
......@@ -3,9 +3,9 @@ package de.hftstuttgart.dtabackend.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.hftstuttgart.dtabackend.rest.v1.unittest.UnitTestUpload;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
......@@ -14,6 +14,11 @@ import java.util.regex.Pattern;
public class RegexUtil {
public final static String DTA_TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)::(.*)$";
public final static String DTA_SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$";
public final static String TESTCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)::(.*)$";
public final static String SUBMISSIONCONFIGREGEX = "^dtt::(.*)::(.*|none)::(.*|none)$";
public enum ConfigType {
TEACHER,
STUDENT,
......@@ -33,11 +38,11 @@ public class RegexUtil {
Pattern pattern;
switch (configType) {
case TEACHER:
pattern = Pattern.compile(UnitTestUpload.TESTCONFIGREGEX);
pattern = Pattern.compile(RegexUtil.TESTCONFIGREGEX);
break;
case STUDENT:
pattern = Pattern.compile(UnitTestUpload.SUBMISSIONCONFIGREGEX);
pattern = Pattern.compile(RegexUtil.SUBMISSIONCONFIGREGEX);
break;
default:
......@@ -46,33 +51,31 @@ public class RegexUtil {
throw new RuntimeException(msg);
}
Matcher config = null;
Matcher config = extractConfig(is, pattern);
LOG.debug("reading config file");
// open received file in a try-with
try (BufferedReader br = new BufferedReader(
new InputStreamReader(
is))) {
return config;
}
public static Matcher extractConfig(InputStream configFileStream, Pattern pattern) {
LOG.debug("reading configuration file");
Matcher configItems=null;
// open saved config in a try-with
try (BufferedReader br = new BufferedReader(new InputStreamReader(configFileStream))) {
String line;
// as long as we haven't found a configuration and have lines left, search
while (config == null && (line = br.readLine()) != null) {
// search for a URI while none is found and there are lines left
while (configItems == null && (line = br.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
LOG.debug(String.format("found dta line: %s", line));
config = matcher;
LOG.debug(String.format("found valid config line: %s", line));
configItems = matcher;
}
}
} catch (IOException e) {
LOG.error("Error while reading repo config", e);
}
finally {
if (config == null) {
throw new RuntimeException("couldn't find repo config for clone");
}
}
return config;
}
return configItems;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment