diff --git a/rewrite-python/src/main/java/org/openrewrite/python/PythonParser.java b/rewrite-python/src/main/java/org/openrewrite/python/PythonParser.java index 5bc0eead..03c1d10a 100644 --- a/rewrite-python/src/main/java/org/openrewrite/python/PythonParser.java +++ b/rewrite-python/src/main/java/org/openrewrite/python/PythonParser.java @@ -44,17 +44,19 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static java.util.Collections.singletonList; import static java.util.Objects.requireNonNull; @SuppressWarnings("unused") @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class PythonParser implements Parser { + private final Collection styles; private final boolean logCompilationWarningsAndErrors; private final JavaTypeCache typeCache; private final List pythonPath; + private final @Nullable Path logFile; + private final int parseTimeoutMs; private @Nullable Process pythonProcess; @@ -248,14 +250,10 @@ public static Builder builder() { } public static Builder usingRemotingInstallation(Path dir) { - try { - return verifyRemotingInstallation(dir); - } catch (IOException | InterruptedException ignore) { - } - return builder(); + return new Builder(dir); } - private static Builder verifyRemotingInstallation(Path dir) throws IOException, InterruptedException { + private static boolean verifyRemotingInstallation(Path dir, @Nullable Path logFile) throws IOException, InterruptedException { if (!Files.isDirectory(dir)) { Files.createDirectories(dir); } @@ -267,26 +265,39 @@ private static Builder verifyRemotingInstallation(Path dir) throws IOException, command.addAll(packages); ProcessBuilder processBuilder = new ProcessBuilder(command); - Process process = processBuilder.start(); + File redirectTo = logFile != null ? logFile.toFile() : new File(System.getProperty("os.name").startsWith("Windows") ? "NULL" : "/dev/null"); + processBuilder.redirectOutput(redirectTo); + processBuilder.redirectError(redirectTo); - int exitCode = process.waitFor(); - return new Builder().pythonPath(singletonList(dir)); + Process process = processBuilder.start(); + return process.waitFor() == 0; } } @SuppressWarnings("unused") public static class Builder extends Parser.Builder { private JavaTypeCache typeCache = new JavaTypeCache(); + + @Nullable + private Path installationDir; + private boolean logCompilationWarningsAndErrors; private final Collection styles = new ArrayList<>(); private List pythonPath = new ArrayList<>(); + private @Nullable Path logFile; + private long parseTimeoutMs = -1; private Builder() { super(Py.CompilationUnit.class); } + private Builder(Path installationDir) { + super(Py.CompilationUnit.class); + this.installationDir = installationDir; + } + public Builder logCompilationWarningsAndErrors(boolean logCompilationWarningsAndErrors) { this.logCompilationWarningsAndErrors = logCompilationWarningsAndErrors; return this; @@ -321,6 +332,14 @@ public Builder pythonPath(List path) { @Override public PythonParser build() { + if (installationDir != null) { + try { + if (verifyRemotingInstallation(installationDir, logFile) && !pythonPath.contains(installationDir)) { + pythonPath.add(installationDir); + } + } catch (IOException | InterruptedException ignore) { + } + } return new PythonParser(styles, logCompilationWarningsAndErrors, typeCache, pythonPath, logFile, (int) parseTimeoutMs); }