From 72e5519cef30c8ca57b89572e19a3ffdab143737 Mon Sep 17 00:00:00 2001 From: az Date: Tue, 23 May 2023 09:56:31 +0200 Subject: [PATCH] [skip ci] add possibility to install python packages in venv (important e.g. for remote installation) --- .../provs/desktop/infrastructure/Python.kt | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Python.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Python.kt index 03b0724..9088db6 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Python.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/desktop/infrastructure/Python.kt @@ -7,36 +7,49 @@ import org.domaindrivenarchitecture.provs.framework.ubuntu.install.base.aptInsta import java.io.File -fun Prov.provisionPython() = task { +fun Prov.provisionPython(venvHome: String? = "~/.venv/meissa") = task { installPython3() - configureVenv() - installPybuilder() - installRestClient() - installJupyterlab() + if (venvHome != null) { configureVenv(venvHome) } + installPybuilder(venvHome) + installRestClient(venvHome) + installJupyterlab(venvHome) } fun Prov.installPython3(): ProvResult = task { aptInstall("python3-venv python3-pip") } -fun Prov.configureVenv(): ProvResult = task { - val venvHome = "~/.venv/meissa" - cmd("python3 -m venv " + venvHome) - cmd("source " + venvHome + "/bin/activate") - createSymlink(File(venvHome + "/bin/activate"), File("~/.bashrc.d/venv.sh")) - cmd("pip3 install pip --upgrade") +fun Prov.configureVenv(venvHome: String): ProvResult = task { + cmd("python3 -m venv $venvHome") + createSymlink(File("$venvHome/bin/activate"), File("~/.bashrc.d/venv.sh")) + pipInstall("pip --upgrade", venvHome) } -fun Prov.installPybuilder(): ProvResult = task { - cmd("pip3 install pybuilder ddadevops pypandoc mockito coverage unittest-xml-reporting deprecation" + - " python_terraform dda_python_terraform boto3 pyyaml ") - cmd("pip3 install --upgrade ddadevops") +fun Prov.installPybuilder(venvHome: String? = null): ProvResult = task { + pipInstall("pybuilder ddadevops pypandoc mockito coverage unittest-xml-reporting deprecation" + + " python_terraform dda_python_terraform boto3 pyyaml ", + venvHome + ) + pipInstall("--upgrade ddadevops", venvHome) } -fun Prov.installRestClient(): ProvResult = task { - cmd("pip3 install requests") +fun Prov.installRestClient(venvHome: String? = null): ProvResult = task { + pipInstall("requests", venvHome) } -fun Prov.installJupyterlab(): ProvResult = task { - cmd("pip3 install jupyterlab pandas matplotlib") +fun Prov.installJupyterlab(venvHome: String? = null): ProvResult = task { + pipInstall("jupyterlab pandas matplotlib", venvHome) +} + + +private fun Prov.pipInstall(pkg: String, venvHome: String? = null) { + cmd(activateVenvCommandPrefix(venvHome) + "pip3 install $pkg") +} + +private fun activateVenvCommandPrefix(venvHome: String?): String { + return if (venvHome == null) { + "" + } else { + "source $venvHome/bin/activate && " + } }