From 6eea344a264c908146d6105c8647637f5e27bb08 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Wed, 26 Jan 2022 16:46:16 -0800 Subject: [PATCH 1/2] Enhance CompareSpdxDocs to take a directory of SPDX files as input Signed-off-by: Gary O'Neall --- README.md | 10 ++- .../java/org/spdx/tools/CompareSpdxDocs.java | 90 ++++++++++++------- 2 files changed, 68 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index ced05aa..55dee33 100644 --- a/README.md +++ b/README.md @@ -56,11 +56,17 @@ The following tools can be used to compare one or more SPDX documents: java -jar spdx-tools-jar-with-dependencies.jar CompareSpdxDocs doc1 doc2 [output] - * CompareMultipleSpdxDocs + * CompareMultipleSpdxDocs with files Example to compare multiple SPDX files provided in rdf format and provide a spreadsheet with the results: - java -jar spdx-tools-jar-with-dependencies.jar CompareMultipleSpdxDocs output.xls doc1 doc2 ... docN + java -jar spdx-tools-jar-with-dependencies.jar CompareMultipleSpdxDocs output.xlsx doc1 doc2 ... docN + + * CompareMultipleSpdxDocs with directory + + Example to compare all SPDX documents in a directory "/home/me/spdxdocs" and provide a spreadsheet with the results: + + java -jar spdx-tools-jar-with-dependencies.jar CompareMultipleSpdxDocs output.xlsx /home/me/spdxdocs ## SPDX Viewer The following tool can be used to "Pretty Print" an SPDX document. diff --git a/src/main/java/org/spdx/tools/CompareSpdxDocs.java b/src/main/java/org/spdx/tools/CompareSpdxDocs.java index 79f5a8c..2359bed 100644 --- a/src/main/java/org/spdx/tools/CompareSpdxDocs.java +++ b/src/main/java/org/spdx/tools/CompareSpdxDocs.java @@ -18,6 +18,7 @@ package org.spdx.tools; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -33,15 +34,16 @@ /** * Compares multiple SPDX documents and stores the results in a spreadsheet - * Usage: CompareSpdxDoc output.xls doc1 doc2 doc3 ... docN + * Usage: CompareSpdxDoc output.xlsx doc1 doc2 doc3 ... docN * where output.xls is a file name for the output spreadsheet file - * and docX are SPDX document files to compare. Document files can be either in RDF/XML or tag/value format + * and docX are SPDX document files to compare or directories containing SPDX documents. + * Document files can be either in RDF/XML or tag/value format * * @author Gary O'Neall * */ public class CompareSpdxDocs { - static final int MIN_ARGS = 3; + static final int MIN_ARGS = 2; static final int MAX_ARGS = MultiDocumentSpreadsheet.MAX_DOCUMENTS + 1; static final int ERROR_STATUS = 1; static final Logger logger = LoggerFactory.getLogger(CompareSpdxDocs.class); @@ -83,29 +85,22 @@ public static void onlineFunction(String[] args) throws OnlineToolException { } List compareDocs = new ArrayList<>(); List> verificationErrors = new ArrayList<>(); - + List docNames = new ArrayList<>(); for (int i = 1; i < args.length; i++) { try { - SpdxDocument doc = SpdxToolsHelper.deserializeDocument(new File(args[i])); - compareDocs.add(doc); - List warnings = doc.verify(); - if (!warnings.isEmpty()) { - System.out.println("Verification errors were found in "+args[i].trim()+". See verification errors sheet for details."); - } - verificationErrors.add(warnings); + addDocToComparer(compareDocs, args[i], docNames, verificationErrors); } catch (InvalidSPDXAnalysisException | IOException | InvalidFileNameException e) { throw new OnlineToolException("Error opening SPDX document "+args[i]+": "+e.getMessage()); } } - - List docNames = convertToDocNames(args, 1); + List normalizedDocNames = normalizeDocNames(docNames); MultiDocumentSpreadsheet outSheet = null; try { outSheet = new MultiDocumentSpreadsheet(outputFile, true, false); - outSheet.importVerificationErrors(verificationErrors, docNames); + outSheet.importVerificationErrors(verificationErrors, normalizedDocNames); SpdxComparer comparer = new SpdxComparer(); comparer.compare(compareDocs); - outSheet.importCompareResults(comparer, docNames); + outSheet.importCompareResults(comparer, normalizedDocNames); } catch (SpreadsheetException e) { throw new OnlineToolException("Unable to create output spreadsheet: "+e.getMessage()); } catch (InvalidSPDXAnalysisException e) { @@ -126,35 +121,70 @@ public static void onlineFunction(String[] args) throws OnlineToolException { /** - * Converts the list of URI's or file paths to a list of document names by + * Adds all SPDX documents found in the file or directory to the compareDocs list + * @param compareDocs + * @param filePath + * @param verificationErrors + * @throws InvalidFileNameException + * @throws IOException + * @throws InvalidSPDXAnalysisException + */ + private static void addDocToComparer(List compareDocs, + String filePath, List docNames, List> verificationErrors) throws InvalidSPDXAnalysisException, IOException, InvalidFileNameException { + File spdxDocOrDir = new File(filePath); + if (!spdxDocOrDir.exists()) { + throw new FileNotFoundException("File "+filePath+" not found"); + } + if (spdxDocOrDir.isFile()) { + SpdxDocument doc = SpdxToolsHelper.deserializeDocument(spdxDocOrDir); + compareDocs.add(doc); + List warnings = doc.verify(); + if (!warnings.isEmpty()) { + System.out.println("Verification errors were found in "+filePath.trim()+". See verification errors sheet for details."); + } + verificationErrors.add(warnings); + docNames.add(filePath); + } else if (spdxDocOrDir.isDirectory()) { + for (File file:spdxDocOrDir.listFiles()) { + try { + addDocToComparer(compareDocs, file.getPath(), docNames, verificationErrors); + } catch (InvalidSPDXAnalysisException | IOException | InvalidFileNameException e) { + System.out.println("Error deserializing "+file+". Skipping."); + continue; + } + } + } + } + + /** + * Converts the URI's or file paths to a list of document names by * removing the common string prefixes - * @param args arguments passed into utility - * @param startNameIndex index in args where then names start - * @return + * @param uriFilePaths Un-normalized file paths or URIs + * @return List of normalized doc names */ - private static List convertToDocNames(String[] args, int startNameIndex) { + private static List normalizeDocNames(List uriFilePaths) { List docNames = new ArrayList<>(); - if (args.length < startNameIndex) { - return docNames; + if (uriFilePaths.size() < 1) { + return docNames; } - int commonPrefixIndex = args[startNameIndex].length(); + int commonPrefixIndex = uriFilePaths.get(0).length(); // first find the minimum index length - for (int i = startNameIndex + 1; i < args.length; i++) { - if (args[i].length() < commonPrefixIndex) { - commonPrefixIndex = args[i].length(); + for (int i = 1; i < uriFilePaths.size(); i++) { + if (uriFilePaths.get(i).length() < commonPrefixIndex) { + commonPrefixIndex = uriFilePaths.get(i).length(); } } // look for the smallest common substring - for (int i = startNameIndex + 1; i < args.length; i++) { + for (int i = 1; i < uriFilePaths.size(); i++) { for (int j = 0; j < commonPrefixIndex; j++) { - if (args[i-1].charAt(j) != args[i].charAt(j)) { + if (uriFilePaths.get(i-1).charAt(j) != uriFilePaths.get(i).charAt(j)) { commonPrefixIndex = j; break; } } } - for (int i = startNameIndex; i < args.length; i++) { - docNames.add(args[i].substring(commonPrefixIndex).replace("\\", "/")); + for (String uriFilePath:uriFilePaths) { + docNames.add(uriFilePath.substring(commonPrefixIndex).replace("\\", "/")); } return docNames; } From abba26483bf364d89979e0b552cb4dd7d1b6efc7 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Wed, 26 Jan 2022 16:47:23 -0800 Subject: [PATCH 2/2] Remove the compareDoc from the README Signed-off-by: Gary O'Neall --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 55dee33..99424c7 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,6 @@ Example to convert a SPDX file from tag to rdf format: ## Compare utilities The following tools can be used to compare one or more SPDX documents: - * CompareSpdxDocs - - Example to compare two SPDX files provided in rdf format: - - java -jar spdx-tools-jar-with-dependencies.jar CompareSpdxDocs doc1 doc2 [output] - * CompareMultipleSpdxDocs with files Example to compare multiple SPDX files provided in rdf format and provide a spreadsheet with the results: