Skip to content
Snippets Groups Projects
Commit d6716716 authored by Wei Liu's avatar Wei Liu
Browse files

enhance --level option to accept number range such as 1-3

parent 9e2e469d
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@
<groupId>au.edu.unimelb.mf</groupId>
<artifactId>unimelb-mf-clients</artifactId>
<version>0.7.0</version>
<version>0.7.1</version>
<packaging>jar</packaging>
<name>unimelb-mf-clients</name>
<url>https://gitlab.unimelb.edu.au/resplat-mediaflux/unimelb-mf-clients</url>
......
......@@ -54,8 +54,8 @@ public class InstrumentUploadMissingFind implements MFCommand {
private String token;
@Option(names = { "-l",
"--level" }, paramLabel = "<n>", description = "Level of sub-directories to search. Defaults to 1.", defaultValue = "1")
private int level;
"--level" }, paramLabel = "<n>", description = "Level ( or level range) of sub-directories to search. For example, use 1-3 to specify the range from level 1 to level 3. Defaults to 1.", defaultValue = "1", converter = LevelRange.Converter.class)
private LevelRange level;
@Option(names = { "-a",
"--after" }, paramLabel = "<dd-MMM-yyyy>", description = "Include only the data directories modified after this date (inclusive).", converter = DateTimeConverter.class)
......@@ -102,9 +102,9 @@ public class InstrumentUploadMissingFind implements MFCommand {
if (!settings.hasShareableIdOrToken()) {
throw new IllegalArgumentException("No shareable id or token is specified!");
}
if (this.level > 0) {
settings.setLevel(this.level);
}
settings.setLevel(this.level);
if (this.modifiedAfter != null) {
settings.setAfter(this.modifiedAfter);
}
......@@ -222,8 +222,8 @@ public class InstrumentUploadMissingFind implements MFCommand {
Set<LocalDirectory> mismatchDirs = mismatchDirectories.keySet();
for (LocalDirectory mismatchDir : mismatchDirs) {
Upload upload = mismatchDirectories.get(mismatchDir);
ps.printf("\"%s\",%d,%d,\"%s\",%d,%d,%n", mismatchDir.path, mismatchDir.nbFiles, mismatchDir.totalSize,
upload.namespace(), upload.totalFileCount(), upload.totalFileSize());
ps.printf("\"%s\",%d,%d,\"%s\",%d,%d,%n", mismatchDir.path, mismatchDir.nbFiles,
mismatchDir.totalSize, upload.namespace(), upload.totalFileCount(), upload.totalFileSize());
totalLocalFileCount += mismatchDir.nbFiles;
totalLocalFileSize += mismatchDir.totalSize;
totalUploadFileCount += upload.totalFileCount();
......@@ -238,16 +238,17 @@ public class InstrumentUploadMissingFind implements MFCommand {
}
}
private static void checkDirectories(Collection<Path> parentDirectories, int levels, Date after, Date before,
private static void checkDirectories(Collection<Path> parentDirectories, LevelRange level, Date after, Date before,
List<Upload> uploads, Set<LocalDirectory> missingDirs, Map<LocalDirectory, Upload> mismatchDirs)
throws Throwable {
for (Path parentDir : parentDirectories) {
checkDirectories(parentDir, levels, after, before, uploads, missingDirs, mismatchDirs);
checkDirectories(parentDir, level, after, before, uploads, missingDirs, mismatchDirs);
}
}
private static void checkDirectories(Path parentDir, int level, Date after, Date before, List<Upload> uploads,
Set<LocalDirectory> missingDirs, Map<LocalDirectory, Upload> mismatchDirs) throws IOException {
private static void checkDirectories(Path parentDir, LevelRange level, Date after, Date before,
List<Upload> uploads, Set<LocalDirectory> missingDirs, Map<LocalDirectory, Upload> mismatchDirs)
throws IOException {
int rootDepth = parentDir.getNameCount();
......@@ -258,10 +259,10 @@ public class InstrumentUploadMissingFind implements MFCommand {
Objects.requireNonNull(dir);
Objects.requireNonNull(attrs);
int depth = dir.getNameCount();
if (depth > rootDepth + level) {
if (depth > rootDepth + level.max) {
return FileVisitResult.SKIP_SUBTREE;
}
if (depth == rootDepth + level) {
if (depth >= rootDepth + level.min) {
if (lastModifiedTimeMatches(attrs.lastModifiedTime(), after, before)) {
checkDirectory(dir, uploads, missingDirs, mismatchDirs);
}
......@@ -317,7 +318,7 @@ public class InstrumentUploadMissingFind implements MFCommand {
public static class Settings {
private Long _id;
private String _token;
private int _level;
private LevelRange _level;
private Date _after;
private Date _before;
private Set<Path> _parentDirs = new LinkedHashSet<>();
......@@ -331,7 +332,7 @@ public class InstrumentUploadMissingFind implements MFCommand {
return _token;
}
int level() {
LevelRange level() {
return _level;
}
......@@ -361,7 +362,7 @@ public class InstrumentUploadMissingFind implements MFCommand {
return this;
}
public Settings setLevel(int level) {
public Settings setLevel(LevelRange level) {
_level = level;
return this;
}
......@@ -452,6 +453,49 @@ public class InstrumentUploadMissingFind implements MFCommand {
}
}
private static class LevelRange {
public static final int MIN_LEVEL = 1;
public static final int MAX_LEVEL = 255;
public final int min;
public final int max;
LevelRange(int min, int max) {
this.min = min;
this.max = max;
}
private static class Converter implements CommandLine.ITypeConverter<LevelRange> {
@Override
public LevelRange convert(String value) throws Exception {
String v = value.trim();
if (v.matches("^\\d+$")) {
int n = Integer.parseInt(v);
if (n < MIN_LEVEL || n > MAX_LEVEL) {
throw new IllegalArgumentException(
"invalid level: " + value + ". Expects integer between 1 and 255, found " + n);
}
return new LevelRange(n, n);
} else if (v.matches("^\\d+-\\d+$")) {
int idx = v.indexOf('-');
int min = Integer.parseInt(v.substring(0, idx));
if (min < MIN_LEVEL || min > MAX_LEVEL) {
throw new IllegalArgumentException(
"invalid level: " + value + ". Expects integer between 1 and 255, found " + min);
}
int max = Integer.parseInt(v.substring(idx + 1));
if (max < MIN_LEVEL || max > MAX_LEVEL) {
throw new IllegalArgumentException(
"invalid level: " + value + ". Expects integer between 1 and 255, found " + max);
}
return new LevelRange(min, max);
} else {
throw new IllegalArgumentException("invalid level: '" + value + "'");
}
}
}
}
public static void main(String[] args) {
CommandLine cl = new CommandLine(new InstrumentUploadMissingFind());
cl.setExecutionExceptionHandler(new IExecutionExceptionHandler() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment