Skip to content

Commit 607b323

Browse files
committed
Improve functionality for preparing tasks to be sent to sandbox
1 parent c7746a6 commit 607b323

File tree

6 files changed

+173
-68
lines changed

6 files changed

+173
-68
lines changed

tmc-langs-cli/src/main/java/fi/helsinki/cs/tmc/langs/cli/Main.java

+68-43
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Locale;
3333
import java.util.Map;
3434
import java.util.Set;
35+
import java.util.logging.Level;
3536

3637
/*
3738
* TODO: unstaticify this class
@@ -43,7 +44,10 @@ public final class Main {
4344
private static final Logger logger = LoggerFactory.getLogger(Main.class);
4445

4546
private static final String EXERCISE_PATH = "exercisePath";
47+
private static final String SUBMISSION_PATH = "submissionPath";
4648
private static final String OUTPUT_PATH = "outputPath";
49+
private static final String TMC_RUN_PATH = "tmcRunPath";
50+
private static final String TMC_LANGS_PATH = "tmcLangsPath";
4751
private static final String LOCALE = "locale";
4852

4953
@VisibleForTesting static Map<String, String> argsMap = Maps.newHashMap();
@@ -60,8 +64,11 @@ public final class Main {
6064
+ " Prepare a presentable solution from the original.\n"
6165
+ " prepare-stubs --exercisePath -- outputPath"
6266
+ " Prepare a stub exercise from the original.\n"
63-
+ " prepare-submission --clonePath --submissionPath --outputPath"
64-
+ " Prepares from submission and solution project for which the tests"
67+
// TODO: Not implemented yet
68+
// + " prepare-submission --exercisePath --submissionPath --outputPath"
69+
// + " Prepares from submission and solution project for which the tests.\n"
70+
+ " prepare-sandbox-task --exercisePath --submissionPath --outputPath --tmcRunPath --tmcLangsPath"
71+
+ " Creates a tarball that sandbox can consume.\n"
6572
+ " can be run in sandbox\n"
6673
+ " run-tests --exercisePath --outputPath"
6774
+ " Run the tests for the exercise.\n"
@@ -133,6 +140,9 @@ private static void run(String command) {
133140
case "prepare-solutions":
134141
runPrepareSolutions();
135142
break;
143+
case "prepare-sandbox-task":
144+
runPrepareSandboxTask();
145+
break;
136146
case "get-exercise-packaging-configuration":
137147
runGetExercisePackagingConfiguration();
138148
break;
@@ -151,6 +161,13 @@ private static Path getExercisePathFromArgs() {
151161
}
152162
throw new IllegalStateException("No " + EXERCISE_PATH + " provided");
153163
}
164+
165+
private static Path getSubmissionPathFromArgs() {
166+
if (argsMap.containsKey(SUBMISSION_PATH)) {
167+
return Paths.get(argsMap.get(SUBMISSION_PATH));
168+
}
169+
throw new IllegalStateException("No " + SUBMISSION_PATH + " provided");
170+
}
154171

155172
private static Locale getLocaleFromArgs() {
156173
if (argsMap.containsKey(LOCALE)) {
@@ -165,18 +182,28 @@ private static Path getOutputPathFromArgs() {
165182
}
166183
throw new IllegalStateException("No " + OUTPUT_PATH + " provided");
167184
}
168-
185+
186+
private static Path getTmcRunPathFromArgs() {
187+
if (argsMap.containsKey(TMC_RUN_PATH)) {
188+
return Paths.get(argsMap.get(TMC_RUN_PATH));
189+
}
190+
throw new IllegalStateException("No " + TMC_RUN_PATH + " provided");
191+
}
192+
193+
private static Path getTmcLangsPathFromArgs() {
194+
if (argsMap.containsKey(TMC_LANGS_PATH)) {
195+
return Paths.get(argsMap.get(TMC_LANGS_PATH));
196+
}
197+
throw new IllegalStateException("No " + TMC_LANGS_PATH + " provided");
198+
}
199+
169200
private static void runCheckCodeStyle() {
170201
ValidationResult validationResult = null;
171202
try {
172-
validationResult =
173-
executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
203+
validationResult = executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
174204
} catch (NoLanguagePluginFoundException e) {
175-
logger.error(
176-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
177-
printErrAndExit(
178-
"ERROR: Could not find suitable language plugin for the given exercise "
179-
+ "path.");
205+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
206+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
180207
}
181208

182209
try {
@@ -199,18 +226,13 @@ private static void runScanExercise() {
199226
printErrAndExit("ERROR: Could not scan the exercises.");
200227
}
201228
} catch (NoLanguagePluginFoundException e) {
202-
logger.error(
203-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
204-
printErrAndExit(
205-
"ERROR: Could not find suitable language plugin for the given "
206-
+ "exercise path.");
229+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
230+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
207231
}
208232

209233
try {
210234
JsonWriter.writeObjectIntoJsonFormat(exerciseDesc.get(), getOutputPathFromArgs());
211-
System.out.println(
212-
"Exercises scanned successfully, results can be found in "
213-
+ getOutputPathFromArgs());
235+
System.out.println("Exercises scanned successfully, results can be found in " + getOutputPathFromArgs());
214236
} catch (IOException e) {
215237
logger.error("Could not write output to {}", getOutputPathFromArgs(), e);
216238
printErrAndExit("ERROR: Could not write the results to the given file.");
@@ -256,11 +278,8 @@ private static void runTests() {
256278
try {
257279
runResult = executor.runTests(getExercisePathFromArgs());
258280
} catch (NoLanguagePluginFoundException e) {
259-
logger.error(
260-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
261-
printErrAndExit(
262-
"ERROR: Could not find suitable language plugin for the given "
263-
+ "exercise path.");
281+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
282+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
264283
}
265284

266285
try {
@@ -279,23 +298,17 @@ private static void runPrepareStubs() {
279298
getExercisePathFromArgs(),
280299
getOutputPathFromArgs());
281300
} catch (NoLanguagePluginFoundException e) {
282-
logger.error(
283-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
284-
printErrAndExit(
285-
"ERROR: Could not find suitable language plugin for the given "
286-
+ "exercise path.");
301+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
302+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
287303
}
288304
}
289305

290306
private static void runClean() {
291307
try {
292308
executor.clean(getExercisePathFromArgs());
293309
} catch (NoLanguagePluginFoundException e) {
294-
logger.error(
295-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
296-
printErrAndExit(
297-
"ERROR: Could not find suitable language plugin for the given "
298-
+ "exercise path.");
310+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
311+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
299312
}
300313
}
301314

@@ -338,11 +351,26 @@ private static void runPrepareSolutions() {
338351
getExercisePathFromArgs(),
339352
getOutputPathFromArgs());
340353
} catch (NoLanguagePluginFoundException e) {
341-
logger.error(
342-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
343-
printErrAndExit(
344-
"ERROR: Could not find suitable language plugin for the given "
345-
+ "exercise path.");
354+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
355+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
356+
}
357+
}
358+
359+
private static void runPrepareSandboxTask() {
360+
Path exercisePath = getExercisePathFromArgs();
361+
Path submissionPath = getSubmissionPathFromArgs();
362+
Path outputPath = getOutputPathFromArgs();
363+
Path tmcRunPath = getTmcRunPathFromArgs();
364+
Path tmcLangsPath = getTmcLangsPathFromArgs();
365+
366+
try {
367+
executor.prepareSandboxTask(exercisePath, submissionPath, outputPath, tmcRunPath, tmcLangsPath);
368+
} catch (NoLanguagePluginFoundException ex) {
369+
logger.error("No suitable language plugin for project at {}", exercisePath, ex);
370+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
371+
} catch (IOException e) {
372+
logger.error("An error occurred while preparing task.", e);
373+
printErrAndExit("ERROR: Could not prepare task.");
346374
}
347375
}
348376

@@ -351,11 +379,8 @@ private static void runGetExercisePackagingConfiguration() {
351379
try {
352380
configuration = executor.getExercisePackagingConfiguration(getExercisePathFromArgs());
353381
} catch (NoLanguagePluginFoundException e) {
354-
logger.error(
355-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
356-
printErrAndExit(
357-
"ERROR: Could not find suitable language plugin for the given "
358-
+ "exercise path.");
382+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
383+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
359384
}
360385

361386
try {

tmc-langs-cli/src/test/java/fi/helsinki/cs/tmc/langs/cli/JsonWriterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public MockClass(String name) {
4646
}
4747
}
4848
}
49-
}
49+
}

tmc-langs-util/src/main/java/fi/helsinki/cs/tmc/langs/util/TaskExecutor.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ void prepareSolutions(Map<Path, LanguagePlugin> exerciseMap, Path repoPath, Path
4444
*/
4545
void prepareStubs(Map<Path, LanguagePlugin> exerciseMap, Path repoPath, Path destPath)
4646
throws NoLanguagePluginFoundException;
47+
48+
/**
49+
* Finds the correct language plug-in for the given exercise path. After which
50+
* it copies all files from exercisePath and all student files from submissionPath
51+
* and creates a tarball with the aforementioned files and tmc-langs, and tmc-run.
52+
*/
53+
public void prepareSandboxTask(Path exercisePath, Path submissionPath,
54+
Path outputPath, Path tmcRunPath, Path tmcLangsPath)
55+
throws NoLanguagePluginFoundException, IOException;
4756

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

