Skip to content

Commit d9ec209

Browse files
Mavaifogre
authored andcommitted
Utils tarball extension
1 parent 73d54c1 commit d9ec209

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ __pycache__/
4848
.classpath
4949

5050
/tmc-langs-java/src/main/resources/tmc-junit-runner.jar
51-
/tmc-langs-java/src/test/resources/exit_zero/build/
51+
/tmc-langs-java/src/test/resources/exit_zero/build/
52+

tmc-langs-util/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878
<version>${project.version}</version>
7979
<type>jar</type>
8080
</dependency>
81+
<!-- tar handling -->
82+
<dependency>
83+
<groupId>org.apache.commons</groupId>
84+
<artifactId>commons-compress</artifactId>
85+
<version>1.14</version>
86+
<type>jar</type>
87+
</dependency>
8188
<dependency>
8289
<groupId>com.github.stefanbirkner</groupId>
8390
<artifactId>system-rules</artifactId>

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

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.google.common.annotations.Beta;
1111
import com.google.common.base.Optional;
1212

13+
import org.apache.commons.compress.archivers.ArchiveException;
14+
1315
import java.io.IOException;
1416
import java.nio.file.Path;
1517
import java.util.Locale;
@@ -117,6 +119,18 @@ void extractProject(Path compressedProject, Path targetLocation, boolean overwri
117119
ExercisePackagingConfiguration getExercisePackagingConfiguration(Path path)
118120
throws NoLanguagePluginFoundException;
119121

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+
120134
/**
121135
* Run clean for given path using proper language plugin.
122136
*/

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

+10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import fi.helsinki.cs.tmc.langs.io.NothingIsStudentFileStudentFilePolicy;
1212
import fi.helsinki.cs.tmc.langs.io.zip.StudentFileAwareUnzipper;
1313
import fi.helsinki.cs.tmc.langs.io.zip.Unzipper;
14+
import fi.helsinki.cs.tmc.langs.util.tarservice.TarCreator;
1415

1516
import com.google.common.base.Optional;
1617

18+
import org.apache.commons.compress.archivers.ArchiveException;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
1921

@@ -116,6 +118,14 @@ public ExercisePackagingConfiguration getExercisePackagingConfiguration(Path pat
116118
return getLanguagePlugin(path).getExercisePackagingConfiguration();
117119
}
118120

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+
119129
@Override
120130
public void clean(Path path) throws NoLanguagePluginFoundException {
121131
getLanguagePlugin(path).clean(path);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package fi.helsinki.cs.tmc.langs.util.tarservice;
2+
3+
import static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.LONGFILE_POSIX;
4+
5+
import org.apache.commons.compress.archivers.ArchiveException;
6+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
7+
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
8+
import org.apache.commons.compress.utils.IOUtils;
9+
10+
import java.io.FileInputStream;
11+
import java.io.FileNotFoundException;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
17+
public class TarCreator {
18+
19+
/**
20+
* Copies the location of tmc-langs-cli and tmcrun to unzipped project folder
21+
* and creates a tarball.
22+
*
23+
* @param projectDir Location of unzipped project dir
24+
* @param tmcLangs Location of tmc-langs-cli.jar
25+
* @param tmcrun Location of tmc-run init script
26+
* @param targetLocation Location where the tar archive should be extracted to
27+
* @throws IOException Error!
28+
* @throws ArchiveException Error!
29+
*/
30+
public void createTarFromProject(Path projectDir, Path tmcLangs, Path tmcrun,
31+
Path targetLocation) throws IOException, ArchiveException {
32+
Files.copy(tmcrun, projectDir.resolve(tmcrun.getFileName()));
33+
Files.copy(tmcLangs, projectDir.resolve(tmcLangs.getFileName()));
34+
createTarBall(projectDir, targetLocation);
35+
}
36+
37+
/**
38+
* Creates a tarball from a directory.
39+
*
40+
* @param project Project directory file
41+
* @param targetLocation Location where the tar archive should be extracted to
42+
* @throws IOException Error!
43+
* @throws ArchiveException Error!
44+
*/
45+
private void createTarBall(Path project, Path targetLocation)
46+
throws IOException, ArchiveException {
47+
try (FileOutputStream tarOut = new FileOutputStream(targetLocation.toString())) {
48+
try (TarArchiveOutputStream aos = new TarArchiveOutputStream(tarOut)) {
49+
aos.setLongFileMode(LONGFILE_POSIX);
50+
addFilesToTarBall(project, aos, project);
51+
aos.finish();
52+
}
53+
}
54+
}
55+
56+
/**
57+
* Adds all files and folders inside a folder to a tar file.
58+
*
59+
* @param folder The folder to add
60+
* @param tar TarArchiveOutputStreamer tar
61+
* @param lengthOfPath The length of String from root until the start folder.
62+
* @throws FileNotFoundException Error!
63+
* @throws IOException Error!
64+
*/
65+
private void addFilesToTarBall(Path folder, TarArchiveOutputStream tar,
66+
Path basePath) throws FileNotFoundException, IOException {
67+
for (Path path : Files.newDirectoryStream(folder)) {
68+
if (Files.isDirectory(path)) {
69+
addFilesToTarBall(path, tar, basePath);
70+
} else {
71+
TarArchiveEntry entry = new TarArchiveEntry(basePath.relativize(path).toString());
72+
entry.setSize(path.toFile().length());
73+
tar.putArchiveEntry(entry);
74+
try (FileInputStream fis = new FileInputStream(path.toFile())) {
75+
IOUtils.copy(fis, tar);
76+
}
77+
tar.closeArchiveEntry();
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)