-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[python] Add a CLI command to import pip requirements.txt #652
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
examples/applications/python-processor-exclamation/python/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
uvicorn==0.12.2 | ||
fastapi==0.63.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
langstream-cli/src/main/java/ai/langstream/cli/commands/RootPythonCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.cli.commands; | ||
|
||
import ai.langstream.cli.commands.python.LoadPythonDependenciesCmd; | ||
import lombok.Getter; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "python", | ||
header = "Tools for Python developers", | ||
subcommands = {LoadPythonDependenciesCmd.class}) | ||
@Getter | ||
public class RootPythonCmd { | ||
@CommandLine.ParentCommand private RootCmd rootCmd; | ||
} |
31 changes: 31 additions & 0 deletions
31
langstream-cli/src/main/java/ai/langstream/cli/commands/python/BasePythonCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.cli.commands.python; | ||
|
||
import ai.langstream.cli.commands.BaseCmd; | ||
import ai.langstream.cli.commands.RootCmd; | ||
import ai.langstream.cli.commands.RootPythonCmd; | ||
import picocli.CommandLine; | ||
|
||
public abstract class BasePythonCmd extends BaseCmd { | ||
|
||
@CommandLine.ParentCommand private RootPythonCmd rootPythonCmd; | ||
|
||
@Override | ||
protected RootCmd getRootCmd() { | ||
return rootPythonCmd.getRootCmd(); | ||
} | ||
} |
213 changes: 213 additions & 0 deletions
213
...stream-cli/src/main/java/ai/langstream/cli/commands/python/LoadPythonDependenciesCmd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
/* | ||
* Copyright DataStax, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.langstream.cli.commands.python; | ||
|
||
import ai.langstream.cli.commands.VersionProvider; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import lombok.SneakyThrows; | ||
import org.apache.commons.io.input.Tailer; | ||
import org.apache.commons.io.input.TailerListener; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "load-pip-requirements", | ||
header = "Process python dependencies in requirements.txt") | ||
public class LoadPythonDependenciesCmd extends BasePythonCmd { | ||
|
||
private static final AtomicReference<ProcessHandle> dockerProcess = new AtomicReference<>(); | ||
|
||
@CommandLine.Option( | ||
names = {"-app", "--application"}, | ||
description = "Application directory path", | ||
required = true) | ||
private String appPath; | ||
|
||
@CommandLine.Option( | ||
names = {"--docker-args"}, | ||
description = "Additional docker arguments") | ||
private List<String> dockerAdditionalArgs = new ArrayList<>(); | ||
|
||
@CommandLine.Option( | ||
names = {"--docker-command"}, | ||
description = "Command to run docker") | ||
private String dockerCommand = "docker"; | ||
|
||
@CommandLine.Option( | ||
names = {"--langstream-runtime-version"}, | ||
description = "Version of the LangStream runtime to use") | ||
private String dockerImageVersion = VersionProvider.getMavenVersion(); | ||
|
||
@CommandLine.Option( | ||
names = {"--langstream-runtime-docker-image"}, | ||
description = "Docker image of the LangStream runtime to use") | ||
private String dockerImageName; | ||
|
||
@Override | ||
@SneakyThrows | ||
public void run() { | ||
|
||
if (dockerImageVersion != null && dockerImageVersion.endsWith("-SNAPSHOT")) { | ||
// built-from-sources, not a release | ||
dockerImageVersion = "latest-dev"; | ||
} | ||
|
||
if (dockerImageName == null) { | ||
if (dockerImageVersion != null && dockerImageVersion.equals("latest-dev")) { | ||
// built-from-sources, not a release | ||
dockerImageName = "langstream/langstream-runtime-tester"; | ||
} else { | ||
// default to latest | ||
dockerImageName = "ghcr.io/langstream/langstream-runtime-tester"; | ||
} | ||
} | ||
|
||
if (appPath == null || appPath.isEmpty()) { | ||
throw new IllegalArgumentException("application files are required"); | ||
} | ||
|
||
final File appDirectory = new File(appPath); | ||
|
||
log("Using docker image: " + dockerImageName + ":" + dockerImageVersion); | ||
|
||
downloadDependencies(appDirectory.toPath(), getClient(), this::log); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(this::cleanEnvironment)); | ||
|
||
executeOnDocker(appDirectory); | ||
} | ||
|
||
private void cleanEnvironment() { | ||
if (dockerProcess.get() != null) { | ||
dockerProcess.get().destroyForcibly(); | ||
} | ||
} | ||
|
||
private void executeOnDocker(File appDirectory) throws Exception { | ||
final File appTmp = appDirectory; | ||
|
||
File pythonDirectory = new File(appDirectory, "python"); | ||
if (!pythonDirectory.isDirectory()) { | ||
throw new IllegalArgumentException( | ||
"Directory " + pythonDirectory.getAbsolutePath() + " not found"); | ||
} | ||
File requirementsFile = new File(pythonDirectory, "requirements.txt"); | ||
if (!requirementsFile.isFile()) { | ||
throw new IllegalArgumentException( | ||
"File " | ||
+ requirementsFile.getAbsolutePath() | ||
+ " not found in " | ||
+ pythonDirectory); | ||
} | ||
|
||
String imageName = dockerImageName + ":" + dockerImageVersion; | ||
List<String> commandLine = new ArrayList<>(); | ||
commandLine.add(dockerCommand); | ||
|
||
/* | ||
docker run --rm \ | ||
-v $(pwd):/app-code-download \ | ||
--entrypoint "" \ | ||
-w /app-code-download/python ghcr.io/langstream/langstream-runtime:0.1.0 \ | ||
/bin/bash -c 'pip3 install --target ./lib --upgrade --prefer-binary -r requirements.txt' | ||
|
||
*/ | ||
|
||
commandLine.add("run"); | ||
commandLine.add("--rm"); | ||
commandLine.add("--entrypoint"); | ||
commandLine.add("/bin/bash"); | ||
commandLine.add("-w"); | ||
commandLine.add("/code/application/python"); | ||
|
||
commandLine.add("-v"); | ||
commandLine.add(appTmp.getAbsolutePath() + ":/code/application"); | ||
|
||
if (dockerAdditionalArgs != null) { | ||
commandLine.addAll(dockerAdditionalArgs); | ||
} | ||
|
||
commandLine.add(imageName); | ||
|
||
if (getRootCmd().isVerbose()) { | ||
System.out.println("Executing:"); | ||
System.out.println(String.join(" ", commandLine)); | ||
} | ||
|
||
commandLine.add("-c"); | ||
commandLine.add( | ||
"pip3 install --target ./lib --upgrade --prefer-binary -r requirements.txt"); | ||
|
||
final Path outputLog = Files.createTempFile("langstream", ".log"); | ||
log("Logging to file: " + outputLog.toAbsolutePath()); | ||
ProcessBuilder processBuilder = | ||
new ProcessBuilder(commandLine) | ||
.redirectErrorStream(true) | ||
.redirectOutput(outputLog.toFile()); | ||
Process process = processBuilder.start(); | ||
dockerProcess.set(process.toHandle()); | ||
CompletableFuture.runAsync( | ||
() -> tailLogSysOut(outputLog), Executors.newSingleThreadExecutor()); | ||
|
||
final int exited = process.waitFor(); | ||
// wait for the log to be printed | ||
Thread.sleep(1000); | ||
if (exited != 0) { | ||
throw new RuntimeException("Process exited with code " + exited); | ||
} | ||
} | ||
|
||
private void tailLogSysOut(Path outputLog) { | ||
|
||
TailerListener listener = | ||
new TailerListener() { | ||
@Override | ||
public void fileNotFound() {} | ||
|
||
@Override | ||
public void fileRotated() {} | ||
|
||
@Override | ||
public void handle(Exception e) {} | ||
|
||
@Override | ||
public void handle(String s) { | ||
log(s); | ||
} | ||
|
||
@Override | ||
public void init(Tailer tailer) {} | ||
}; | ||
try (final Tailer tailer = | ||
Tailer.builder() | ||
.setTailerListener(listener) | ||
.setStartThread(false) | ||
.setDelayDuration(Duration.ofMillis(100)) | ||
.setFile(outputLog.toFile()) | ||
.get(); ) { | ||
while (true) { | ||
tailer.run(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we move it to a common function? is the same logic of docker run, right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, done