Search in sources :

Example 1 with ExitException

use of de.jplag.exceptions.ExitException in project JPlag by jplag.

the class SubmissionSetBuilder method loadBaseCodeViaName.

/**
 * Try to load the basecode by looking up the basecode name in the root directory.
 * @return the base code submission if a fitting subdirectory was found, else {@code null}.
 * @throws ExitException when the option value is a sub-directory with errors.
 */
private Submission loadBaseCodeViaName(String baseCodeName, File rootDirectory, Map<File, Submission> foundSubmissions) throws ExitException {
    // Is the option value a single name after trimming spurious separators?
    String name = baseCodeName;
    while (name.startsWith(File.separator)) {
        name = name.substring(1);
    }
    while (name.endsWith(File.separator)) {
        name = name.substring(0, name.length() - 1);
    }
    // If it is not a name of a single sub-directory, bail out.
    if (name.isEmpty() || name.contains(File.separator)) {
        return null;
    }
    if (name.contains(".")) {
        throw new BasecodeException("The basecode directory name \"" + name + "\" cannot contain dots!");
    }
    // Grab the basecode submission from the regular submissions.
    File basecodePath = new File(rootDirectory, baseCodeName);
    Submission baseCode = foundSubmissions.get(makeCanonical(basecodePath, it -> new BasecodeException("Cannot compute canonical file path: " + basecodePath, it)));
    if (baseCode == null) {
        // No base code found at all, report an error.
        throw new BasecodeException(String.format("Basecode path \"%s\" relative to the working directory could not be found.", baseCodeName));
    } else {
        // Found a base code as a submission, report about legacy usage.
        System.out.println("Legacy use of the -bc option found, please specify the basecode by path instead of by name!");
    }
    return baseCode;
}
Also used : Arrays(java.util.Arrays) BasecodeException(de.jplag.exceptions.BasecodeException) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Function(java.util.function.Function) SubmissionException(de.jplag.exceptions.SubmissionException) File(java.io.File) ArrayList(java.util.ArrayList) JPlagOptions(de.jplag.options.JPlagOptions) HashSet(java.util.HashSet) List(java.util.List) RootDirectoryException(de.jplag.exceptions.RootDirectoryException) Map(java.util.Map) Optional(java.util.Optional) Collections(java.util.Collections) ExitException(de.jplag.exceptions.ExitException) BasecodeException(de.jplag.exceptions.BasecodeException) File(java.io.File)

Example 2 with ExitException

use of de.jplag.exceptions.ExitException in project JPlag by jplag.

the class SubmissionSetBuilder method verifyRootDirectories.

/**
 * Verify that the given root directories exist and have no duplicate entries.
 */
private Set<File> verifyRootDirectories(List<String> rootDirectoryNames) throws ExitException {
    Set<File> canonicalRootDirectories = new HashSet<>(rootDirectoryNames.size());
    for (String rootDirectoryName : rootDirectoryNames) {
        File rootDirectory = new File(rootDirectoryName);
        if (!rootDirectory.exists()) {
            throw new RootDirectoryException(String.format("Root directory \"%s\" does not exist!", rootDirectoryName));
        }
        if (!rootDirectory.isDirectory()) {
            throw new RootDirectoryException(String.format("Root directory \"%s\" is not a directory!", rootDirectoryName));
        }
        rootDirectory = makeCanonical(rootDirectory, it -> new RootDirectoryException("Cannot read root directory: " + rootDirectoryName, it));
        if (!canonicalRootDirectories.add(rootDirectory)) {
            // Root directory was already added, report a warning.
            System.out.printf("Warning: Root directory \"%s\" was specified more than once, duplicates will be ignored.", rootDirectoryName);
        }
    }
    return canonicalRootDirectories;
}
Also used : RootDirectoryException(de.jplag.exceptions.RootDirectoryException) Arrays(java.util.Arrays) BasecodeException(de.jplag.exceptions.BasecodeException) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Function(java.util.function.Function) SubmissionException(de.jplag.exceptions.SubmissionException) File(java.io.File) ArrayList(java.util.ArrayList) JPlagOptions(de.jplag.options.JPlagOptions) HashSet(java.util.HashSet) List(java.util.List) RootDirectoryException(de.jplag.exceptions.RootDirectoryException) Map(java.util.Map) Optional(java.util.Optional) Collections(java.util.Collections) ExitException(de.jplag.exceptions.ExitException) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with ExitException

use of de.jplag.exceptions.ExitException in project JPlag by jplag.

the class SubmissionSetBuilder method loadBaseCodeAsPath.

/**
 * Try to load the basecode under the assumption of being a path.
 * @return Base code submission if the option value can be interpreted as global path, else {@code null}.
 * @throws ExitException when the option value is a path with errors.
 */
private Submission loadBaseCodeAsPath(String baseCodeName) throws ExitException {
    File basecodeSubmission = new File(baseCodeName);
    if (!basecodeSubmission.exists()) {
        return null;
    }
    String errorMessage = isExcludedEntry(basecodeSubmission);
    if (errorMessage != null) {
        // Stating an excluded path as basecode isn't very useful.
        throw new BasecodeException(errorMessage);
    }
    try {
        // in the output since basecode matches are removed from it
        return processSubmission(basecodeSubmission.getName(), basecodeSubmission);
    } catch (SubmissionException exception) {
        // Change thrown exception to basecode exception.
        throw new BasecodeException(exception.getMessage(), exception);
    } catch (ExitException exception) {
        throw exception;
    }
}
Also used : BasecodeException(de.jplag.exceptions.BasecodeException) SubmissionException(de.jplag.exceptions.SubmissionException) File(java.io.File) ExitException(de.jplag.exceptions.ExitException)

Example 4 with ExitException

use of de.jplag.exceptions.ExitException in project JPlag by jplag.

the class CLI method main.

/**
 * Main class for using JPlag via the CLI.
 * @param args are the CLI arguments that will be passed to JPlag.
 */
public static void main(String[] args) {
    try {
        CLI cli = new CLI();
        Namespace arguments = cli.parseArguments(args);
        JPlagOptions options = cli.buildOptionsFromArguments(arguments);
        JPlag program = new JPlag(options);
        System.out.println("JPlag initialized");
        JPlagResult result = program.run();
        Report report = new JsonReport();
        report.saveReport(result, arguments.getString(RESULT_FOLDER.flagWithoutDash()));
    } catch (ExitException exception) {
        System.out.println("Error: " + exception.getMessage());
        System.exit(1);
    }
}
Also used : JPlagOptions(de.jplag.options.JPlagOptions) Report(de.jplag.reporting2.Report) JsonReport(de.jplag.reporting2.JsonReport) JsonReport(de.jplag.reporting2.JsonReport) ExitException(de.jplag.exceptions.ExitException) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Aggregations

ExitException (de.jplag.exceptions.ExitException)4 BasecodeException (de.jplag.exceptions.BasecodeException)3 SubmissionException (de.jplag.exceptions.SubmissionException)3 JPlagOptions (de.jplag.options.JPlagOptions)3 File (java.io.File)3 RootDirectoryException (de.jplag.exceptions.RootDirectoryException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Function (java.util.function.Function)2 JsonReport (de.jplag.reporting2.JsonReport)1 Report (de.jplag.reporting2.Report)1