From a0963e49a9d86e71ec2ccef549be5b9a23fbde3c Mon Sep 17 00:00:00 2001
From: Greg DiCristofaro <gregd@basistech.com>
Date: Tue, 29 Aug 2023 13:31:13 -0400
Subject: [PATCH] work towards module updates

---
 release_scripts/APIUpdate/pom.xml             |   4 +-
 .../sleuthkit/autopsy/apiupdate/APIDiff.java  |   4 +-
 .../org/sleuthkit/autopsy/apiupdate/Main.java |  16 ++-
 .../autopsy/apiupdate/ModuleUpdates.java      | 121 +++++++++---------
 4 files changed, 81 insertions(+), 64 deletions(-)

diff --git a/release_scripts/APIUpdate/pom.xml b/release_scripts/APIUpdate/pom.xml
index 0feced8364..20efd2b91e 100644
--- a/release_scripts/APIUpdate/pom.xml
+++ b/release_scripts/APIUpdate/pom.xml
@@ -9,7 +9,7 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>17</maven.compiler.source>
         <maven.compiler.target>17</maven.compiler.target>
-        <exec.mainClass>org.sleuthkit.autopsy.classpathsimplication.apiupdate.APIUpdate</exec.mainClass>
+        <exec.mainClass>org.sleuthkit.autopsy.apiupdate.Main</exec.mainClass>
     </properties>
     <dependencies>
         <dependency>
@@ -40,7 +40,7 @@
                 <configuration>
                     <archive>
                         <manifest>
-                            <mainClass>org.sleuthkit.autopsy.apiupdate.Main</mainClass>
+                            <mainClass>${exec.mainClass}</mainClass>
                         </manifest>
                     </archive>
                     <descriptorRefs>
