From 7c38b53062ca3ddb3c03949c5be949df3c0b2b83 Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Thu, 7 Sep 2023 11:05:54 -0400 Subject: [PATCH] #47 :goal_net: catch pyenv vs. system python configuration mismatch; with self PR feedback --- .../habushu/PyenvAndPoetrySetup.java | 13 ++- .../habushu/exec/PythonVersionHelper.java | 100 ++++++++++++------ 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/PyenvAndPoetrySetup.java b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/PyenvAndPoetrySetup.java index 11aee32..5459089 100644 --- a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/PyenvAndPoetrySetup.java +++ b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/PyenvAndPoetrySetup.java @@ -139,7 +139,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { } if (!missingRequiredToolMsgs.isEmpty()) { - throw new MojoExecutionException(StringUtils.join(missingRequiredToolMsgs, System.lineSeparator())); + throw new MojoExecutionException(StringUtils.join(System.lineSeparator(), missingRequiredToolMsgs, System.lineSeparator())); } @@ -191,6 +191,17 @@ private String validatateAndConfigurePyenv(List missingRequiredToolMsgs, currentPythonVersion = pyenvHelper.getCurrentPythonVersion(); } + // Check for misconfigured pyenv that looks right, but is actually not "taking" due to missing PATH setup: + PythonVersionHelper pythonVersionHelper = new PythonVersionHelper(baseDir, pythonVersion); + String postPyenvActivatedPythonVersion = pythonVersionHelper.getCurrentPythonVersion(); + if (!pythonVersion.equals(postPyenvActivatedPythonVersion)) { + missingRequiredToolMsgs.add(String.format("Expected 'pyenv' to set Python to %s but instead found %s!", + pythonVersion, postPyenvActivatedPythonVersion)); + missingRequiredToolMsgs.add("'pyenv' is installed, but not configured correctly. " + + "Ensure your PATH includes 'pyenv init -' expected content " + + "OR do not configure habushu to use 'pyenv' to manage the Python version!"); + } + log.debug("pyenv already installed"); } return currentPythonVersion; diff --git a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/exec/PythonVersionHelper.java b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/exec/PythonVersionHelper.java index d2cfe13..a0bc23c 100644 --- a/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/exec/PythonVersionHelper.java +++ b/habushu-maven-plugin/src/main/java/org/technologybrewery/habushu/exec/PythonVersionHelper.java @@ -10,6 +10,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.event.Level; /** * Helps determine the version of Python that is available on the user's @@ -21,17 +22,17 @@ public class PythonVersionHelper { private static final String PYTHON_COMMAND = "python"; private static final String PYTHON_3_COMMAND = "python3"; - private static final String pythonVersion3Regex = "^3.*"; - private static final String extractVersionRegex = "^.*?(?=([0-9]))"; + private static final String PYTHON_VERSION_3_REGEX = "^3.*"; + private static final String EXTRACT_VERSION_REGEX = "^.*?(?=([\\d]))"; private final String desiredPythonVersion; private final File workingDirectory; public PythonVersionHelper(File workingDirectory, String desiredPythonVersion) { - Validate.notNull(desiredPythonVersion); + Validate.notNull(desiredPythonVersion); - this.workingDirectory = workingDirectory; - this.desiredPythonVersion = desiredPythonVersion; + this.workingDirectory = workingDirectory; + this.desiredPythonVersion = desiredPythonVersion; } /** @@ -41,8 +42,32 @@ public PythonVersionHelper(File workingDirectory, String desiredPythonVersion) { * @return */ public String getCurrentPythonVersion() throws MojoExecutionException { - String version = execute(Collections.singletonList("--version")); - return version.replaceAll(extractVersionRegex, ""); + String version = quietlyExecute(Collections.singletonList("--version")); + return version.replaceAll(EXTRACT_VERSION_REGEX, ""); + } + + /** + * Executes a python command with the given arguments, logs the executed command + * at DEBUG level, and returns the resultant process output as a string. + * + * @param arguments + * @return + * @throws MojoExecutionException + */ + protected String quietlyExecute(List arguments) throws MojoExecutionException { + return execute(arguments, Level.DEBUG); + } + + /** + * Executes a python command with the given arguments, logs the executed command + * at INFO level, and returns the resultant process output as a string. + * + * @param arguments + * @return + * @throws MojoExecutionException + */ + protected String execute(List arguments) throws MojoExecutionException { + return execute(arguments, Level.INFO); } /** @@ -53,38 +78,45 @@ public String getCurrentPythonVersion() throws MojoExecutionException { * @return * @throws MojoExecutionException */ - protected String execute(List arguments) throws MojoExecutionException { - ProcessExecutor executor; - - if (desiredPythonVersion.matches(pythonVersion3Regex) && isPython3Installed()) { - executor = createPythonExecutor(PYTHON_3_COMMAND, arguments); - if (logger.isInfoEnabled()) { - logger.info("Executing python3 command: {} {}", PYTHON_3_COMMAND, StringUtils.join(arguments, " ")); - } - } else { - executor = createPythonExecutor(PYTHON_COMMAND, arguments); - if (logger.isInfoEnabled()) { - logger.info("Executing python command: {} {}", PYTHON_COMMAND, StringUtils.join(arguments, " ")); - } - } - - return executor.executeAndGetResult(logger); + private String execute(List arguments, Level logLevel) throws MojoExecutionException { + ProcessExecutor executor; + String pythonCommand; + + if (desiredPythonVersion.matches(PYTHON_VERSION_3_REGEX) && isPython3Installed()) { + executor = createPythonExecutor(PYTHON_3_COMMAND, arguments); + pythonCommand = PYTHON_3_COMMAND; + + } else { + executor = createPythonExecutor(PYTHON_COMMAND, arguments); + pythonCommand = PYTHON_COMMAND; + } + + if (logger.isInfoEnabled() || logger.isDebugEnabled()) { + String logStatement = String.format("Executing command: %s %s", pythonCommand, StringUtils.join(arguments, " ")); + if (Level.INFO.equals(logLevel)) { + logger.info(logStatement); + } else if (Level.DEBUG.equals(logLevel)) { + logger.debug(logStatement); + } + } + + return executor.executeAndGetResult(logger); } private ProcessExecutor createPythonExecutor(String pythonCommand, List arguments) { - List fullCommandArgs = new ArrayList<>(); - fullCommandArgs.add(pythonCommand); - fullCommandArgs.addAll(arguments); - return new ProcessExecutor(workingDirectory, fullCommandArgs, Platform.guess(), null); + List fullCommandArgs = new ArrayList<>(); + fullCommandArgs.add(pythonCommand); + fullCommandArgs.addAll(arguments); + return new ProcessExecutor(workingDirectory, fullCommandArgs, Platform.guess(), null); } private boolean isPython3Installed() { - try { - ProcessExecutor executor = createPythonExecutor(PYTHON_3_COMMAND, Collections.singletonList("--version")); - executor.executeAndGetResult(logger); - } catch (Throwable e) { - return false; - } - return true; + try { + ProcessExecutor executor = createPythonExecutor(PYTHON_3_COMMAND, Collections.singletonList("--version")); + executor.executeAndGetResult(logger); + } catch (Throwable e) { + return false; + } + return true; } }