@@ -25,37 +25,45 @@ public LogicClean(CommandClean cmd) {
2525
2626 public void run () {
2727 log .info ("Running cleanup" );
28- if (cmd .dryRun ) log .info ("Dry-run, won't take any action." );
29- int stepsPerformed = 0 ;
28+ final List <Path > scheduledForDeletion = getScheduledForDeletion ();
3029
31- if (!cmd .noPep ) {
32- stepsPerformed ++;
33- log .info ("Cleaning up peptide identification files." );
34- DeletionStats stats = deleteMatchingFilesInSubtrees (cmd .inputFiles ,
35- CacheLocator ::isPepCacheFile , cmd .dryRun );
36- log .info ("Pep ID cleanup summary: scheduled: {}, deleted: {}, skipped: {}, error: {}" ,
37- stats .scheduled , stats .deleted , stats .skipped .size (), stats .error .size ());
38- }
30+ final String list = scheduledForDeletion .stream ().map (Path ::toString )
31+ .collect (Collectors .joining ("\n " ));
32+ log .info ("Files to delete:\n " + list );
3933
40- if (!cmd .noLcms ) {
41- stepsPerformed ++;
42- log .info ("Cleaning up LC-MS calibration files." );
43- DeletionStats stats = deleteMatchingFilesInSubtrees (cmd .inputFiles ,
44- CacheLocator ::isCalCacheFile , cmd .dryRun );
45- log .info ("Calibration cleanup summary: scheduled: {}, deleted: {}, skipped: {}, error: {}" ,
46- stats .scheduled , stats .deleted , stats .skipped .size (), stats .error .size ());
34+ if (cmd .dryRun ) {
35+ log .info ("Dry-run, won't take any action." );
36+ return ;
4737 }
4838
49- if (stepsPerformed == 0 ) log .info ("You made it clear that nothing should be cleaned up. Doing nothing." );
39+ DeletionStats stats = delete (scheduledForDeletion );
40+ log .info ("Cleanup: deleted {}/{}, errors: {}" , stats .deleted .size (), stats .scheduled .size (), stats .error .size ());
41+ }
42+
43+ private static List <Path > modifyInputPathsForCacheDeletion (List <Path > paths ) {
44+ List <Path > possibleCachePaths = new ArrayList <>();
45+ for (Path inputFile : paths ) {
46+ if (Files .isDirectory (inputFile )) {
47+ possibleCachePaths .add (inputFile );
48+ } else {
49+ Path parent = inputFile .getParent ();
50+ Path fn = inputFile .getFileName ();
51+ possibleCachePaths .add (parent .resolve (fn .toString () + CacheLocator .CACHE_EXT_PEP ));
52+ possibleCachePaths .add (parent .resolve (fn .toString () + CacheLocator .CACHE_EXT_CAL ));
53+ }
54+ }
55+ return possibleCachePaths ;
5056 }
5157
5258 public List <Path > getScheduledForDeletion () {
5359 List <Path > delete = new ArrayList <>();
60+ final List <Path > putativeCachePaths = modifyInputPathsForCacheDeletion (cmd .inputFiles );
61+
5462 if (!cmd .noPep ) {
55- delete .addAll (getScheduledForDeletion (cmd . inputFiles , CacheLocator ::isPepCacheFile ));
63+ delete .addAll (getScheduledForDeletion (putativeCachePaths , CacheLocator ::isPepCacheFile ));
5664 }
5765 if (!cmd .noLcms ) {
58- delete .addAll (getScheduledForDeletion (cmd . inputFiles , CacheLocator ::isCalCacheFile ));
66+ delete .addAll (getScheduledForDeletion (putativeCachePaths , CacheLocator ::isCalCacheFile ));
5967 }
6068 return delete ;
6169 }
@@ -89,73 +97,34 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
8997 log .warn ("Error collecting files for deletion in subtree: {}" + path , e );
9098 }
9199 } else {
92- if (condition .test (path )) {
100+ if (Files . exists ( path ) && condition .test (path )) {
93101 delete .add (path );
94102 }
95103 }
96104 }
97105 return new ArrayList <>(delete );
98106 }
99107
100- private class DeletionStats {
101- int scheduled = 0 ;
102- int deleted = 0 ;
103- ConcurrentLinkedQueue <Path > skipped = new ConcurrentLinkedQueue <>();
108+ private static class DeletionStats {
109+ ConcurrentLinkedQueue <Path > scheduled = new ConcurrentLinkedQueue <>();
110+ ConcurrentLinkedQueue <Path > deleted = new ConcurrentLinkedQueue <>();
104111 ConcurrentLinkedQueue <Path > error = new ConcurrentLinkedQueue <>();
105112 }
106113
107- private DeletionStats deleteMatchingFilesInSubtrees (Iterable <? extends Path > paths , Predicate <? super Path > condition , boolean dryRun ) {
108- final ConcurrentLinkedQueue <Path > delete = new ConcurrentLinkedQueue <>();
109- final DeletionStats stats = new DeletionStats ();
110-
111- for (Path path : paths ) {
112- if (Files .isDirectory (path )) {
113- try {
114- Files .walkFileTree (path , new SimpleFileVisitor <Path >() {
115- @ Override
116- public FileVisitResult preVisitDirectory (Path dir , BasicFileAttributes attrs ) {
117- if (!Files .isReadable (dir )) return FileVisitResult .SKIP_SUBTREE ;
118- return FileVisitResult .CONTINUE ;
119- }
120-
121- @ Override
122- public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) {
123- if (!Files .isWritable (file )) {
124- log .warn ("File not writeable, skipping: {}" , file );
125- stats .skipped .add (file );
126- return FileVisitResult .CONTINUE ;
127- }
128- if (condition .test (file )) delete .add (file );
129- return FileVisitResult .CONTINUE ;
130- }
131- });
132- } catch (IOException e ) {
133- log .error ("Error collecting files for deletion in subtree: {}" + path , e );
134- }
135- } else {
136- if (CacheLocator .isPepCacheFile (path ))
137- delete .add (path );
138- }
139- }
140-
141- if (!delete .isEmpty ()) {
142- log .info ("Files scheduled for deletion:\n {}" ,
143- delete .stream ().map (Path ::toString ).collect (Collectors .joining ("\n " )));
144- } else {
145- log .info ("Found no files to delete." );
146- }
147- stats .scheduled = delete .size ();
148-
149- if (!dryRun ) delete .forEach (path -> {
114+ public static DeletionStats delete (List <Path > pathsToDelete ) {
115+ DeletionStats ds = new DeletionStats ();
116+ ds .scheduled .addAll (pathsToDelete );
117+ for (Path path : pathsToDelete ) {
150118 try {
151- Files .deleteIfExists (path );
152- stats .deleted ++;
119+ if (Files .deleteIfExists (path )) {
120+ ds .deleted .add (path );
121+ }
153122 } catch (IOException e ) {
154- log .error ("Could not delete file: " + path , e );
155- stats .error .add (path );
123+ log .error ("Error deleting file: " + path . toString () + " \n Details: \n " + e . getMessage () );
124+ ds .error .add (path );
156125 }
157- });
158-
159- return stats ;
126+ }
127+ return ds ;
160128 }
129+
161130}
0 commit comments