diff --git a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/APIDiff.java b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/APIDiff.java
index 7c73627d81..1ccdd5d82e 100644
--- a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/APIDiff.java
+++ b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/APIDiff.java
@@ -27,7 +27,7 @@ public class APIDiff {
     private static final FileFilter JAR_FILTER
             = (File f) -> f.isFile() && (f.getName().toLowerCase().endsWith(".jar") || f.getName().toLowerCase().endsWith(".nbm"));
 
-    private static List<String> getCommonJars(File prevDir, File currDir) {
+    static List<String> getCommonJars(File prevDir, File currDir) {
         Set<String> prevJars = getJars(prevDir);
         Set<String> currJars = getJars(currDir);
 
@@ -44,7 +44,7 @@ private static Set<String> getJars(File dir) {
                 .collect(Collectors.toSet());
     }
 
-    private static Set<String> getPublicPackages(File jarFile) throws IOException, IllegalStateException {
+    static Set<String> getPublicPackages(File jarFile) throws IOException, IllegalStateException {
         String publicPackageStr = ManifestLoader.loadFromJar(jarFile).getValue("OpenIDE-Module-Public-Packages");
         if (publicPackageStr == null) {
             throw new IllegalStateException(MessageFormat.format("Manifest for {0} does not have key of 'OpenIDE-Module-Public-Packages'", jarFile.getAbsolutePath()));
diff --git a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/Main.java b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/Main.java
index 47ada3e089..71e410cc99 100644
--- a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/Main.java
+++ b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/Main.java
@@ -27,6 +27,7 @@
 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;
 
 /**
  *
@@ -35,7 +36,7 @@
 public class Main {
 
     public static void main(String[] args) {
-        args = "-c C:\\Users\\gregd\\Documents\\Source\\autopsy\\build\\cluster\\modules -p C:\\Users\\gregd\\Desktop\\prevVers -cv 4.21.0 -pv 4.20.0".split(" ");
+        args = "-c C:\\Users\\gregd\\Documents\\Source\\autopsy\\build\\cluster\\modules -p C:\\Users\\gregd\\Desktop\\prevVers -cv 4.21.0 -pv 4.20.0 -s C:\\Users\\gregd\\Documents\\Source\\autopsy".split(" ");
         CLIArgs cliArgs;
         try {
             cliArgs = CLIProcessor.parseCli(args);
@@ -49,6 +50,19 @@ public static void main(String[] args) {
             return;
         }
 
+        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 : getCommonJars(cliArgs.getPreviousVersPath(), cliArgs.getCurrentVersPath())) {
 ////            getComparison(
 ////                    cliArgs.getPreviousVersion(),
diff --git a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java
index 06b3792a8a..40ead8e718 100644
--- a/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java
+++ b/release_scripts/APIUpdate/src/main/java/org/sleuthkit/autopsy/apiupdate/ModuleUpdates.java
@@ -7,12 +7,14 @@
 import java.io.File;
 import java.io.IOException;
 import java.text.MessageFormat;
-import java.text.ParseException;
+import java.util.Map;
 import java.util.jar.Attributes;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.apache.commons.lang3.StringUtils;
 
 /**
@@ -22,79 +24,64 @@
 public class ModuleUpdates {
 
     private static final Logger LOGGER = Logger.getLogger(ModuleUpdates.class.getName());
-//        // Spec
-//            // project.properties
-//                // spec.version.base
-//            // manifest
-//                // OpenIDE-Module-Specification-Version
-//        // Implementation
-//            // manifest
-//                // OpenIDE-Module-Implementation-Version
-//        // Release
-//            // manifest
-//                // OpenIDE-Module (/number)
-//                
-//        // Dependency specification
-//            // project.xml
-//                // project.configuration.data.module-dependencies.dependency.run-dependency:
-//                    // specification-version
-//                    // release-version
 
-    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 IMPL_KEY = "OpenIDE-Module-Implementation-Version";
     private static final String RELEASE_KEY = "OpenIDE-Module";
+    
 
     private static final Pattern RELEASE_REGEX = Pattern.compile("^\\s*(?<releaseName>.+?)(/(?<releaseNum>\\d*))?\\s*$");
 
     private static final SemVer DEFAULT_SEMVER = new SemVer(1, 0, null);
     private static final int DEFAULT_VERS_VAL = 1;
 
+    private static final String AUTOPSY_PREFIX = "org-sleuthkit-autopsy-";
+    
     private static SemVer parseSemVer(String semVerStr, SemVer defaultSemVer, String resourceForLogging) {
         if (StringUtils.isBlank(semVerStr)) {
+            LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse semver for empty string in {0}", resourceForLogging));
             return defaultSemVer;
         }
 
         Matcher m = SPEC_REGEX.matcher(semVerStr);
         if (m.find()) {
             try {
-                int major = Integer.parseInt(m.group("major"));
+                String majorStr = m.group("major");
+                int major = StringUtils.isBlank(majorStr) ? 1 : Integer.parseInt(majorStr);
                 int minor = Integer.parseInt(m.group("minor"));
                 String patchStr = m.group("patch");
                 Integer patch = StringUtils.isBlank(patchStr) ? null : Integer.parseInt(patchStr);
                 return new SemVer(major, minor, patch);
             } catch (NullPointerException | NumberFormatException ex) {
                 LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse semver string {0} for {1}", semVerStr, resourceForLogging), ex);
-                return defaultSemVer;
             }
         } else {
-            return defaultSemVer;
+            LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse semver string {0} for {1}", semVerStr, resourceForLogging));
         }
-    }
 
-    private static ReleaseVal parseReleaseVers(String releaseStr, ReleaseVal defaultVal, String resourceForLogging) {
-        if (StringUtils.isBlank(releaseStr)) {
-            return defaultVal;
-        }
+        return defaultSemVer;
+    }
 
-        Matcher m = RELEASE_REGEX.matcher(releaseStr);
-        if (m.find()) {
+    private static ReleaseVal parseReleaseVers(String releaseStr, String resourceForLogging) {
+        Matcher m = RELEASE_REGEX.matcher(StringUtils.defaultString(releaseStr));
+        if (StringUtils.isNotBlank(releaseStr) && m.find()) {
+            String releaseName = m.group("releaseName");
+            Integer releaseNum = null;
             try {
-                int major = Integer.parseInt(m.group("major"));
-                int minor = Integer.parseInt(m.group("minor"));
-                String patchStr = m.group("patch");
-                Integer patch = StringUtils.isBlank(patchStr) ? null : Integer.parseInt(patchStr);
-                return new SemVer(major, minor, patch);
+                String releaseNumStr = m.group("releaseNum");
+                releaseNum = StringUtils.isBlank(releaseNumStr) ? null : Integer.parseInt(releaseNumStr);
             } catch (NullPointerException | NumberFormatException ex) {
-                LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse semver string {0} for {1}", releaseStr, resourceForLogging), ex);
-                return defaultSemVer;
+                LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse release version string {0} for {1}", releaseStr, resourceForLogging), ex);
             }
+            return new ReleaseVal(releaseName, releaseNum);
         } else {
-            return defaultSemVer;
+            LOGGER.log(Level.SEVERE, MessageFormat.format("Unable to parse release version string {0} for {1}", releaseStr, resourceForLogging));
         }
+
+        return new ReleaseVal("", null);
+
     }
-    
-    
 
     private static int tryParse(String str, int defaultVal, String resourceForLogging) {
         try {
@@ -105,7 +92,7 @@ private static int tryParse(String str, int defaultVal, String resourceForLoggin
         }
     }
 
-    public static SemVer getPrevVersions(File jarFile) throws IOException {
+    public static ModuleVersionNumbers getVersionsFromJar(File jarFile) throws IOException {
         Attributes manifest = ManifestLoader.loadFromJar(jarFile);
         String spec = manifest.getValue(SPEC_KEY);
         SemVer specSemVer = parseSemVer(spec, DEFAULT_SEMVER,
@@ -114,7 +101,32 @@ public static SemVer getPrevVersions(File jarFile) throws IOException {
         int implementation = tryParse(manifest.getValue(IMPL_KEY), DEFAULT_VERS_VAL,
                 MessageFormat.format("{0} in manifest for {1}", IMPL_KEY, jarFile));
 
-//        // Spec
+        ReleaseVal release = parseReleaseVers(manifest.getValue(RELEASE_KEY),
+                MessageFormat.format("{0} in manifest for {1}", RELEASE_KEY, jarFile));
+
+        return new ModuleVersionNumbers(jarFile.getName(), specSemVer, implementation, release);
+    }
+
+    private static void updateVersions() {
+//        [specification major/minor/patch, implementation, release]
+//        assumed defaults???
+//        NON_COMPATIBLE:
+//            specification.major += 1
+//            implementation += 1
+//            release += 1
+//        COMPATIBLE:
+//            specification.minor += 1
+//            implementation += 1
+//        NO_CHANGES:
+//            implementation += 1
+    }
+
+    private static void setVersions(File srcDir, Map<String, ModuleVersionNumbers> versNums) {
+        // TODO parse from repo/DIR/manifest.mf release version
+        Map<String, File> moduleDirs = Stream.of(srcDir.listFiles((File f) -> f.isDirectory()))
+                .filter(f -> versNums.containsKey((AUTOPSY_PREFIX + f.getName()).toLowerCase()))
+                .collect(Collectors.toMap(f -> (AUTOPSY_PREFIX + f.getName()).toLowerCase(), f -> f, (f1, f2) -> f1);
+        //        // Spec
 //            // project.properties
 //                // spec.version.base
 //            // manifest
@@ -133,21 +145,6 @@ public static SemVer getPrevVersions(File jarFile) throws IOException {
 //                    // release-version
     }
 
-    private static void updateVersions() {
-//        [specification major/minor/patch, implementation, release]
-//        assumed defaults???
-//        NON_COMPATIBLE:
-//            specification.major += 1
-//            implementation += 1
-//            release += 1
-//        COMPATIBLE:
-//            specification.minor += 1
-//            implementation += 1
-//        NO_CHANGES:
-//            implementation += 1
-
-    }
-
     public static class ReleaseVal {
 
         private final String moduleName;
@@ -206,16 +203,22 @@ public String getSemVerStr() {
 
     public static class ModuleVersionNumbers {
 
+        private final String jarName;
         private final SemVer spec;
         private final int implementation;
-        private final int release;
+        private final ReleaseVal release;
 
-        public ModuleVersionNumbers(SemVer spec, int implementation, int release) {
+        public ModuleVersionNumbers(String jarName, SemVer spec, int implementation, ReleaseVal release) {
+            this.jarName = jarName;
             this.spec = spec;
             this.implementation = implementation;
             this.release = release;
         }
 
+        public String getJarName() {
+            return jarName;
+        }
+
         public SemVer getSpec() {
             return spec;
         }
@@ -224,7 +227,7 @@ public int getImplementation() {
             return implementation;
         }
 
-        public int getRelease() {
+        public ReleaseVal getRelease() {
             return release;
         }
 
-- 
GitLab