122-
/**
123-
* Creates a tarball that can be submitted to TMC-sandbox.
124-
* The tar is created to the target location
125-
*
126-
* @param projectDir Location of the unzipped project
127-
* @param tmcLangs Location of tmc-langs-cli.jar
128-
* @param tmcrun Location of tmc-run init script
129-
* @param targetLocation Location where the tar archive should be extracted to
130-
*/
131-
void compressTarForSubmitting(Path projectDir, Path tmcLangs, Path tmcrun, Path targetLocation)
132-
throws IOException, ArchiveException;
133-
134131
/**
135132
* Run clean for given path using proper language plugin.
136133
*/
137134
void clean(Path path) throws NoLanguagePluginFoundException;
135+
138136
}

tmc-langs-util/src/main/java/fi/helsinki/cs/tmc/langs/util/TaskExecutorImpl.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616
import com.google.common.base.Optional;
1717

1818
import org.apache.commons.compress.archivers.ArchiveException;
19+
20+
import org.codehaus.plexus.util.FileUtils;
21+
1922
import org.slf4j.Logger;
2023
import org.slf4j.LoggerFactory;
2124

2225
import java.io.IOException;
26+
import java.nio.file.Files;
2327
import java.nio.file.Path;
2428
import java.util.Locale;
2529
import java.util.Map;
30+
import java.util.logging.Level;
31+
2632

