Skip to content

Commit

Permalink
Log result of module installation to log file
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 20, 2024
1 parent 224fc07 commit 47f1445
Showing 1 changed file with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamedStyles> styles;
private final boolean logCompilationWarningsAndErrors;
private final JavaTypeCache typeCache;
private final List<Path> pythonPath;

private final @Nullable Path logFile;

private final int parseTimeoutMs;

private @Nullable Process pythonProcess;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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<NamedStyles> styles = new ArrayList<>();
private List<Path> 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;
Expand Down Expand Up @@ -321,6 +332,14 @@ public Builder pythonPath(List<Path> 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);
}

Expand Down

0 comments on commit 47f1445

Please sign in to comment.