Search in sources :

Example 1 with CompileException

use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.

the class RulesCache method createMachine.

private PrologMachineCopy createMachine(Project.NameKey project, ObjectId rulesId) throws CompileException {
    //
    if (rulesDir != null) {
        Path jarPath = rulesDir.resolve("rules-" + rulesId.getName() + ".jar");
        if (Files.isRegularFile(jarPath)) {
            URL[] cp = new URL[] { toURL(jarPath) };
            return save(newEmptyMachine(new URLClassLoader(cp, systemLoader)));
        }
    }
    // Dynamically consult the rules into the machine's internal database.
    //
    String rules = read(project, rulesId);
    PrologMachineCopy pmc = consultRules("rules.pl", new StringReader(rules));
    if (pmc == null) {
        throw new CompileException("Cannot consult rules of " + project);
    }
    return pmc;
}
Also used : Path(java.nio.file.Path) URLClassLoader(java.net.URLClassLoader) StringReader(java.io.StringReader) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) PrologMachineCopy(com.googlecode.prolog_cafe.lang.PrologMachineCopy) URL(java.net.URL)

Example 2 with CompileException

use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.

the class RulesCache method read.

private String read(Project.NameKey project, ObjectId rulesId) throws CompileException {
    try (Repository git = gitMgr.openRepository(project)) {
        try {
            ObjectLoader ldr = git.open(rulesId, Constants.OBJ_BLOB);
            byte[] raw = ldr.getCachedBytes(maxSrcBytes);
            return RawParseUtils.decode(raw);
        } catch (LargeObjectException e) {
            throw new CompileException("rules of " + project + " are too large", e);
        } catch (RuntimeException | IOException e) {
            throw new CompileException("Cannot load rules of " + project, e);
        }
    } catch (IOException e) {
        throw new CompileException("Cannot open repository " + project, e);
    }
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Repository(org.eclipse.jgit.lib.Repository) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException)

Example 3 with CompileException

use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.

the class SubmitRuleEvaluator method runSubmitFilters.

private Term runSubmitFilters(Term results, PrologEnvironment env, String filterRuleLocatorName, String filterRuleWrapperName) throws RuleEvalException {
    ProjectState projectState = control.getProjectControl().getProjectState();
    PrologEnvironment childEnv = env;
    for (ProjectState parentState : projectState.parents()) {
        PrologEnvironment parentEnv;
        try {
            parentEnv = parentState.newPrologEnvironment();
        } catch (CompileException err) {
            throw new RuleEvalException("Cannot consult rules.pl for " + parentState.getProject().getName(), err);
        }
        parentEnv.copyStoredValues(childEnv);
        Term filterRule = parentEnv.once("gerrit", filterRuleLocatorName, new VariableTerm());
        try {
            if (opts.fastEvalLabels()) {
                env.once("gerrit", "assume_range_from_label");
            }
            Term[] template = parentEnv.once("gerrit", filterRuleWrapperName, filterRule, results, new VariableTerm());
            results = template[2];
        } catch (ReductionLimitException err) {
            throw new RuleEvalException(String.format("%s on change %d of %s", err.getMessage(), cd.getId().get(), parentState.getProject().getName()));
        } catch (RuntimeException err) {
            throw new RuleEvalException(String.format("Exception calling %s on change %d of %s", filterRule, cd.getId().get(), parentState.getProject().getName()), err);
        } finally {
            reductionsConsumed += env.getReductions();
        }
        childEnv = parentEnv;
    }
    return results;
}
Also used : CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ReductionLimitException(com.googlecode.prolog_cafe.exceptions.ReductionLimitException) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PrologEnvironment(com.google.gerrit.rules.PrologEnvironment)

Example 4 with CompileException

use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.

the class SubmitRuleEvaluator method getPrologEnvironment.

