Skip to content

Commit 0abbdd2

Browse files
OAarnenygrenh
authored andcommitted
Improve functionality for preparing tasks to be sent to sandbox
1 parent 29b9ab2 commit 0abbdd2

File tree

6 files changed

+173
-67
lines changed

6 files changed

+173
-67
lines changed

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

+68-42
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Locale;
3434
import java.util.Map;
3535
import java.util.Set;
36+
import java.util.logging.Level;
3637

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

4647
private static final String EXERCISE_PATH = "exercisePath";
48+
private static final String SUBMISSION_PATH = "submissionPath";
4749
private static final String OUTPUT_PATH = "outputPath";
50+
private static final String TMC_RUN_PATH = "tmcRunPath";
51+
private static final String TMC_LANGS_PATH = "tmcLangsPath";
4852
private static final String LOCALE = "locale";
4953

5054
@VisibleForTesting static Map<String, String> argsMap = Maps.newHashMap();
@@ -62,8 +66,11 @@ public final class Main {
6266
+ " Prepare a presentable solution from the original.\n"
6367
+ " prepare-stubs --exercisePath -- outputPath"
6468
+ " Prepare a stub exercise from the original.\n"
65-
+ " prepare-submission --clonePath --submissionPath --outputPath"
66-
+ " Prepares from submission and solution project for which the tests"
69+
// TODO: Not implemented yet
70+
// + " prepare-submission --exercisePath --submissionPath --outputPath"
71+
// + " Prepares from submission and solution project for which the tests.\n"
72+
+ " prepare-sandbox-task --exercisePath --submissionPath --outputPath --tmcRunPath --tmcLangsPath"
73+
+ " Creates a tarball that sandbox can consume.\n"
6774
+ " can be run in sandbox\n"
6875
+ " run-tests --exercisePath --outputPath"
6976
+ " Run the tests for the exercise.\n"
@@ -138,6 +145,9 @@ private static void run(String command) {
138145
case "prepare-solutions":
139146
runPrepareSolutions();
140147
break;
148+
case "prepare-sandbox-task":
149+
runPrepareSandboxTask();
150+
break;
141151
case "get-exercise-packaging-configuration":
142152
runGetExercisePackagingConfiguration();
143153
break;
@@ -157,6 +167,13 @@ private static Path getExercisePathFromArgs() {
157167
throw new IllegalStateException("No " + EXERCISE_PATH + " provided");
158168
}
159169

170+
private static Path getSubmissionPathFromArgs() {
171+
if (argsMap.containsKey(SUBMISSION_PATH)) {
172+
return Paths.get(argsMap.get(SUBMISSION_PATH));
173+
}
174+
throw new IllegalStateException("No " + SUBMISSION_PATH + " provided");
175+
}
176+
160177
private static Locale getLocaleFromArgs() {
161178
if (argsMap.containsKey(LOCALE)) {
162179
return new Locale(argsMap.get(LOCALE));
@@ -182,17 +199,28 @@ private static void runCompressProject() {
182199
}
183200
}
184201

202+
203+
private static Path getTmcRunPathFromArgs() {
204+
if (argsMap.containsKey(TMC_RUN_PATH)) {
205+
return Paths.get(argsMap.get(TMC_RUN_PATH));
206+
}
207+
throw new IllegalStateException("No " + TMC_RUN_PATH + " provided");
208+
}
209+
210+
private static Path getTmcLangsPathFromArgs() {
211+
if (argsMap.containsKey(TMC_LANGS_PATH)) {
212+
return Paths.get(argsMap.get(TMC_LANGS_PATH));
213+
}
214+
throw new IllegalStateException("No " + TMC_LANGS_PATH + " provided");
215+
}
216+
185217
private static void runCheckCodeStyle() {
186218
ValidationResult validationResult = null;
187219
try {
188-
validationResult =
189-
executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
220+
validationResult = executor.runCheckCodeStyle(getExercisePathFromArgs(), getLocaleFromArgs());
190221
} catch (NoLanguagePluginFoundException e) {
191-
logger.error(
192-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
193-
printErrAndExit(
194-
"ERROR: Could not find suitable language plugin for the given exercise "
195-
+ "path.");
222+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
223+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
196224
}
197225

198226
try {
@@ -215,18 +243,13 @@ private static void runScanExercise() {
215243
printErrAndExit("ERROR: Could not scan the exercises.");
216244
}
217245
} catch (NoLanguagePluginFoundException e) {
218-
logger.error(
219-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
220-
printErrAndExit(
221-
"ERROR: Could not find suitable language plugin for the given "
222-
+ "exercise path.");
246+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
247+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
223248
}
224249

225250
try {
226251
JsonWriter.writeObjectIntoJsonFormat(exerciseDesc.get(), getOutputPathFromArgs());
227-
System.out.println(
228-
"Exercises scanned successfully, results can be found in "
229-
+ getOutputPathFromArgs());
252+
System.out.println("Exercises scanned successfully, results can be found in " + getOutputPathFromArgs());
230253
} catch (IOException e) {
231254
logger.error("Could not write output to {}", getOutputPathFromArgs(), e);
232255
printErrAndExit("ERROR: Could not write the results to the given file.");
@@ -272,11 +295,8 @@ private static void runTests() {
272295
try {
273296
runResult = executor.runTests(getExercisePathFromArgs());
274297
} catch (NoLanguagePluginFoundException e) {
275-
logger.error(
276-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
277-
printErrAndExit(
278-
"ERROR: Could not find suitable language plugin for the given "
279-
+ "exercise path.");
298+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
299+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
280300
}
281301

282302
try {
@@ -295,23 +315,17 @@ private static void runPrepareStubs() {
295315
getExercisePathFromArgs(),
296316
getOutputPathFromArgs());
297317
} catch (NoLanguagePluginFoundException e) {
298-
logger.error(
299-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
300-
printErrAndExit(
301-
"ERROR: Could not find suitable language plugin for the given "
302-
+ "exercise path.");
318+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
319+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
303320
}
304321
}
305322

306323
private static void runClean() {
307324
try {
308325
executor.clean(getExercisePathFromArgs());
309326
} catch (NoLanguagePluginFoundException e) {
310-
logger.error(
311-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
312-
printErrAndExit(
313-
"ERROR: Could not find suitable language plugin for the given "
314-
+ "exercise path.");
327+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
328+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
315329
}
316330
}
317331

@@ -354,11 +368,26 @@ private static void runPrepareSolutions() {
354368
getExercisePathFromArgs(),
355369
getOutputPathFromArgs());
356370
} catch (NoLanguagePluginFoundException e) {
357-
logger.error(
358-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
359-
printErrAndExit(
360-
"ERROR: Could not find suitable language plugin for the given "
361-
+ "exercise path.");
371+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
372+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
373+
}
374+
}
375+
376+
private static void runPrepareSandboxTask() {
377+
Path exercisePath = getExercisePathFromArgs();
378+
Path submissionPath = getSubmissionPathFromArgs();
379+
Path outputPath = getOutputPathFromArgs();
380+
Path tmcRunPath = getTmcRunPathFromArgs();
381+
Path tmcLangsPath = getTmcLangsPathFromArgs();
382+
383+
try {
384+
executor.prepareSandboxTask(exercisePath, submissionPath, outputPath, tmcRunPath, tmcLangsPath);
385+
} catch (NoLanguagePluginFoundException ex) {
386+
logger.error("No suitable language plugin for project at {}", exercisePath, ex);
387+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
388+
} catch (IOException e) {
389+
logger.error("An error occurred while preparing task.", e);
390+
printErrAndExit("ERROR: Could not prepare task.");
362391
}
363392
}
364393

@@ -367,11 +396,8 @@ private static void runGetExercisePackagingConfiguration() {
367396
try {
368397
configuration = executor.getExercisePackagingConfiguration(getExercisePathFromArgs());
369398
} catch (NoLanguagePluginFoundException e) {
370-
logger.error(
371-
"No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
372-
printErrAndExit(
373-
"ERROR: Could not find suitable language plugin for the given "
374-
+ "exercise path.");
399+
logger.error("No suitable language plugin for project at {}", getExercisePathFromArgs(), e);
400+
printErrAndExit("ERROR: Could not find suitable language plugin for the given exercise path.");
375401
}
376402

377403
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(path);
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)