Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAT-325 : Added check to skip expensive processing in SPDX #192

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions apache-rat-core/src/main/java/org/apache/rat/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ public class Report {
*/
public static void main(String[] args) throws Exception {
Options opts = buildOptions();

CommandLine cl;
try {
cl = new DefaultParser().parse(opts, args);
} catch (ParseException e) {
System.err.println(e.getMessage());
System.err.println("Please use the \"--help\" option to see a list of valid commands and options");
DefaultLog.INSTANCE.error(e.getMessage());
DefaultLog.INSTANCE.error("Please use the \"--help\" option to see a list of valid commands and options");
System.exit(1);
return; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE
// for "cl"
Expand All @@ -147,7 +146,7 @@ public static void main(String[] args) throws Exception {
printUsage(opts);
} else {
ReportConfiguration configuration = createConfiguration(args[0], cl);
configuration.validate(System.err::println);
configuration.validate(DefaultLog.INSTANCE::error);

boolean dryRun = false;

Expand Down Expand Up @@ -210,7 +209,7 @@ static ReportConfiguration createConfiguration(String baseDirectory, CommandLine
if (cl.hasOption(STYLESHEET_CLI)) {
String[] style = cl.getOptionValues(STYLESHEET_CLI);
if (style.length != 1) {
System.err.println("Please specify a single stylesheet");
DefaultLog.INSTANCE.error("Please specify a single stylesheet");
System.exit(1);
}
configuration.setStyleSheet(() -> Files.newInputStream(Paths.get(style[0])));
Expand Down Expand Up @@ -256,10 +255,10 @@ static FilenameFilter parseExclusions(List<String> excludes) {
orFilter.addFileFilter(new NameFileFilter(exclusion));
orFilter.addFileFilter(WildcardFileFilter.builder().setWildcards(exclusion).get());
} catch (PatternSyntaxException e) {
System.err.println("Will skip given exclusion '" + exclude + "' due to " + e);
DefaultLog.INSTANCE.error("Will skip given exclusion '" + exclude + "' due to " + e);
}
}
System.err.println("Ignored " + ignoredLines + " lines in your exclusion files as comments or empty lines.");
DefaultLog.INSTANCE.error("Ignored " + ignoredLines + " lines in your exclusion files as comments or empty lines.");
return new NotFileFilter(orFilter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void setStyleReport(boolean styleReport) {
* closed consider wrapping it in a {@code NoCloseOutputStream}
*
* @param out The OutputStream supplier that provides the output stream to write
* the report to. (may be null)
* the report to. A null value will use System.out.
* @see NoCloseOutputStream
*/
public void setOut(IOSupplier<OutputStream> out) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.rat.document.impl.guesser.BinaryGuesser;
import org.apache.rat.document.impl.guesser.NoteGuesser;
import org.apache.rat.license.ILicense;
import org.apache.rat.utils.Log;
import org.apache.rat.utils.Log.Level;

/**
* Creates default analysers.
Expand All @@ -40,11 +42,13 @@ public class DefaultAnalyserFactory {
* @param licenses The licenses to use in the Analyser.
* @return A document analyser that uses the provides licenses.
*/
public static IDocumentAnalyser createDefaultAnalyser(Collection<ILicense> licenses) {
public static IDocumentAnalyser createDefaultAnalyser(Log log, Collection<ILicense> licenses) {
if (licenses.isEmpty()) {
throw new ConfigurationException("At least one license must be defined");
}
return new DefaultAnalyser(new LicenseCollection(licenses));
log.debug("Licenses in Test");
licenses.forEach(log::debug);
return new DefaultAnalyser(log, new LicenseCollection(licenses));
}

/**
Expand All @@ -56,13 +60,16 @@ private final static class DefaultAnalyser implements IDocumentAnalyser {
* The license to analyze
*/
private final ILicense license;
/** The log to use */
private final Log log;

/**
* Constructs a DocumentAnalyser for the specified license.
* @param license The license to analyse
*/
public DefaultAnalyser(final ILicense license) {
public DefaultAnalyser(final Log log, final ILicense license) {
this.license = license;
this.log = log;
}

@Override
Expand All @@ -76,7 +83,7 @@ public void analyse(Document document) throws RatDocumentAnalysisException {
documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_BINARY;
} else {
documentCategory = MetaData.RAT_DOCUMENT_CATEGORY_DATUM_STANDARD;
final DocumentHeaderAnalyser headerAnalyser = new DocumentHeaderAnalyser(license);
final DocumentHeaderAnalyser headerAnalyser = new DocumentHeaderAnalyser(log, license);
headerAnalyser.analyse(document);
}
document.getMetaData().set(documentCategory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.apache.rat.document.IDocumentAnalyser;
import org.apache.rat.document.RatDocumentAnalysisException;
import org.apache.rat.license.ILicense;
import org.apache.rat.utils.Log;

import static java.lang.String.format;

/**
* A Document analyzer that analyses document headers for a license.
Expand All @@ -35,21 +38,24 @@ class DocumentHeaderAnalyser implements IDocumentAnalyser {
* The license to analyse
*/
private final ILicense license;
/** the logger to use */
private final Log log;

/**
* Constructs the HeaderAnalyser for the specific license.
*
* @param license The license to analyse
*/
public DocumentHeaderAnalyser(final ILicense license) {
public DocumentHeaderAnalyser(final Log log, final ILicense license) {
super();
this.license = license;
this.log = log;
}

@Override
public void analyse(Document document) throws RatDocumentAnalysisException {
try (Reader reader = document.reader()) {
// TODO: worker function should be moved into this class
log.info(format("Processing: %s", document));
HeaderCheckWorker worker = new HeaderCheckWorker(reader, license, document);
worker.read();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.rat.analysis;

import java.util.Collection;
import java.util.Collections;

import org.apache.rat.analysis.matchers.AbstractMatcherContainer;
import org.apache.rat.license.ILicense;
Expand All @@ -45,7 +46,7 @@ class LicenseCollection extends AbstractMatcherContainer implements ILicense {
*/
public LicenseCollection(Collection<ILicense> enclosed) {
super(enclosed);
this.enclosed = enclosed;
this.enclosed = Collections.unmodifiableCollection(enclosed);
this.matchingLicense = null;
this.lastState = State.i;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,19 @@ public CopyrightMatcher(String id, String start, String stop, String owner) {

@Override
protected boolean doMatch(String line) {
Matcher matcher = COPYRIGHT_PATTERN.matcher(line);
if (matcher.find()) {
String buffer = line.substring(matcher.end());
matcher = dateOwnerPattern.matcher(buffer);
if (matcher.find() && matcher.start() == 0) {
return true;
}
if (ownerDatePattern != null) {
matcher = ownerDatePattern.matcher(buffer);
return matcher.find() && matcher.start() == 0;
String lowerLine = line.toLowerCase();
if (lowerLine.contains("copyright") || lowerLine.contains("(c)") || line.contains("©")) {
Matcher matcher = COPYRIGHT_PATTERN.matcher(line);
if (matcher.find()) {
String buffer = line.substring(matcher.end());
matcher = dateOwnerPattern.matcher(buffer);
if (matcher.find() && matcher.start() == 0) {
return true;
}
if (ownerDatePattern != null) {
matcher = ownerDatePattern.matcher(buffer);
return matcher.find() && matcher.start() == 0;
}
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private boolean check(String line, Match caller) {
// if the line has not been seen yet see if we can extract the SPDX id from the line.
// if so then see if that name has been registered. If so then we have a match and set
// lastMatch.
if (lastLine == null || !lastLine.equals(line)) {
if ((lastLine == null || !lastLine.equals(line)) && line.contains("SPDX-License-Identifier")) {
Matcher matcher = groupSelector.matcher(line);
if (matcher.find()) {
lastMatch = matchers.get(matcher.group(1));
Expand Down Expand Up @@ -127,5 +127,12 @@ public class Match extends AbstractSimpleMatcher {
protected boolean doMatch(String line) {
return SPDXMatcherFactory.this.check(line, this);
}

@Override
public void reset() {
super.reset();
SPDXMatcherFactory.this.lastMatch = null;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.rat.annotation;

import org.apache.commons.io.IOUtils;
import org.apache.rat.utils.Log;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -196,9 +197,16 @@ public abstract class AbstractLicenseAppender {
}

private boolean isForced;
/** The log to use */
private final Log log;

public AbstractLicenseAppender() {
/**
* Constructor
* @param log The log to use.
*/
public AbstractLicenseAppender(final Log log) {
super();
this.log = log;
}

/**
Expand Down Expand Up @@ -247,11 +255,11 @@ public void append(File document) throws IOException {
if (isForced) {
boolean deleted = document.delete();
if (!deleted) {
System.err.println("Could not delete original file to prepare renaming.");
log.error("Could not delete original file to prepare renaming.");
}
boolean renamed = newDocument.renameTo(document.getAbsoluteFile());
if (!renamed) {
System.err.println("Failed to rename new file, original file remains unchanged.");
log.error("Failed to rename new file, original file remains unchanged.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.File;

import org.apache.rat.utils.Log;


/**
* Add an Apache License V2 license header to a
Expand All @@ -35,8 +37,8 @@ public class ApacheV2LicenseAppender extends AbstractLicenseAppender {
/**
* Create a license appender with the standard ASF license header.
*/
public ApacheV2LicenseAppender() {
super();
public ApacheV2LicenseAppender(final Log log) {
super(log);
}

/**
Expand All @@ -45,8 +47,8 @@ public ApacheV2LicenseAppender() {
*
* @param copyright copyright line.
*/
public ApacheV2LicenseAppender(String copyright) {
super();
public ApacheV2LicenseAppender(final Log log, String copyright) {
super(log);
this.copyright = copyright;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public MetaData() {
* @param license the license to add metadata for.
*/
public void reportOnLicense(ILicense license) {

if (StringUtils.isNotBlank(license.getNotes())) {
set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, license.getNotes()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.rat.api.RatException;
import org.apache.rat.report.AbstractReport;
import org.apache.rat.report.xml.writer.IXmlWriter;
import org.apache.rat.report.xml.writer.impl.base.XmlWriter;

import java.io.IOException;
import java.util.Calendar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import org.apache.rat.api.MetaData.Datum;
import org.apache.rat.api.RatException;
import org.apache.rat.report.AbstractReport;
import org.apache.rat.utils.Log;


public class LicenseAddingReport extends AbstractReport {
private final AbstractLicenseAppender appender;

public LicenseAddingReport(String pCopyrightMsg, boolean pForced) {
appender = pCopyrightMsg == null ? new ApacheV2LicenseAppender() : new ApacheV2LicenseAppender(pCopyrightMsg);
public LicenseAddingReport(final Log log, String pCopyrightMsg, boolean pForced) {
appender = pCopyrightMsg == null ? new ApacheV2LicenseAppender(log) : new ApacheV2LicenseAppender(log,pCopyrightMsg);
appender.setForce(pForced);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public static RatReport createStandardReport(IXmlWriter writer,
reporters.add(new ClaimAggregator(statistic));
}
if (configuration.isAddingLicenses()) {
reporters.add(new LicenseAddingReport(configuration.getCopyrightMessage(), configuration.isAddingLicensesForced()));
reporters.add(new LicenseAddingReport(configuration.getLog(), configuration.getCopyrightMessage(), configuration.isAddingLicensesForced()));
}
reporters.add(new SimpleXmlClaimReporter(writer));

final IDocumentAnalyser analyser =
DefaultAnalyserFactory.createDefaultAnalyser(configuration.getLicenses(LicenseFilter.all));
DefaultAnalyserFactory.createDefaultAnalyser(configuration.getLog(), configuration.getLicenses(LicenseFilter.all));
final DefaultPolicy policy = new DefaultPolicy(configuration.getLicenseFamilies(LicenseFilter.approved));

final IDocumentAnalyser[] analysers = {analyser, policy};
Expand Down
35 changes: 21 additions & 14 deletions apache-rat-core/src/main/java/org/apache/rat/utils/DefaultLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,32 @@ public class DefaultLog implements Log {
* The instance of the default log.
*/
public static final DefaultLog INSTANCE = new DefaultLog();

private Level level;

private DefaultLog() {
level = Level.INFO;
}

public void setLevel(Level level) {
this.level = level;
}
@Override
public void log(Level level, String msg) {
switch (level) {
case DEBUG:
case INFO:
case WARN:
System.out.format("%s: %s%n", level, msg);
break;
case ERROR:
System.err.format("%s: %s%n", level, msg);
break;
case OFF:
break;
default:
break;
}
if (this.level.ordinal() <= level.ordinal())
switch (level) {
case DEBUG:
case INFO:
case WARN:
System.out.format("%s: %s%n", level, msg);
break;
case ERROR:
System.err.format("%s: %s%n", level, msg);
break;
case OFF:
break;
default:
break;
}
}
}
Loading