Skip to content

Commit

Permalink
#47 🥅 catch pyenv vs. system python configuration mismatch; with self…
Browse files Browse the repository at this point in the history
… PR feedback
  • Loading branch information
d-ryan-ashcraft committed Sep 7, 2023
1 parent ead26ad commit 7c38b53
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

}

Expand Down Expand Up @@ -191,6 +191,17 @@ private String validatateAndConfigurePyenv(List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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<String> 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<String> arguments) throws MojoExecutionException {
return execute(arguments, Level.INFO);
}

/**
Expand All @@ -53,38 +78,45 @@ public String getCurrentPythonVersion() throws MojoExecutionException {
* @return
* @throws MojoExecutionException
*/
protected String execute(List<String> 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<String> 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<String> arguments) {
List<String> fullCommandArgs = new ArrayList<>();
fullCommandArgs.add(pythonCommand);
fullCommandArgs.addAll(arguments);
return new ProcessExecutor(workingDirectory, fullCommandArgs, Platform.guess(), null);
List<String> 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;
}
}

0 comments on commit 7c38b53

Please sign in to comment.