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

View File

@ -3,31 +3,14 @@
*/ */
package org.sleuthkit.autopsy.apiupdate; package org.sleuthkit.autopsy.apiupdate;
import japicmp.cmp.JApiCmpArchive; import java.util.HashMap;
import japicmp.cmp.JarArchiveComparator; import java.util.Map;
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.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.cli.ParseException; import org.apache.commons.cli.ParseException;
import org.sleuthkit.autopsy.apiupdate.CLIProcessor.CLIArgs; import org.sleuthkit.autopsy.apiupdate.CLIProcessor.CLIArgs;
import org.sleuthkit.autopsy.apiupdate.ModuleUpdates.ModuleVersionNumbers; import org.sleuthkit.autopsy.apiupdate.ModuleUpdates.ModuleVersionNumbers;
import org.sleuthkit.autopsy.apiupdate.ModuleUpdates.ReleaseVal;
/** /**
* *
@ -49,17 +32,32 @@ public class Main {
System.exit(-1); System.exit(-1);
return; 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())) { // for (String commonJarFileName : APIDiff.getCommonJars(cliArgs.getPreviousVersPath(), cliArgs.getCurrentVersPath())) {
try { // try {
ModuleVersionNumbers m = ModuleUpdates.getVersionsFromJar(cliArgs.getPreviousVersPath().toPath().resolve(commonJarFileName).toFile()); // 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())); // System.out.println(MessageFormat.format("release: {0}, spec: {1}, implementation: {2}", m.getRelease().getFullReleaseStr(), m.getSpec().getSemVerStr(), m.getImplementation()));
//
} catch (IOException ex) { // } catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, 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.jar.Attributes;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
@ -44,6 +45,9 @@ import org.xml.sax.SAXException;
public class ModuleUpdates { public class ModuleUpdates {
private static final Logger LOGGER = Logger.getLogger(ModuleUpdates.class.getName()); 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 Pattern SPEC_REGEX = Pattern.compile("^\\s*((?<major>\\d*)\\.)?(?<minor>\\d*)(\\.(?<patch>\\d*))?\\s*$");
private static final String SPEC_KEY = "OpenIDE-Module-Specification-Version"; 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 PROJ_XML_REL_PATH = "nbproject/project.xml";
private static final String MANIFEST_FILE_NAME = "manifest.mf"; 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_RELEASE_VERS_EL = "release-version";
private static final String PROJ_XML_SPEC_VERS_EL = "specification-version"; private static final String PROJ_XML_SPEC_VERS_EL = "specification-version";
private static final String PROJ_XML_IMPL_VERS_EL = "implementation-version"; private static final String PROJ_XML_IMPL_VERS_EL = "implementation-version";
@ -225,17 +229,17 @@ public class ModuleUpdates {
attributes = ManifestLoader.loadInputStream(manifestIs); 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, SPEC_KEY, thisVersNums.getSpec().getSemVerStr(), true);
updateAttr(attributes, RELEASE_KEY, thisVersNums.getRelease().getFullReleaseStr(), 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) { if (updateOnlyIfPresent && attributes.getValue(key) == null) {
return; return;
} }
attributes.put(key, val); attributes.putValue(key, val);
} }
private static void updateProjXml(File moduleDir, Map<String, ModuleVersionNumbers> versNums) private static void updateProjXml(File moduleDir, Map<String, ModuleVersionNumbers> versNums)
@ -277,7 +281,7 @@ public class ModuleUpdates {
Transformer transformer = transformerFactory.newTransformer(); Transformer transformer = transformerFactory.newTransformer();
// pretty print XML // pretty print XML
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(projectXmlDoc); DOMSource source = new DOMSource(projectXmlDoc);
try (FileOutputStream xmlOut = new FileOutputStream(projXmlFile)) { try (FileOutputStream xmlOut = new FileOutputStream(projXmlFile)) {
@ -298,7 +302,7 @@ public class ModuleUpdates {
String newChildNodeText = childElText.get(childNodeEl); String newChildNodeText = childElText.get(childNodeEl);
if (newChildNodeText != null && StringUtils.isNotBlank(childNodeText)) { if (newChildNodeText != null && StringUtils.isNotBlank(childNodeText)) {
childNode.setPrefix(newChildNodeText); childNode.setTextContent(newChildNodeText);
changed = true; changed = true;
} }
} }
@ -381,20 +385,20 @@ public class ModuleUpdates {
public static class ModuleVersionNumbers { public static class ModuleVersionNumbers {
private final String jarName; private final String moduleName;
private final SemVer spec; private final SemVer spec;
private final int implementation; private final int implementation;
private final ReleaseVal release; private final ReleaseVal release;
public ModuleVersionNumbers(String jarName, SemVer spec, int implementation, ReleaseVal release) { public ModuleVersionNumbers(String moduleName, SemVer spec, int implementation, ReleaseVal release) {
this.jarName = jarName; this.moduleName = moduleName;
this.spec = spec; this.spec = spec;
this.implementation = implementation; this.implementation = implementation;
this.release = release; this.release = release;
} }
public String getJarName() { public String getModuleName() {
return jarName; return moduleName;
} }
public SemVer getSpec() { public SemVer getSpec() {