Search in sources :

Example 1 with StarlarkThread

use of net.starlark.java.eval.StarlarkThread in project copybara by google.

the class SkylarkTransformation method transform.

@Override
public TransformationStatus transform(TransformWork work) throws IOException, ValidationException, RepoException {
    SkylarkConsole skylarkConsole = new SkylarkConsole(work.getConsole());
    TransformWork skylarkWork = work.withConsole(skylarkConsole).withParams(params);
    TransformationStatus status = TransformationStatus.success();
    try (Mutability mu = Mutability.create("dynamic_transform")) {
        StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
        thread.setPrintHandler(printHandler);
        Object result = Starlark.call(thread, function, ImmutableList.of(skylarkWork), /*kwargs=*/
        ImmutableMap.of());
        result = result == Starlark.NONE ? TransformationStatus.success() : result;
        checkCondition(result instanceof TransformationStatus, "Dynamic transforms functions should return nothing or objects of type %s, but '%s'" + " returned: %s", TransformationStatus.STARLARK_TYPE_NAME, describe(), result);
        status = (TransformationStatus) result;
    } catch (EvalException e) {
        if (e.getCause() instanceof EmptyChangeException) {
            throw ((EmptyChangeException) e.getCause());
        }
        if (e.getCause() instanceof RepoException) {
            throw new RepoException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
        }
        throw new ValidationException(String.format("Error while executing the skylark transformation %s: %s", describe(), e.getMessageWithStack()), e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("This should not happen.", e);
    } finally {
        work.updateFrom(skylarkWork);
    }
    checkCondition(skylarkConsole.getErrorCount() == 0, "%d error(s) while executing %s", skylarkConsole.getErrorCount(), describe());
    return status;
}
Also used : TransformationStatus(com.google.copybara.TransformationStatus) ValidationException(com.google.copybara.exception.ValidationException) NonReversibleValidationException(com.google.copybara.exception.NonReversibleValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) TransformWork(com.google.copybara.TransformWork) Mutability(net.starlark.java.eval.Mutability) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Example 2 with StarlarkThread

use of net.starlark.java.eval.StarlarkThread in project copybara by google.

the class StarlarkAction method run.

@Override
public <T extends SkylarkContext<T>> void run(ActionContext<T> context) throws ValidationException, RepoException {
    SkylarkContext<T> actionContext = context.withParams(params);
    try (Mutability mu = Mutability.create("dynamic_action")) {
        StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT);
        thread.setPrintHandler(printHandler);
        Object result = Starlark.call(thread, function, ImmutableList.of(actionContext), /*kwargs=*/
        ImmutableMap.of());
        context.onFinish(result, actionContext);
    } catch (EvalException e) {
        Throwable cause = e.getCause();
        String error = String.format("Error while executing the skylark transformation %s: %s.", function.getName(), e.getMessageWithStack());
        if (cause instanceof RepoException) {
            throw new RepoException(error, cause);
        }
        throw new ValidationException(error, cause);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException("This should not happen.", e);
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) StarlarkThread(net.starlark.java.eval.StarlarkThread) Mutability(net.starlark.java.eval.Mutability) EvalException(net.starlark.java.eval.EvalException) RepoException(com.google.copybara.exception.RepoException)

Aggregations

RepoException (com.google.copybara.exception.RepoException)2 ValidationException (com.google.copybara.exception.ValidationException)2 EvalException (net.starlark.java.eval.EvalException)2 Mutability (net.starlark.java.eval.Mutability)2 StarlarkThread (net.starlark.java.eval.StarlarkThread)2 TransformWork (com.google.copybara.TransformWork)1 TransformationStatus (com.google.copybara.TransformationStatus)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 NonReversibleValidationException (com.google.copybara.exception.NonReversibleValidationException)1