From 78364a30165e0144d6fc2794b3f24a2aa8315a67 Mon Sep 17 00:00:00 2001 From: Jim Nicholson Date: Wed, 14 Feb 2024 00:05:33 -0800 Subject: [PATCH] Simplify venv setup --- vars/pythonVenvStage.groovy | 58 +++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/vars/pythonVenvStage.groovy b/vars/pythonVenvStage.groovy index ec4baf5..65a47d2 100644 --- a/vars/pythonVenvStage.groovy +++ b/vars/pythonVenvStage.groovy @@ -26,38 +26,40 @@ void call(Map config = [:], Closure body) { stage(runConfig.stepName) { printBanner(runConfig.stepName) - if (fileExists(runConfig.venvDir) && runConfig.recreateVenv) { - echo 'Removing old venv...' - dir(runConfig.venvDir) { - deleteDir() + ansiColor('css') { + if (fileExists(runConfig.venvDir) && runConfig.recreateVenv) { + echo 'Removing old venv...' + dir(runConfig.venvDir) { + deleteDir() + } } - } - if (!fileExists(runConfig.venvDir)) { - createVenv = "${runConfig.pythonPath} -m venv ${runConfig.venvDir}" - echo "python create venv cmd is: ${createVenv}" - sh createVenv - } + if (!fileExists(runConfig.venvDir)) { + createVenv = "${runConfig.pythonPath} -m venv ${runConfig.venvDir}" + echo "python create venv cmd is: ${createVenv}" + sh createVenv + } - withEnv(venvEnv) { - if (runConfig.updatePip) { - updateCmd = "${venvPython.join('/')} -m pip install --upgrade pip" - echo "Updating pip with (${updateCmd})" - echo "Environment is ${venvEnv}" - sh updateCmd + withEnv(venvEnv) { + List setupCmds = [] + if (runConfig.updatePip) { + updateCmd = "${venvPython.join('/')} -m pip install --upgrade pip" + setupCmds.add(updateCmd) + } + if (runConfig.installPipTools) { + installCmd = "${venvPython.join('/')} -m pip install pip-tools" + setupCmds.add(installCmd) + } + if (fileExists(runConfig.requirementsFile)) { + reqCmd = "pip-compile ${runConfig.requirementsFile}" + setupCmds.add(reqCmd) + } + if (!setupCmds.isEmpty()) { + sh setupCmds.join('\n') + } + echo 'Executing closure...' + body() } - if (runConfig.installPipTools) { - installCmd = "${venvPython.join('/')} -m pip install pip-tools" - echo "Installing pip-tools with (${installCmd})" - sh installCmd - } - if (fileExists(runConfig.requirementsFile)) { - reqCmd = "pip-compile ${runConfig.requirementsFile}" - echo "Collecting requirements with (${reqCmd})" - sh reqCmd - } - echo 'Executing closure...' - body() } } }