Skip to content

Commit

Permalink
feat(tasks): add an option to disabled venv on Python
Browse files Browse the repository at this point in the history
  • Loading branch information
tchiotludo committed Sep 2, 2022
1 parent db9dc81 commit 27c632d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
16 changes: 15 additions & 1 deletion core/src/main/java/io/kestra/core/tasks/scripts/Python.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public class Python extends AbstractPython implements RunnableTask<ScriptOutput>
@PluginProperty(dynamic = true)
private List<String> args;

@Schema(
title = "Create a virtual env",
description = "When a virtual env is created, we will install the `requirements` needed. " +
"Disabled it if all the requirements is already on the file system.\n" +
"If you disabled the virtual env creation, the `requirements` will be ignored."
)
@PluginProperty(dynamic = false)
@Builder.Default
protected Boolean virtualEnv = true;

@Override
protected Map<String, String> finalInputFiles(RunContext runContext) throws IOException, IllegalVariableEvaluationException {
Map<String, String> map = super.finalInputFiles(runContext);
Expand All @@ -114,7 +124,11 @@ public ScriptOutput run(RunContext runContext) throws Exception {

return run(runContext, throwSupplier(() -> {
List<String> renderer = new ArrayList<>();
renderer.add(this.virtualEnvCommand(runContext, requirements));
if (this.virtualEnv) {
renderer.add(this.virtualEnvCommand(runContext, requirements));
} else if (this.exitOnFailed) {
renderer.add("set -o errexit");
}

for (String command : commands) {
String argsString = args == null ? "" : " " + runContext.render(String.join(" ", args), additionalVars);
Expand Down
25 changes: 21 additions & 4 deletions core/src/test/java/io/kestra/core/tasks/PythonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import io.kestra.core.tasks.scripts.Bash;
import io.kestra.core.tasks.scripts.Python;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import jakarta.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -88,6 +86,25 @@ void requirements() throws Exception {
assertThat(run.getVars().get("extract"), is("200"));
}

@Test
void noVirtualEnv() throws Exception {
RunContext runContext = runContextFactory.of();
Python python = Python.builder()
.id("test-python-task")
.inputFiles(Map.of(
"main.py", "from kestra import Kestra\n" +
"Kestra.outputs({'ok': True})\n"
))
.commands(List.of("python main.py"))
.virtualEnv(false)
.build();

ScriptOutput run = python.run(runContext);

assertThat(run.getExitCode(), is(0));
assertThat(run.getVars().get("ok"), is(true));
}

@Test
void docker() throws Exception {
Map<String, String> files = new HashMap<>();
Expand Down

0 comments on commit 27c632d

Please sign in to comment.