This commit is contained in:
Greg DiCristofaro 2023-08-29 17:13:37 -04:00
parent a6b075f652
commit 00e03e7520
3 changed files with 60 additions and 56 deletions

View File

@ -57,8 +57,8 @@ public class CLIProcessor {
.option("cv")
.required(true)
.build();
static Option SRC_LOC_OPT = Option.builder()
static Option SRC_LOC_OPT = Option.builder()
.argName("path")
.desc("The path to the root of the autopsy repor")
.hasArg(true)
@ -97,8 +97,7 @@ public class CLIProcessor {
static Options HELP_OPTIONS = getCliOptions(Collections.singletonList(HELP_OPT));
private static CommandLineParser parser = new DefaultParser();
private static HelpFormatter helpFormatter = new HelpFormatter();
static void printHelp(Exception ex) {
@ -133,15 +132,14 @@ public class CLIProcessor {
if (!prevVersFile.isDirectory()) {
throw new ParseException("No directory found at " + prevVersFile.getAbsolutePath());
}
if (!srcPathFile.isDirectory()) {
if (!srcPathFile.isDirectory()) {
throw new ParseException("No directory found at " + srcPathFile.getAbsolutePath());
}
return new CLIArgs(curVers, prevVers, curVersFile, prevVersFile, srcPathFile, false);
}
public static class CLIArgs {
private final String currentVersion;
@ -151,6 +149,15 @@ public class CLIProcessor {
private final boolean isHelp;
private final File srcPath;
public CLIArgs(String currentVersion, String previousVersion, File currentVersPath, File previousVersPath, File srcPath, boolean isHelp) {
this.currentVersion = currentVersion;
this.previousVersion = previousVersion;
this.currentVersPath = currentVersPath;
this.previousVersPath = previousVersPath;
this.srcPath = srcPath;
this.isHelp = isHelp;
}
public String getCurrentVersion() {
return currentVersion;
}
@ -171,13 +178,8 @@ public class CLIProcessor {
return isHelp;
}
public CLIArgs(String currentVersion, String previousVersion, File currentVersPath, File previousVersPath, File srcPath, boolean isHelp) {
this.currentVersion = currentVersion;
this.previousVersion = previousVersion;
this.currentVersPath = currentVersPath;
this.previousVersPath = previousVersPath;
this.srcPath = srcPath;
this.isHelp = isHelp;
public File getSrcPath() {
return srcPath;
}
}

View File

@ -3,31 +3,14 @@
*/
package org.sleuthkit.autopsy.apiupdate;
import japicmp.cmp.JApiCmpArchive;
import japicmp.cmp.JarArchiveComparator;
import japicmp.cmp.JarArchiveComparatorOptions;
import japicmp.model.JApiClass;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.cli.ParseException;
import org.sleuthkit.autopsy.apiupdate.CLIProcessor.CLIArgs;
import org.sleuthkit.autopsy.apiupdate.ModuleUpdates.ModuleVersionNumbers;
import org.sleuthkit.autopsy.apiupdate.ModuleUpdates.ReleaseVal;
/**
*
@ -49,17 +32,32 @@ public class Main {
System.exit(-1);
return;
}
Map<String, ModuleVersionNumbers> versNums = Stream.of(
new ModuleVersionNumbers(
"org.sleuthkit.autopsy.core",
new ModuleUpdates.SemVer(1,2,3),
4,
new ReleaseVal("org.sleuthkit.autopsy.core", 5)),
new ModuleVersionNumbers(
"org.sleuthkit.autopsy.corelibs",
new ModuleUpdates.SemVer(6,7,8),
9,
new ReleaseVal("org.sleuthkit.autopsy.corelibs", 10)))
.collect(Collectors.toMap(v -> v.getModuleName(), v -> v, (v1, v2) -> v1));
ModuleUpdates.setVersions(cliArgs.getSrcPath(), versNums);
for (String commonJarFileName : APIDiff.getCommonJars(cliArgs.getPreviousVersPath(), cliArgs.getCurrentVersPath())) {
try {
ModuleVersionNumbers m = ModuleUpdates.getVersionsFromJar(cliArgs.getPreviousVersPath().toPath().resolve(commonJarFileName).toFile());
System.out.println(MessageFormat.format("release: {0}, spec: {1}, implementation: {2}", m.getRelease().getFullReleaseStr(), m.getSpec().getSemVerStr(), m.getImplementation()));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
// for (String commonJarFileName : APIDiff.getCommonJars(cliArgs.getPreviousVersPath(), cliArgs.getCurrentVersPath())) {
// try {
// ModuleVersionNumbers m = ModuleUpdates.getVersionsFromJar(cliArgs.getPreviousVersPath().toPath().resolve(commonJarFileName).toFile());
// System.out.println(MessageFormat.format("release: {0}, spec: {1}, implementation: {2}", m.getRelease().getFullReleaseStr(), m.getSpec().getSemVerStr(), m.getImplementation()));
//
// } catch (IOException ex) {
// Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
// }
//
// }

View File

@ -16,12 +16,13 @@ import java.util.Properties;
import java.util.jar.Attributes;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
@ -44,6 +45,9 @@ import org.xml.sax.SAXException;
public class ModuleUpdates {
private static final Logger LOGGER = Logger.getLogger(ModuleUpdates.class.getName());
static {
LOGGER.addHandler(new StreamHandler(System.out, new SimpleFormatter()));
}
private static final Pattern SPEC_REGEX = Pattern.compile("^\\s*((?<major>\\d*)\\.)?(?<minor>\\d*)(\\.(?<patch>\\d*))?\\s*$");
private static final String SPEC_KEY = "OpenIDE-Module-Specification-Version";
@ -60,7 +64,7 @@ public class ModuleUpdates {
private static final String PROJ_XML_REL_PATH = "nbproject/project.xml";
private static final String MANIFEST_FILE_NAME = "manifest.mf";
private static final String PROJ_XML_FMT_STR = "//project/configuration/data/module-dependencies/dependency[code-name-base[contains(text(), '{0}')]]/run-dependency";
private static final String PROJ_XML_FMT_STR = "//project/configuration/data/module-dependencies/dependency[code-name-base[contains(text(), ''{0}'')]]/run-dependency";
private static final String PROJ_XML_RELEASE_VERS_EL = "release-version";
private static final String PROJ_XML_SPEC_VERS_EL = "specification-version";
private static final String PROJ_XML_IMPL_VERS_EL = "implementation-version";
@ -225,17 +229,17 @@ public class ModuleUpdates {
attributes = ManifestLoader.loadInputStream(manifestIs);
}
updateAttr(attributes, IMPL_KEY, thisVersNums.getImplementation(), true);
updateAttr(attributes, IMPL_KEY, Integer.toString(thisVersNums.getImplementation()), true);
updateAttr(attributes, SPEC_KEY, thisVersNums.getSpec().getSemVerStr(), true);
updateAttr(attributes, RELEASE_KEY, thisVersNums.getRelease().getFullReleaseStr(), true);
}
private static void updateAttr(Attributes attributes, String key, Object val, boolean updateOnlyIfPresent) {
private static void updateAttr(Attributes attributes, String key, String val, boolean updateOnlyIfPresent) {
if (updateOnlyIfPresent && attributes.getValue(key) == null) {
return;
}
attributes.put(key, val);
attributes.putValue(key, val);
}
private static void updateProjXml(File moduleDir, Map<String, ModuleVersionNumbers> versNums)
@ -277,7 +281,7 @@ public class ModuleUpdates {
Transformer transformer = transformerFactory.newTransformer();
// pretty print XML
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(projectXmlDoc);
try (FileOutputStream xmlOut = new FileOutputStream(projXmlFile)) {
@ -298,7 +302,7 @@ public class ModuleUpdates {
String newChildNodeText = childElText.get(childNodeEl);
if (newChildNodeText != null && StringUtils.isNotBlank(childNodeText)) {
childNode.setPrefix(newChildNodeText);
childNode.setTextContent(newChildNodeText);
changed = true;
}
}
@ -381,20 +385,20 @@ public class ModuleUpdates {
public static class ModuleVersionNumbers {
private final String jarName;
private final String moduleName;
private final SemVer spec;
private final int implementation;
private final ReleaseVal release;
public ModuleVersionNumbers(String jarName, SemVer spec, int implementation, ReleaseVal release) {
this.jarName = jarName;
public ModuleVersionNumbers(String moduleName, SemVer spec, int implementation, ReleaseVal release) {
this.moduleName = moduleName;
this.spec = spec;
this.implementation = implementation;
this.release = release;
}
public String getJarName() {
return jarName;
public String getModuleName() {
return moduleName;
}
public SemVer getSpec() {