Fix a few exceptions uncovered while runnning on the large corpus

parent 0bf0effa
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
<dependency> <dependency>
<groupId>info.picocli</groupId> <groupId>info.picocli</groupId>
<artifactId>picocli</artifactId> <artifactId>picocli</artifactId>
<version>4.0.0-alpha-3</version> <version>4.0.0-beta-1b</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
......
package org.aminb.alloy.catalyst; package org.aminb.alloy.catalyst;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.nio.file.Files; import java.nio.file.Files;
...@@ -102,10 +103,13 @@ public final class SSR extends CatalystCommand implements Runnable { ...@@ -102,10 +103,13 @@ public final class SSR extends CatalystCommand implements Runnable {
corpus, cr.elapsed_time)); corpus, cr.elapsed_time));
long ssrs = 0, ssrc = 0; long ssrs = 0, ssrc = 0;
for (FileResult fr : cr.file_results) { for (FileResult fr : cr.file_results)
ssrs += fr.ssr_result.sig_count; if (fr.ssr_result != null) { // happens when there are
ssrc += fr.ssr_result.ssr_candidate_count; // errors in analyzeFile
} // and it exits early
ssrs += fr.ssr_result.sig_count;
ssrc += fr.ssr_result.ssr_candidate_count;
}
pln(String.format("SSR candidates: %d/%d (%.2f%%)", pln(String.format("SSR candidates: %d/%d (%.2f%%)",
ssrc, ssrs, 100. * ssrc / ssrs)); ssrc, ssrs, 100. * ssrc / ssrs));
cr.ssr_result = new SSRResult(); cr.ssr_result = new SSRResult();
...@@ -124,11 +128,30 @@ public final class SSR extends CatalystCommand implements Runnable { ...@@ -124,11 +128,30 @@ public final class SSR extends CatalystCommand implements Runnable {
fr.mutation_results = new ArrayList<MutationResult>(); fr.mutation_results = new ArrayList<MutationResult>();
fr.errors = new ArrayList<ParseOrTypeError>(); fr.errors = new ArrayList<ParseOrTypeError>();
Path outFile = Paths.get(outDir, corpus, fr.file_name.replace("/", outSep));
Writer w = setupWriter(outFile, true);
String progress = String.format("[%d/%d]: ", index, total); String progress = String.format("[%d/%d]: ", index, total);
Path prefix = Paths.get(outDir, corpus);
Path fName;
try {
int diff = outSep.length() - 1 /* length of separatorChar */;
int wiggleRoom =
prefix.toString().length() + 1 /* one joining separatorChar */ +
Utils.countMatches(fr.file_name, File.separatorChar) * diff;
fName = Utils.shortenPathIfNeeded(Paths.get(fr.file_name), 255, wiggleRoom);
}
catch (PathTooLongException e) {
String
msg = "Error in creating output json file:",
err = Utils.toString(e);
pln(progress + "error: " + fr.file_name);
fr.errors.add(new ParseOrTypeError(String.format("%s%n%s", msg, err), true));
errorCount.getAndIncrement();
return fr;
}
Path outFile = Paths.get(prefix.toString(),
fName.toString().replace(File.separator, outSep));
Writer w = setupWriter(outFile, true);
A4Reporter rep = new A4Reporter() { A4Reporter rep = new A4Reporter() {
@Override @Override
public void warning(ErrorWarning msg) { public void warning(ErrorWarning msg) {
......
package org.aminb.alloy.catalyst; package org.aminb.alloy.catalyst;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256; import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_256;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
...@@ -10,8 +14,12 @@ public class Utils { ...@@ -10,8 +14,12 @@ public class Utils {
return new org.apache.commons.codec.binary.Base32().encodeAsString(new DigestUtils(SHA_256).digest(s)); return new org.apache.commons.codec.binary.Base32().encodeAsString(new DigestUtils(SHA_256).digest(s));
} }
public static String sha256_n(String s, int n) {
return sha256(s).substring(0, n).toLowerCase();
}
public static String sha256_32(String s) { public static String sha256_32(String s) {
return sha256(s).substring(0, 32).toLowerCase(); return sha256_n(s, 32);
} }
public static RuntimeException systemExit(int status) { public static RuntimeException systemExit(int status) {
...@@ -36,4 +44,43 @@ public class Utils { ...@@ -36,4 +44,43 @@ public class Utils {
} }
return sb.toString().trim(); return sb.toString().trim();
} }
public static int countMatches(String s, char c) {
if (s.isEmpty())
return 0;
int count = 0;
for (int i = 0; i < s.length(); i++)
if (c == s.charAt(i))
count++;
return count;
}
public static Path shortenPathIfNeeded(Path p, int max, int wiggleRoom)
throws PathTooLongException {
if (p.toString().length() > max - wiggleRoom) {
Path parent = p;
for (int i = 0; i < countMatches(p.toString(), File.separatorChar); i++) {
parent = parent.getParent();
String parentName = parent.getFileName().toString();
if (parentName.length() > 16)
p = Paths.get(p.toString()
.replace(parentName, sha256_n(parentName, 16)));
if (p.toString().length() <= max - wiggleRoom)
return p;
}
throw new PathTooLongException("Path too long and/or too deeply nested");
}
else
return p;
}
}
class PathTooLongException extends Exception {
public PathTooLongException(String msg) {
super(msg);
}
public PathTooLongException(String msg, Throwable err) {
super(msg, err);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment