Skip to content

Instantly share code, notes, and snippets.

@peterkir
Last active April 15, 2016 07:04
Show Gist options
  • Save peterkir/83fa1bc685f104ca9c9a to your computer and use it in GitHub Desktop.
Save peterkir/83fa1bc685f104ca9c9a to your computer and use it in GitHub Desktop.
Java - parse log file, extract filelist and append to each file something
package io.klib.tools.files;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AppendToFile {
private static final String APPENDIX = " ";
private static final Logger log = Logger.getLogger(AppendToFile.class.getName());
static String filenamePattern =
"([a-zA-Z0-9.-_]+[_64|_32]?)_([0-9]*\\.[0-9]*\\.[0-9]*[\\.a-zA-Z0-9-_]+)";
public static void main(String[] args) {
String workspace = "";
String logFile = "";
if (args.length == 2) {
workspace = args[0];
logFile = args[1];
}
else {
System.out.println("usage: <appendToFile> <pathToBuildLogFile> <pathToWorkspace>");
System.exit(0);
}
String content;
Path path;
try {
content = new String(Files.readAllBytes(Paths.get(logFile)));
Pattern p =
Pattern.compile(".*?\\[java\\] ### files with lower versions \\[TROUBLE\\]\\s*(.*?)\\[java\\] done\\..*?",
Pattern.DOTALL);
Matcher matcher = p.matcher(content);
if (matcher.find()) {
String troubleSection = matcher.group(1);
String linePatternString = ".*?\\[java\\] file " + filenamePattern + "\\.jar.*";
Pattern filePattern = Pattern.compile(linePatternString);
Matcher fnm = filePattern.matcher(troubleSection);
while (fnm.find()) {
String bundleName = fnm.group(1);
path = Paths.get(workspace, bundleName, ".project");
if (path.toFile().exists()) {
Files.write(path, APPENDIX.getBytes("utf-8"), StandardOpenOption.APPEND);
log.info("project file with path " + path + " -> patched");
}
else {
log.fine("case bnd project, so trying to patch only the bnd bundle descriptor file first");
String bndBundleName = bundleName.substring(0, bundleName.lastIndexOf("."));
String bndFile = bundleName.substring(bundleName.lastIndexOf(".") + 1);
path = Paths.get(workspace, bndBundleName, bndFile + ".bnd");
if (path.toFile().exists()) {
Files.write(path, APPENDIX.getBytes("utf-8"), StandardOpenOption.APPEND);
log.info("bnd file with path " + path + " -> patched");
}
else {
log.fine("case bnd bundles descriptor file not found, hence patching the project file");
path = Paths.get(workspace, bndBundleName, ".project");
if (path.toFile().exists()) {
Files.write(path, APPENDIX.getBytes("utf-8"), StandardOpenOption.APPEND);
log.info("project file with path " + path + " -> patched");
}
else {
log.severe("project " + bundleName + " and parent not found - fix me manually");
}
}
}
}
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment