Skip to content

Commit

Permalink
Improve functionality for preparing tasks to be sent to sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
OAarne authored and nygrenh committed Dec 18, 2017
1 parent 29b9ab2 commit 0abbdd2
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 67 deletions.
110 changes: 68 additions & 42 deletions tmc-langs-cli/src/main/java/fi/helsinki/cs/tmc/langs/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

/*
* TODO: unstaticify this class
Expand All @@ -44,7 +45,10 @@ public final class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);

private static final String EXERCISE_PATH = "exercisePath";
private static final String SUBMISSION_PATH = "submissionPath";
private static final String OUTPUT_PATH = "outputPath";
private static final String TMC_RUN_PATH = "tmcRunPath";
private static final String TMC_LANGS_PATH = "tmcLangsPath";
private static final String LOCALE = "locale";

@VisibleForTesting static Map<String, String> argsMap = Maps.newHashMap();
Expand All @@ -62,8 +66,11 @@ public final class Main {
+ " Prepare a presentable solution from the original.\n"
+ " prepare-stubs --exercisePath -- outputPath"
+ " Prepare a stub exercise from the original.\n"
+ " prepare-submission --clonePath --submissionPath --outputPath"
+ " Prepares from submission and solution project for which the tests"
// TODO: Not implemented yet
// + " prepare-submission --exercisePath --submissionPath --outputPath"
// + " Prepares from submission and solution project for which the tests.\n"
+ " prepare-sandbox-task --exercisePath --submissionPath --outputPath --tmcRunPath --tmcLangsPath"
+ " Creates a tarball that sandbox can consume.\n"
+ " can be run in sandbox\n"
+ " run-tests --exercisePath --outputPath"
+ " Run the tests for the exercise.\n"
Expand Down Expand Up @@ -138,6 +145,9 @@ private static void run(String command) {
case "prepare-solutions":
runPrepareSolutions();
break;
case "prepare-sandbox-task":
runPrepareSandboxTask();
break;
case "get-exercise-packaging-configuration":
runGetExercisePackagingConfiguration();
break;
Expand All @@ -157,6 +167,13 @@ private static Path getExercisePathFromArgs() {
throw new IllegalStateException("No " + EXERCISE_PATH + " provided");
}

private static Path getSubmissionPathFromArgs() {
if (argsMap.containsKey(SUBMISSION_PATH)) {
return Paths.get(argsMap.get(SUBMISSION_PATH));
}
throw new IllegalStateException("No " + SUBMISSION_PATH + " provided");
}

private static Locale getLocaleFromArgs() {
if (argsMap.containsKey(LOCALE)) {
return new Locale(argsMap.get(LOCALE));
Expand All @@ -182,17 +199,28 @@ private static void runCompressProject() {
}
}


private static Path getTmcRunPathFromArgs() {
if (argsMap.containsKey(TMC_RUN_PATH)) {
return Paths.get(argsMap.get(TMC_RUN_PATH));
}
throw new IllegalStateException("No " + TMC_RUN_PATH + " provided");
}

private static Path getTmcLangsPathFromArgs() {
if (argsMap.containsKey(TMC_LANGS_PATH)) {
return Paths.get(argsMap.get(TMC_LANGS_PATH));
}
throw new IllegalStateException("No " + TMC_LANGS_PATH + " provided");
}

private static void runCheckCodeStyle() {
ValidationResult validationResult = null;
try {
validationResult =
executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
validationResult = executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given exercise "
+ "path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}

try {
Expand All @@ -215,18 +243,13 @@ private static void runScanExercise() {
printErrAndExit("ERROR: Could not scan the exercises.");
}
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}

try {
JsonWriter.writeObjectIntoJsonFormat(exerciseDesc.get(), getOutputPathFromArgs());
System.out.println(
"Exercises scanned successfully, results can be found in "
+ getOutputPathFromArgs());
System.out.println("Exercises scanned successfully, results can be found in " + getOutputPathFromArgs());
} catch (IOException e) {
logger.error("Could not write output to {}", getOutputPathFromArgs(), e);
printErrAndExit("ERROR: Could not write the results to the given file.");
Expand Down Expand Up @@ -272,11 +295,8 @@ private static void runTests() {
try {
runResult = executor.runTests(getExercisePathFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}

try {
Expand All @@ -295,23 +315,17 @@ private static void runPrepareStubs() {
getExercisePathFromArgs(),
getOutputPathFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}
}

private static void runClean() {
try {
executor.clean(getExercisePathFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}
}

Expand Down Expand Up @@ -354,11 +368,26 @@ private static void runPrepareSolutions() {
getExercisePathFromArgs(),
getOutputPathFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}
}

private static void runPrepareSandboxTask() {
Path exercisePath = getExercisePathFromArgs();
Path submissionPath = getSubmissionPathFromArgs();
Path outputPath = getOutputPathFromArgs();
Path tmcRunPath = getTmcRunPathFromArgs();
Path tmcLangsPath = getTmcLangsPathFromArgs();

try {
executor.prepareSandboxTask(exercisePath, submissionPath, outputPath, tmcRunPath, tmcLangsPath);
} catch (NoLanguagePluginFoundException ex) {
logger.error("No suitable language plugin for project at {}", exercisePath, ex);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
} catch (IOException e) {
logger.error("An error occurred while preparing task.", e);
printErrAndExit("ERROR: Could not prepare task.");
}
}

Expand All @@ -367,11 +396,8 @@ private static void runGetExercisePackagingConfiguration() {
try {
configuration = executor.getExercisePackagingConfiguration(getExercisePathFromArgs());
} catch (NoLanguagePluginFoundException e) {
logger.error(
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit(
"ERROR: Could not find suitable language plugin for the given "
+ "exercise path.");
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ public MockClass(String name) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ void prepareSolutions(Map<Path, LanguagePlugin> exerciseMap, Path repoPath, Path
*/
void prepareStubs(Map<Path, LanguagePlugin> exerciseMap, Path repoPath, Path destPath)
throws NoLanguagePluginFoundException;

/**
* Finds the correct language plug-in for the given exercise path. After which
* it copies all files from exercisePath and all student files from submissionPath
* and creates a tarball with the aforementioned files and tmc-langs, and tmc-run.
*/
public void prepareSandboxTask(Path exercisePath, Path submissionPath,
Path outputPath, Path tmcRunPath, Path tmcLangsPath)
throws NoLanguagePluginFoundException, IOException;

/**
* Finds the correct language plug-in for the given exercise path. After which calls the
Expand Down Expand Up @@ -119,20 +128,9 @@ void extractProject(Path compressedProject, Path targetLocation, boolean overwri
ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path)
throws NoLanguagePluginFoundException;

/**
* Creates a tarball that can be submitted to TMC-sandbox.
* The tar is created to the target location
*
* @param projectDir Location of the unzipped project
* @param tmcLangs Location of tmc-langs-cli.jar
* @param tmcrun Location of tmc-run init script
* @param targetLocation Location where the tar archive should be extracted to
*/
void compressTarForSubmitting(Path projectDir, Path tmcLangs, Path tmcrun, Path targetLocation)
throws IOException, ArchiveException;

/**
* Run clean for given path using proper language plugin.
*/
void clean(Path path) throws NoLanguagePluginFoundException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
import com.google.common.base.Optional;

import org.apache.commons.compress.archivers.ArchiveException;

import org.codehaus.plexus.util.FileUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;


public class TaskExecutorImpl implements TaskExecutor {

Expand Down Expand Up @@ -106,7 +112,26 @@ public void prepareSolutions(
throws NoLanguagePluginFoundException {
new ExerciseBuilder().prepareSolutions(exerciseMap, repoPath, destPath);
}


@Override
public void prepareSandboxTask(Path exercisePath, Path submissionPath, Path outputPath,
Path tmcRunPath, Path tmcLangsPath)
throws NoLanguagePluginFoundException, IOException {
Path tempdir = Files.createTempDirectory("sandbox-task");
FileUtils.copyDirectoryStructure(exercisePath.toFile(), tempdir.toFile());
getLanguagePlugin(exercisePath).prepareSubmission(submissionPath, tempdir);
TarCreator tarCreator = new TarCreator();
log.info("Copying files to directory " + submissionPath.toString()
+ " and creating tar ball");
try {
tarCreator.createTarFromProject(tempdir, tmcLangsPath, tmcRunPath, outputPath);
} catch (ArchiveException ex) {
java.util.logging.Logger.getLogger(TaskExecutorImpl.class.getName()).log(Level.SEVERE,
"Failed to create tar from project " + submissionPath, ex);
}
FileUtils.forceDelete(tempdir.toFile());
}

@Override
public byte[] compressProject(Path path) throws NoLanguagePluginFoundException, IOException {
return getLanguagePlugin(path).compressProject(path);
Expand All @@ -118,14 +143,6 @@ public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path pat
return getLanguagePlugin(path).getExercisePackagingConfiguration(path);
}

@Override
public void compressTarForSubmitting(Path projectDir, Path tmcLangs,
Path tmcrun, Path targetLocation) throws IOException, ArchiveException {
TarCreator tarCompresser = new TarCreator();
log.info("Copying files to directory " + projectDir.toString() + " and creating tar ball");
tarCompresser.createTarFromProject(projectDir, tmcLangs, tmcrun, targetLocation);
}

@Override
public void clean(Path path) throws NoLanguagePluginFoundException {
getLanguagePlugin(path).clean(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TarCreator {

Expand All @@ -29,8 +30,8 @@ public class TarCreator {
*/
public void createTarFromProject(Path projectDir, Path tmcLangs, Path tmcrun,
Path targetLocation) throws IOException, ArchiveException {
Files.copy(tmcrun, projectDir.resolve(tmcrun.getFileName()));
Files.copy(tmcLangs, projectDir.resolve(tmcLangs.getFileName()));
Files.copy(tmcrun, projectDir.resolve(Paths.get("tmc-run.sh")));
Files.copy(tmcLangs, projectDir.resolve(Paths.get("tmc-langs.jar")));
createTarBall(projectDir, targetLocation);
}

Expand Down Expand Up @@ -58,7 +59,6 @@ private void createTarBall(Path project, Path targetLocation)
*
* @param folder The folder to add
* @param tar TarArchiveOutputStreamer tar
* @param lengthOfPath The length of String from root until the start folder.
* @throws FileNotFoundException Error!
* @throws IOException Error!
*/
Expand Down
Loading

0 comments on commit 0abbdd2

Please sign in to comment.