Skip to content

Commit

Permalink
Allow passing in an alternate Python executable (#98)
Browse files Browse the repository at this point in the history
* Allow passing in an alternate Python executable

* Also replace in `initializeRemoting`
  • Loading branch information
timtebeek authored Dec 3, 2024
1 parent fd71272 commit 53e237f
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class PythonParser implements Parser {
private final Collection<NamedStyles> styles;
private final boolean logCompilationWarningsAndErrors;
private final JavaTypeCache typeCache;
private final String executable;
private final List<Path> pythonPath;

private final @Nullable Path logFile;
Expand Down Expand Up @@ -187,7 +188,7 @@ private void initializeRemoting(ExecutionContext ctx) throws IOException {

int port = 54322;
if (!isServerRunning(port)) {
ProcessBuilder processBuilder = new ProcessBuilder("python3", "-m", "rewrite.remote.server", Integer.toString(port));
ProcessBuilder processBuilder = new ProcessBuilder(executable, "-m", "rewrite.remote.server", Integer.toString(port));
if (!pythonPath.isEmpty()) {
Map<String, String> environment = processBuilder.environment();
environment.compute("PYTHONPATH", (k, current) ->
Expand Down Expand Up @@ -269,15 +270,15 @@ public static Builder usingRemotingInstallation(Path dir) {
return new Builder(dir);
}

private static boolean verifyRemotingInstallation(Path dir, @Nullable Path logFile) throws IOException, InterruptedException {
private static boolean verifyRemotingInstallation(Path dir, String executable, @Nullable Path logFile) throws IOException, InterruptedException {
if (!Files.isDirectory(dir)) {
Files.createDirectories(dir);
}

try (InputStream inputStream = requireNonNull(PythonParser.class.getClassLoader().getResourceAsStream("META-INF/python-requirements.txt"))) {
List<String> packages = new BufferedReader(new InputStreamReader(inputStream)).lines().filter(l -> !l.isEmpty()).collect(Collectors.toList());

List<String> command = new ArrayList<>(Arrays.asList("python3", "-m", "pip", "install", "--target", dir.toString()));
List<String> command = new ArrayList<>(Arrays.asList(executable, "-m", "pip", "install", "--target", dir.toString()));
command.addAll(packages);

ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand All @@ -294,6 +295,8 @@ private static boolean verifyRemotingInstallation(Path dir, @Nullable Path logFi
public static class Builder extends Parser.Builder {
private JavaTypeCache typeCache = new JavaTypeCache();

private String executable = "python3";

@Nullable
private Path installationDir;

Expand All @@ -314,6 +317,11 @@ private Builder(Path installationDir) {
this.installationDir = installationDir;
}

public Builder executable(String executable) {
this.executable = executable;
return this;
}

public Builder logCompilationWarningsAndErrors(boolean logCompilationWarningsAndErrors) {
this.logCompilationWarningsAndErrors = logCompilationWarningsAndErrors;
return this;
Expand Down Expand Up @@ -350,13 +358,13 @@ public Builder pythonPath(List<Path> path) {
public PythonParser build() {
if (installationDir != null) {
try {
if (verifyRemotingInstallation(installationDir, logFile) && !pythonPath.contains(installationDir)) {
if (verifyRemotingInstallation(installationDir, executable, logFile) && !pythonPath.contains(installationDir)) {
pythonPath.add(installationDir);
}
} catch (IOException | InterruptedException ignore) {
}
}
return new PythonParser(styles, logCompilationWarningsAndErrors, typeCache, pythonPath, logFile, (int) parseTimeoutMs);
return new PythonParser(styles, logCompilationWarningsAndErrors, typeCache, executable, pythonPath, logFile, (int) parseTimeoutMs);
}

@Override
Expand Down

0 comments on commit 53e237f

Please sign in to comment.