1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import java.io.File; |
24 |
|
import java.io.IOException; |
25 |
|
import java.net.MalformedURLException; |
26 |
|
import java.net.URL; |
27 |
|
import java.nio.file.FileSystems; |
28 |
|
import java.nio.file.FileVisitOption; |
29 |
|
import java.nio.file.FileVisitResult; |
30 |
|
import java.nio.file.Files; |
31 |
|
import java.nio.file.Path; |
32 |
|
import java.nio.file.PathMatcher; |
33 |
|
import java.nio.file.Paths; |
34 |
|
import java.nio.file.SimpleFileVisitor; |
35 |
|
import java.nio.file.attribute.BasicFileAttributes; |
36 |
|
import java.util.ArrayList; |
37 |
|
import java.util.Collections; |
38 |
|
import java.util.EnumSet; |
39 |
|
import java.util.HashSet; |
40 |
|
import java.util.List; |
41 |
|
import java.util.Set; |
42 |
|
import java.util.stream.Collectors; |
43 |
|
|
44 |
|
import jalview.bin.Console; |
45 |
|
|
|
|
| 86.9% |
Uncovered Elements: 35 (267) |
Complexity: 68 |
Complexity Density: 0.41 |
|
46 |
|
public class FileUtils |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
54 |
164 |
public static List<File> getFilesFromGlob(String pattern)... |
55 |
|
{ |
56 |
164 |
return getFilesFromGlob(pattern, true); |
57 |
|
} |
58 |
|
|
|
|
| 87% |
Uncovered Elements: 6 (46) |
Complexity: 13 |
Complexity Density: 0.43 |
|
59 |
182 |
public static List<File> getFilesFromGlob(String pattern,... |
60 |
|
boolean allowSingleFilenameThatDoesNotExist) |
61 |
|
{ |
62 |
182 |
pattern = substituteHomeDir(pattern); |
63 |
182 |
String relativePattern = pattern.startsWith(File.separator) ? null |
64 |
|
: pattern; |
65 |
182 |
List<File> files = new ArrayList<>(); |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
182 |
int firstGlobChar = -1; |
74 |
182 |
boolean foundGlobChar = false; |
75 |
182 |
for (char c : new char[] { '*', '{', '?' }) |
76 |
|
{ |
77 |
546 |
if (pattern.indexOf(c) > -1 |
78 |
|
&& (pattern.indexOf(c) < firstGlobChar || !foundGlobChar)) |
79 |
|
{ |
80 |
65 |
firstGlobChar = pattern.indexOf(c); |
81 |
65 |
foundGlobChar = true; |
82 |
|
} |
83 |
|
} |
84 |
182 |
int lastFS = pattern.lastIndexOf(File.separatorChar, firstGlobChar); |
85 |
182 |
if (foundGlobChar) |
86 |
|
{ |
87 |
65 |
String pS = pattern.substring(0, lastFS + 1); |
88 |
65 |
String rest = pattern.substring(lastFS + 1); |
89 |
65 |
if ("".equals(pS)) |
90 |
|
{ |
91 |
0 |
pS = "."; |
92 |
|
} |
93 |
65 |
Path parentDir = Paths.get(pS); |
94 |
65 |
if (parentDir.toFile().exists()) |
95 |
|
{ |
96 |
65 |
try |
97 |
|
{ |
98 |
65 |
String glob = "glob:" + parentDir.toString() + File.separator |
99 |
|
+ rest; |
100 |
65 |
if (Platform.isWin()) |
101 |
|
{ |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
0 |
glob = glob.replaceAll("\\\\", "\\\\\\\\"); |
106 |
|
} |
107 |
65 |
PathMatcher pm = FileSystems.getDefault().getPathMatcher(glob); |
108 |
65 |
int maxDepth = rest.contains("**") ? 1028 |
109 |
|
: (int) (rest.chars() |
110 |
|
.filter(ch -> ch == File.separatorChar).count()) |
111 |
|
+ 1; |
112 |
|
|
113 |
65 |
Files.walkFileTree(parentDir, |
114 |
|
EnumSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth, |
115 |
|
new SimpleFileVisitor<Path>() |
116 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
117 |
6440 |
@Override... |
118 |
|
public FileVisitResult visitFile(Path path, |
119 |
|
BasicFileAttributes attrs) throws IOException |
120 |
|
{ |
121 |
6440 |
if (pm.matches(path)) |
122 |
|
{ |
123 |
3174 |
files.add(path.toFile()); |
124 |
|
} |
125 |
6440 |
return FileVisitResult.CONTINUE; |
126 |
|
} |
127 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
128 |
0 |
@Override... |
129 |
|
public FileVisitResult visitFileFailed(Path file, |
130 |
|
IOException exc) throws IOException |
131 |
|
{ |
132 |
0 |
return FileVisitResult.CONTINUE; |
133 |
|
} |
134 |
|
}); |
135 |
|
} catch (IOException e) |
136 |
|
{ |
137 |
0 |
e.printStackTrace(); |
138 |
|
} |
139 |
|
} |
140 |
|
} |
141 |
|
else |
142 |
|
{ |
143 |
|
|
144 |
117 |
File f = new File(pattern); |
145 |
117 |
if (allowSingleFilenameThatDoesNotExist || f.exists()) |
146 |
|
{ |
147 |
109 |
files.add(f); |
148 |
|
} |
149 |
|
} |
150 |
182 |
Collections.sort(files); |
151 |
|
|
152 |
182 |
return files; |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
139 |
public static List<String> getFilenamesFromGlob(String pattern)... |
156 |
|
{ |
157 |
|
|
158 |
139 |
return getFilesFromGlob(pattern).stream().map(f -> f.getPath()) |
159 |
|
.collect(Collectors.toList()); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
162 |
373 |
public static String substituteHomeDir(String path)... |
163 |
|
{ |
164 |
373 |
return path.startsWith("~" + File.separator) |
165 |
|
? System.getProperty("user.home") + path.substring(1) |
166 |
|
: path; |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
78 |
public static String getBasename(File file)... |
173 |
|
{ |
174 |
78 |
return getBasenameOrExtension(file, false); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
180 |
10 |
public static String getExtension(File file)... |
181 |
|
{ |
182 |
10 |
return getBasenameOrExtension(file, true); |
183 |
|
} |
184 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
185 |
88 |
public static String getBasenameOrExtension(File file, boolean extension)... |
186 |
|
{ |
187 |
88 |
if (file == null) |
188 |
0 |
return null; |
189 |
|
|
190 |
88 |
String value = null; |
191 |
88 |
String filename = file.getName(); |
192 |
88 |
int lastDot = filename.lastIndexOf('.'); |
193 |
88 |
if (lastDot > 0) |
194 |
|
{ |
195 |
87 |
value = extension ? filename.substring(lastDot + 1) |
196 |
|
: filename.substring(0, lastDot); |
197 |
|
} |
198 |
|
else |
199 |
|
{ |
200 |
1 |
value = extension ? "" : filename; |
201 |
|
} |
202 |
88 |
return value; |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
|
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
209 |
104 |
public static String getDirname(File file)... |
210 |
|
{ |
211 |
104 |
if (file == null) |
212 |
0 |
return null; |
213 |
|
|
214 |
104 |
String dirname = null; |
215 |
104 |
File p = file.getParentFile(); |
216 |
104 |
if (p == null) |
217 |
|
{ |
218 |
0 |
p = new File("."); |
219 |
|
} |
220 |
104 |
File d = new File(substituteHomeDir(p.getPath())); |
221 |
104 |
dirname = d.getPath(); |
222 |
104 |
return dirname; |
223 |
|
} |
224 |
|
|
|
|
| 91.3% |
Uncovered Elements: 2 (23) |
Complexity: 5 |
Complexity Density: 0.29 |
|
225 |
19 |
public static String convertWildcardsToPath(String value, String wildcard,... |
226 |
|
String dirname, String basename) |
227 |
|
{ |
228 |
19 |
if (value == null) |
229 |
|
{ |
230 |
0 |
return null; |
231 |
|
} |
232 |
19 |
StringBuilder path = new StringBuilder(); |
233 |
19 |
int lastFileSeparatorIndex = value.lastIndexOf(File.separatorChar); |
234 |
19 |
int wildcardBeforeIndex = value.indexOf(wildcard); |
235 |
19 |
if (lastFileSeparatorIndex > wildcard.length() - 1 |
236 |
|
&& wildcardBeforeIndex < lastFileSeparatorIndex) |
237 |
|
{ |
238 |
12 |
path.append(value.substring(0, wildcardBeforeIndex)); |
239 |
12 |
path.append(dirname); |
240 |
12 |
path.append(value.substring(wildcardBeforeIndex + wildcard.length(), |
241 |
|
lastFileSeparatorIndex + 1)); |
242 |
|
} |
243 |
|
else |
244 |
|
{ |
245 |
7 |
path.append(value.substring(0, lastFileSeparatorIndex + 1)); |
246 |
|
} |
247 |
19 |
int wildcardAfterIndex = value.indexOf(wildcard, |
248 |
|
lastFileSeparatorIndex); |
249 |
19 |
if (wildcardAfterIndex > lastFileSeparatorIndex) |
250 |
|
{ |
251 |
14 |
path.append(value.substring(lastFileSeparatorIndex + 1, |
252 |
|
wildcardAfterIndex)); |
253 |
14 |
path.append(basename); |
254 |
14 |
path.append(value.substring(wildcardAfterIndex + wildcard.length())); |
255 |
|
} |
256 |
|
else |
257 |
|
{ |
258 |
5 |
path.append(value.substring(lastFileSeparatorIndex + 1)); |
259 |
|
} |
260 |
19 |
return path.toString(); |
261 |
|
} |
262 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
263 |
114 |
public static File getParentDir(File file)... |
264 |
|
{ |
265 |
114 |
if (file == null) |
266 |
|
{ |
267 |
0 |
return null; |
268 |
|
} |
269 |
114 |
File parentDir = file.getAbsoluteFile().getParentFile(); |
270 |
114 |
return parentDir; |
271 |
|
} |
272 |
|
|
|
|
| 70.8% |
Uncovered Elements: 7 (24) |
Complexity: 6 |
Complexity Density: 0.43 |
|
273 |
114 |
public static boolean checkParentDir(File file, boolean mkdirs)... |
274 |
|
{ |
275 |
114 |
if (file == null) |
276 |
|
{ |
277 |
0 |
return false; |
278 |
|
} |
279 |
114 |
File parentDir = getParentDir(file); |
280 |
114 |
if (parentDir.exists()) |
281 |
|
{ |
282 |
|
|
283 |
112 |
return true; |
284 |
|
} |
285 |
|
|
286 |
2 |
if (!mkdirs) |
287 |
|
{ |
288 |
0 |
return false; |
289 |
|
} |
290 |
|
|
291 |
2 |
Path path = file.toPath(); |
292 |
20 |
for (int i = 0; i < path.getNameCount(); i++) |
293 |
|
{ |
294 |
18 |
Path p = path.getName(i); |
295 |
18 |
if ("..".equals(p.toString())) |
296 |
|
{ |
297 |
0 |
Console.warn("Cautiously not running mkdirs on " + file.toString() |
298 |
|
+ " because the path to be made contains '..'"); |
299 |
0 |
return false; |
300 |
|
} |
301 |
|
} |
302 |
|
|
303 |
2 |
return parentDir.mkdirs(); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
@param |
310 |
|
|
311 |
|
@return |
312 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
313 |
15 |
public static String getExtension(String filename)... |
314 |
|
{ |
315 |
15 |
return getBaseOrExtension(filename, true); |
316 |
|
} |
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
@param |
326 |
|
@return |
327 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
328 |
15 |
public static String getBase(String filename)... |
329 |
|
{ |
330 |
15 |
return getBaseOrExtension(filename, false); |
331 |
|
} |
332 |
|
|
|
|
| 86.7% |
Uncovered Elements: 4 (30) |
Complexity: 9 |
Complexity Density: 0.5 |
|
333 |
30 |
public static String getBaseOrExtension(String filename0,... |
334 |
|
boolean extension) |
335 |
|
{ |
336 |
30 |
if (filename0 == null) |
337 |
|
{ |
338 |
0 |
return null; |
339 |
|
} |
340 |
30 |
String filename = filename0; |
341 |
30 |
boolean isUrl = false; |
342 |
30 |
if (HttpUtils.startsWithHttpOrHttps(filename)) |
343 |
|
{ |
344 |
10 |
try |
345 |
|
{ |
346 |
10 |
URL url = new URL(filename); |
347 |
10 |
filename = url.getPath(); |
348 |
10 |
isUrl = true; |
349 |
|
} catch (MalformedURLException e) |
350 |
|
{ |
351 |
|
|
352 |
|
} |
353 |
|
} |
354 |
30 |
int dot = filename.lastIndexOf('.'); |
355 |
30 |
int slash = filename.lastIndexOf('/'); |
356 |
30 |
if (!File.separator.equals("/") && !isUrl) |
357 |
|
{ |
358 |
0 |
slash = filename.lastIndexOf(File.separator); |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
30 |
boolean hasExtension = dot > slash + 1; |
363 |
30 |
if (extension) |
364 |
|
{ |
365 |
15 |
return hasExtension ? filename.substring(dot + 1) : null; |
366 |
|
} |
367 |
|
else |
368 |
|
{ |
369 |
15 |
dot = filename0.lastIndexOf('.'); |
370 |
15 |
return hasExtension ? filename0.substring(0, dot + 1) : filename0; |
371 |
|
} |
372 |
|
} |
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
@param |
385 |
|
@param |
386 |
|
@param |
387 |
|
@param |
388 |
|
@param |
389 |
|
@return |
390 |
|
|
|
|
| 93.4% |
Uncovered Elements: 5 (76) |
Complexity: 14 |
Complexity Density: 0.26 |
|
391 |
7 |
public static List<File> getMatchingVersionedFiles(String[] templates,... |
392 |
|
String[] roots, String[] versionWhitelist, |
393 |
|
String[] versionBlacklist, String versionSeparator, |
394 |
|
boolean exists) |
395 |
|
{ |
396 |
7 |
Set<File> matchingFiles = new HashSet<>(); |
397 |
7 |
if (templates == null) |
398 |
|
{ |
399 |
0 |
Console.debug( |
400 |
|
"getMatchingVersionedFiles called with a null template array"); |
401 |
0 |
List<File> files = new ArrayList<File>(); |
402 |
0 |
files.addAll(matchingFiles); |
403 |
0 |
return files; |
404 |
|
} |
405 |
|
|
406 |
7 |
for (String template : templates) |
407 |
|
{ |
408 |
7 |
Console.debug("Using template '" + template + "'"); |
409 |
7 |
for (String root : roots) |
410 |
|
{ |
411 |
14 |
Console.debug("Using root '" + root + "'"); |
412 |
|
|
413 |
|
|
414 |
14 |
if (versionBlacklist != null) |
415 |
|
{ |
416 |
10 |
String globMatch = String.format(template, root, "*"); |
417 |
10 |
Console.debug("Using glob '" + globMatch + "'"); |
418 |
10 |
List<File> foundFiles = FileUtils.getFilesFromGlob(globMatch, |
419 |
|
false); |
420 |
10 |
for (File found : foundFiles) |
421 |
|
{ |
422 |
15 |
Console.debug("Checking " + found.getPath() + " is okay"); |
423 |
15 |
boolean add = true; |
424 |
15 |
for (String notVersion : versionBlacklist) |
425 |
|
{ |
426 |
20 |
StringBuilder vSB = new StringBuilder(); |
427 |
20 |
if (versionSeparator != null) |
428 |
|
{ |
429 |
15 |
vSB.append(versionSeparator); |
430 |
|
} |
431 |
20 |
vSB.append(notVersion); |
432 |
20 |
String versionString = vSB.toString(); |
433 |
20 |
if (String.format(template, root, versionString) |
434 |
|
.equals(found.getPath())) |
435 |
|
{ |
436 |
8 |
add = false; |
437 |
8 |
Console.debug( |
438 |
|
"Not adding " + found.getPath() + ": version '" |
439 |
|
+ notVersion + "' is in the blacklist"); |
440 |
8 |
break; |
441 |
|
} |
442 |
|
} |
443 |
15 |
if (add) |
444 |
|
{ |
445 |
7 |
Console.debug("Adding " + found.getPath() + " to list"); |
446 |
7 |
matchingFiles.add(found); |
447 |
|
} |
448 |
|
} |
449 |
|
|
450 |
10 |
if (versionSeparator != null) |
451 |
|
{ |
452 |
|
|
453 |
8 |
String nonVersioned = String.format(template, root, ""); |
454 |
8 |
matchingFiles.addAll( |
455 |
|
FileUtils.getFilesFromGlob(nonVersioned, false)); |
456 |
|
} |
457 |
|
} |
458 |
|
|
459 |
|
|
460 |
|
|
461 |
14 |
if (versionWhitelist != null) |
462 |
|
{ |
463 |
10 |
Console.debug("Adding " + versionWhitelist.length |
464 |
|
+ " whitelist versions"); |
465 |
10 |
for (String addVersion : versionWhitelist) |
466 |
|
{ |
467 |
10 |
StringBuilder vSB = new StringBuilder(); |
468 |
10 |
if (versionSeparator != null) |
469 |
|
{ |
470 |
8 |
vSB.append(versionSeparator); |
471 |
|
} |
472 |
10 |
vSB.append(addVersion); |
473 |
10 |
String versionString = vSB.toString(); |
474 |
10 |
String versionPath = String.format(template, root, |
475 |
|
versionString); |
476 |
10 |
Console.debug("Adding whitelist path '" + versionPath + "'"); |
477 |
10 |
File file = new File(versionPath); |
478 |
10 |
if (file.exists() || !exists) |
479 |
|
{ |
480 |
6 |
matchingFiles.add(file); |
481 |
|
} |
482 |
|
} |
483 |
|
|
484 |
10 |
if (versionSeparator != null) |
485 |
|
{ |
486 |
|
|
487 |
8 |
String nonVersioned = String.format(template, root, ""); |
488 |
8 |
File file = new File(nonVersioned); |
489 |
8 |
if (file.exists() || !exists) |
490 |
|
{ |
491 |
2 |
matchingFiles.add(file); |
492 |
|
} |
493 |
|
} |
494 |
|
} |
495 |
|
} |
496 |
|
} |
497 |
|
|
498 |
7 |
List<File> files = new ArrayList<File>(); |
499 |
7 |
files.addAll(matchingFiles); |
500 |
7 |
return files; |
501 |
|
} |
502 |
|
|
503 |
|
} |