private PrologEnvironment getPrologEnvironment(CurrentUser user) throws RuleEvalException {
    ProjectState projectState = control.getProjectControl().getProjectState();
    PrologEnvironment env;
    try {
        if (opts.rule() == null) {
            env = projectState.newPrologEnvironment();
        } else {
            env = projectState.newPrologEnvironment("stdin", new StringReader(opts.rule()));
        }
    } catch (CompileException err) {
        String msg;
        if (opts.rule() == null && control.getProjectControl().isOwner()) {
            msg = String.format("Cannot load rules.pl for %s: %s", getProjectName(), err.getMessage());
        } else if (opts.rule() != null) {
            msg = err.getMessage();
        } else {
            msg = String.format("Cannot load rules.pl for %s", getProjectName());
        }
        throw new RuleEvalException(msg, err);
    }
    env.set(StoredValues.REVIEW_DB, cd.db());
    env.set(StoredValues.CHANGE_DATA, cd);
    env.set(StoredValues.CHANGE_CONTROL, control);
    if (user != null) {
        env.set(StoredValues.CURRENT_USER, user);
    }
    return env;
}
Also used : StringReader(java.io.StringReader) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) PrologEnvironment(com.google.gerrit.rules.PrologEnvironment)

Example 5 with CompileException

use of com.googlecode.prolog_cafe.exceptions.CompileException in project gerrit by GerritCodeReview.

the class PrologCompiler method compileJava.

/** Compile java src into java .class files */
private void compileJava(File tempDir) throws IOException, CompileException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        throw new CompileException("JDK required (running inside of JRE)");
    }
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(getAllFiles(tempDir, ".java"));
        ArrayList<String> options = new ArrayList<>();
        String classpath = getMyClasspath();
        if (classpath != null) {
            options.add("-classpath");
            options.add(classpath);
        }
        options.add("-d");
        options.add(tempDir.getPath());
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
        if (!task.call()) {
            Locale myLocale = Locale.getDefault();
            StringBuilder msg = new StringBuilder();
            msg.append("Cannot compile to Java bytecode:");
            for (Diagnostic<? extends JavaFileObject> err : diagnostics.getDiagnostics()) {
                msg.append('\n');
                msg.append(err.getKind());
                msg.append(": ");
                if (err.getSource() != null) {
                    msg.append(err.getSource().getName());
                }
                msg.append(':');
                msg.append(err.getLineNumber());
                msg.append(": ");
                msg.append(err.getMessage(myLocale));
            }
            throw new CompileException(msg.toString());
        }
    }
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) JavaFileObject(javax.tools.JavaFileObject) CompileException(com.googlecode.prolog_cafe.exceptions.CompileException) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector)

Aggregations

CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)10 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)4 JavaObjectTerm (com.googlecode.prolog_cafe.lang.JavaObjectTerm)3 StructureTerm (com.googlecode.prolog_cafe.lang.StructureTerm)3 PushbackReader (java.io.PushbackReader)3 StringReader (java.io.StringReader)3 PrologEnvironment (com.google.gerrit.rules.PrologEnvironment)2 ListTerm (com.googlecode.prolog_cafe.lang.ListTerm)2 Term (com.googlecode.prolog_cafe.lang.Term)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Repository (org.eclipse.jgit.lib.Repository)2 FactoryModule (com.google.gerrit.extensions.config.FactoryModule)1 PrologCompiler (com.google.gerrit.pgm.rules.PrologCompiler)1 Project (com.google.gerrit.reviewdb.client.Project)1 ReductionLimitException (com.googlecode.prolog_cafe.exceptions.ReductionLimitException)1 SyntaxException (com.googlecode.prolog_cafe.exceptions.SyntaxException)1 TermException (com.googlecode.prolog_cafe.exceptions.TermException)1 BufferingPrologControl (com.googlecode.prolog_cafe.lang.BufferingPrologControl)1 IntegerTerm (com.googlecode.prolog_cafe.lang.IntegerTerm)1