2733
public class TaskExecutorImpl implements TaskExecutor {
2834

@@ -106,7 +112,26 @@ public void prepareSolutions(
106112
throws NoLanguagePluginFoundException {
107113
new ExerciseBuilder().prepareSolutions(exerciseMap, repoPath, destPath);
108114
}
109-
115+
116+
@Override
117+
public void prepareSandboxTask(Path exercisePath, Path submissionPath, Path outputPath,
118+
Path tmcRunPath, Path tmcLangsPath)
119+
throws NoLanguagePluginFoundException, IOException {
120+
Path tempdir = Files.createTempDirectory("sandbox-task");
121+
FileUtils.copyDirectoryStructure(exercisePath.toFile(), tempdir.toFile());
122+
getLanguagePlugin(exercisePath).prepareSubmission(submissionPath, tempdir);
123+
TarCreator tarCreator = new TarCreator();
124+
log.info("Copying files to directory " + submissionPath.toString()
125+
+ " and creating tar ball");
126+
try {
127+
tarCreator.createTarFromProject(tempdir, tmcLangsPath, tmcRunPath, outputPath);
128+
} catch (ArchiveException ex) {
129+
java.util.logging.Logger.getLogger(TaskExecutorImpl.class.getName()).log(Level.SEVERE,
130+
"Failed to create tar from project " + submissionPath, ex);
131+
}
132+
FileUtils.forceDelete(tempdir.toFile());
133+
}
134+
110135
@Override
111136
public byte[] compressProject(Path path) throws NoLanguagePluginFoundException, IOException {
112137
return getLanguagePlugin(path).compressProject(path);
@@ -118,14 +143,6 @@ public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path pat
118143
return getLanguagePlugin(path).getExercisePackagingConfiguration();
119144
}
120145

121-
@Override
122-
public void compressTarForSubmitting(Path projectDir, Path tmcLangs,
123-
Path tmcrun, Path targetLocation) throws IOException, ArchiveException {
124-
TarCreator tarCompresser = new TarCreator();
125-
log.info("Copying files to directory " + projectDir.toString() + " and creating tar ball");
126-
tarCompresser.createTarFromProject(projectDir, tmcLangs, tmcrun, targetLocation);
127-
}
128-
129146
@Override
130147
public void clean(Path path) throws NoLanguagePluginFoundException {
131148
getLanguagePlugin(path).clean(path);

tmc-langs-util/src/main/java/fi/helsinki/cs/tmc/langs/util/tarservice/TarCreator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.IOException;
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
16+
import java.nio.file.Paths;
1617

1718
public class TarCreator {
1819

@@ -29,8 +30,8 @@ public class TarCreator {
2930
*/
3031
public void createTarFromProject(Path projectDir, Path tmcLangs, Path tmcrun,
3132
Path targetLocation) throws IOException, ArchiveException {
32-
Files.copy(tmcrun, projectDir.resolve(tmcrun.getFileName()));
33-
Files.copy(tmcLangs, projectDir.resolve(tmcLangs.getFileName()));
33+
Files.copy(tmcrun, projectDir.resolve(Paths.get("tmc-run.sh")));
34+
Files.copy(tmcLangs, projectDir.resolve(Paths.get("tmc-langs.jar")));
3435
createTarBall(projectDir, targetLocation);
3536
}
3637

@@ -58,7 +59,6 @@ private void createTarBall(Path project, Path targetLocation)
5859
*
5960
* @param folder The folder to add
6061
* @param tar TarArchiveOutputStreamer tar
61-
* @param lengthOfPath The length of String from root until the start folder.
6262
* @throws FileNotFoundException Error!
6363
* @throws IOException Error!
6464
*/

0 commit comments

Comments
 (0)