From 7a4ab771e60e153d26bfe1af1a2a82e41e3afca5 Mon Sep 17 00:00:00 2001 From: Frank Liu Date: Thu, 18 May 2023 10:32:48 -0700 Subject: [PATCH] [python] Support pip install in offline mode (#729) Fixes #728 --- .../main/java/ai/djl/python/engine/PyEnv.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/engines/python/src/main/java/ai/djl/python/engine/PyEnv.java b/engines/python/src/main/java/ai/djl/python/engine/PyEnv.java index ec8117b2b1e..14da31521c5 100644 --- a/engines/python/src/main/java/ai/djl/python/engine/PyEnv.java +++ b/engines/python/src/main/java/ai/djl/python/engine/PyEnv.java @@ -31,6 +31,7 @@ import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -167,17 +168,27 @@ public synchronized void installDependency(Path modelDir) { } Path file = modelDir.resolve("requirements.txt"); if (Files.isRegularFile(file)) { - String[] cmd = { - pythonExecutable, - "-m", - "pip", - "-q", - "install", - "-r", - file.toAbsolutePath().toString() - }; + List cmd = new ArrayList<>(9); + cmd.add(pythonExecutable); + cmd.add("-m"); + cmd.add("pip"); + if (!logger.isDebugEnabled()) { + cmd.add("-q"); + } + cmd.add("install"); + cmd.add("-r"); + cmd.add(file.toAbsolutePath().toString()); + if (Boolean.getBoolean("offline")) { + cmd.add("--no-deps"); + } + Path dir = modelDir.resolve("requirements"); + if (Files.isDirectory(dir)) { + cmd.add("-f"); + cmd.add(file.toAbsolutePath().toString()); + } try { logger.info("Found requirements.txt, start installing Python dependencies..."); + logger.debug("{}", cmd); Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start(); String logOutput; try (InputStream is = process.getInputStream()) { @@ -186,10 +197,10 @@ public synchronized void installDependency(Path modelDir) { int ret = process.waitFor(); if (ret == 0) { logger.info("pip install requirements succeed!"); - logger.debug(logOutput); + logger.debug("{}", logOutput); } else { - logger.warn("requirements installation failed! With error code: {}", ret); - logger.warn(logOutput); + logger.warn("pip install failed with error code: {}", ret); + logger.warn("{}", logOutput); } } catch (IOException | InterruptedException e) { logger.warn("pip install requirements failed.", e);