27
27
28
28
import java .io .File ;
29
29
import java .io .IOException ;
30
+ import java .nio .file .FileVisitResult ;
30
31
import java .nio .file .Files ;
31
32
import java .nio .file .Path ;
32
33
import java .nio .file .Paths ;
34
+ import java .nio .file .SimpleFileVisitor ;
35
+ import java .nio .file .attribute .BasicFileAttributes ;
33
36
import java .util .List ;
34
37
import java .util .Locale ;
35
38
import java .util .Map ;
@@ -42,8 +45,8 @@ public final class Python3Plugin extends AbstractLanguagePlugin {
42
45
private static final Path INIT_PY_PATH = Paths .get ("__init__.py" );
43
46
private static final Path TMC_TEST_LIBRARY_PATH = Paths .get ("tmc" );
44
47
private static final Path MAIN_PY_PATH = Paths .get ("__main__.py" );
45
- private static final String CANNOT_CLEAN_FILES_MESSAGE = "Failed to clean files." ;
46
48
49
+ private static final String CANNOT_CLEAN_FILES_MESSAGE = "Failed to clean files." ;
47
50
private static final String CANNOT_RUN_TESTS_MESSAGE = "Failed to run tests." ;
48
51
private static final String CANNOT_PARSE_TEST_RESULTS_MESSAGE = "Failed to read test results." ;
49
52
private static final String CANNOT_SCAN_EXERCISE_MESSAGE = "Failed to scan exercise." ;
@@ -156,11 +159,29 @@ private String[] getTestCommand() {
156
159
157
160
@ Override
158
161
public void clean (Path path ) {
159
- String [] command = {"/bin/bash" , "-c" , "find . -type d -name __pycache__ -exec rm -r {} \\ + -o -type f -name .available_points.json -delete -o -type f -name .tmc_test_results.json -delete" };
160
- ProcessRunner runner = new ProcessRunner (command , path );
161
162
try {
162
- runner .call ();
163
- } catch (Exception e ) {
163
+ Files .walkFileTree (path , new SimpleFileVisitor <Path >() {
164
+ @ Override
165
+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs )
166
+ throws IOException {
167
+ if (file .endsWith (".available_points.json" )
168
+ || file .endsWith (".tmc_test_results.json" )
169
+ || file .toString ().contains ("__pycache__" )) {
170
+ Files .delete (file );
171
+ }
172
+ return FileVisitResult .CONTINUE ;
173
+ }
174
+
175
+ @ Override
176
+ public FileVisitResult postVisitDirectory (Path dir , IOException exc )
177
+ throws IOException {
178
+ if (dir .endsWith ("__pycache__" )) {
179
+ Files .delete (dir );
180
+ }
181
+ return FileVisitResult .CONTINUE ;
182
+ }
183
+ });
184
+ } catch (IOException e ) {
164
185
log .error (CANNOT_CLEAN_FILES_MESSAGE , e );
165
186
}
166
187
}
0 commit comments