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;
}
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);
}
}
Aggregations