-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* scripts support
- Loading branch information
imakunin
committed
May 4, 2024
1 parent
6d25de1
commit b098732
Showing
7 changed files
with
133 additions
and
86 deletions.
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
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
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
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
96 changes: 96 additions & 0 deletions
96
lzy/execution-env/src/main/java/ai/lzy/env/aux/PythonBaseEnvironment.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,96 @@ | ||
package ai.lzy.env.aux; | ||
|
||
import ai.lzy.env.logs.LogStream; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipInputStream; | ||
|
||
public interface PythonBaseEnvironment extends AuxEnvironment { | ||
|
||
static void installLocalModules(Map<String, String> localModules, Path localModulesPath, Logger log, | ||
LogStream userOut, LogStream userErr) throws InstallationException | ||
{ | ||
var msg = "Install python local modules to %s".formatted(localModulesPath); | ||
log.info(msg); | ||
userOut.log(msg); | ||
try { | ||
Files.createDirectories(localModulesPath); | ||
} catch (IOException e) { | ||
String errorMessage = "Failed to create directory to download local modules into;\n" | ||
+ " Directory name: " + localModulesPath + "\n"; | ||
log.error(errorMessage); | ||
userErr.log(errorMessage); | ||
throw new InstallationException(errorMessage); | ||
} | ||
|
||
log.info("Created directory {} to download local modules into", localModulesPath); | ||
for (var entry : localModules.entrySet()) { | ||
installLocalModule(entry.getKey(), entry.getValue(), localModulesPath, log, userOut, userErr); | ||
} | ||
} | ||
|
||
static void installLocalModule(String name, String url, Path path, Logger log, LogStream userOut, LogStream userErr) | ||
throws InstallationException | ||
{ | ||
log.info("Installing local module '{}' from {}", name, url); | ||
userOut.log("Installing local module '%s'".formatted(name)); | ||
|
||
File tempFile = null; | ||
try { | ||
tempFile = File.createTempFile("tmp-file", ".zip"); | ||
|
||
try (InputStream in = new URL(url).openStream()) { | ||
Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
|
||
log.info("Trying to unzip module archive {}", tempFile.getAbsolutePath()); | ||
extractFiles(tempFile, path); | ||
} catch (Exception e) { | ||
log.error("Failed to install local module '{}'", name, e); | ||
var errorMessage = "Failed to install local module '%s': %s".formatted(name, e.getMessage()); | ||
userErr.log(errorMessage); | ||
|
||
if (tempFile != null) { | ||
try { | ||
//noinspection ResultOfMethodCallIgnored | ||
tempFile.delete(); | ||
} catch (Exception ignored) { | ||
// ignore | ||
} | ||
} | ||
|
||
throw new InstallationException(errorMessage); | ||
} | ||
} | ||
|
||
private static void extractFiles(File zip, Path targetDir) throws IOException { | ||
try (var zipStream = new ZipInputStream(new FileInputStream(zip))) { | ||
ZipEntry zipEntry = zipStream.getNextEntry(); | ||
while (zipEntry != null) { | ||
final Path entryTargetPath = targetDir.resolve(zipEntry.getName()); | ||
if (!entryTargetPath.startsWith(targetDir)) { | ||
throw new IOException( | ||
"Zip entry '%s' is trying to escape target path '%s'".formatted(entryTargetPath, targetDir)); | ||
} | ||
if (zipEntry.isDirectory()) { | ||
Files.createDirectories(entryTargetPath); | ||
} else { | ||
Files.createDirectories(entryTargetPath.getParent()); | ||
Files.copy(zipStream, entryTargetPath); | ||
} | ||
zipEntry = zipStream.getNextEntry(); | ||
} | ||
} | ||
} | ||
|
||
} |
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
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