Skip to content

Commit 5b8c939

Browse files
committed
improved logging in GraalpPyRunner
1 parent e79ae1b commit 5b8c939

File tree

3 files changed

+130
-78
lines changed

3 files changed

+130
-78
lines changed

graalpython/graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/ExecGraalPyMojo.java

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ public void execute() throws MojoExecutionException {
9090
runGraalPy(project, getLog(), args);
9191
}
9292

93-
static List<String> runGraalPy(MavenProject project, Log log, String... args) throws MojoExecutionException {
93+
static void runGraalPy(MavenProject project, Log log, String... args) throws MojoExecutionException {
94+
runGraalPy(project, new LogDelegate(log), args);
95+
}
96+
97+
static void runGraalPy(MavenProject project, Log log, List<String> out, String... args) throws MojoExecutionException {
98+
runGraalPy(project, new LogDelegate(log, out), args);
99+
}
100+
101+
private static void runGraalPy(MavenProject project, GraalPyRunner.Log log, String... args) throws MojoExecutionException {
94102
var classpath = calculateClasspath(project);
95103
try {
96-
return GraalPyRunner.run(classpath, new MavenLogDelegate(log), args);
104+
GraalPyRunner.run(classpath, log, args);
97105
} catch (IOException | InterruptedException e) {
98106
throw new MojoExecutionException(e);
99107
}
@@ -131,46 +139,75 @@ private static HashSet<String> calculateClasspath(MavenProject project) throws M
131139
return classpath;
132140
}
133141

134-
private static class MavenLogDelegate implements GraalPyRunner.Log {
142+
private static class LogDelegate implements GraalPyRunner.Log {
135143
private final Log delegate;
136144

137-
private MavenLogDelegate(Log delegate) {
145+
private final List<String> output;
146+
147+
private LogDelegate(Log delegate) {
148+
this(delegate, null);
149+
}
150+
private LogDelegate(Log delegate, List<String> output) {
138151
this.delegate = delegate;
152+
this.output = output;
153+
}
154+
155+
public void subProcessOut(CharSequence var1) {
156+
if(output != null) {
157+
output.add(var1.toString());
158+
} else {
159+
System.out.println(var1.toString());
160+
}
161+
}
162+
163+
public void subProcessErr(CharSequence var1) {
164+
System.err.println(var1.toString());
165+
}
166+
167+
public void subProcessOut(Throwable var1) {
168+
var1.printStackTrace();
169+
System.out.println(var1.toString());
170+
}
171+
172+
public void subProcessErr(Throwable var1) {
173+
var1.printStackTrace();
174+
System.err.println(var1.toString());
139175
}
140-
public void debug(CharSequence var1) {
176+
177+
public void mvnDebug(CharSequence var1) {
141178
delegate.debug(var1);
142179
}
143-
public void debug(CharSequence var1, Throwable var2) {
180+
public void mvnDebug(CharSequence var1, Throwable var2) {
144181
delegate.debug(var1, var2);
145182
}
146-
public void debug(Throwable var1) {
183+
public void mvnDebug(Throwable var1) {
147184
delegate.debug(var1);
148185
}
149-
public void info(CharSequence var1) {
186+
public void mvnInfo(CharSequence var1) {
150187
delegate.info(var1);
151188
}
152-
public void info(CharSequence var1, Throwable var2) {
189+
public void mvnInfo(CharSequence var1, Throwable var2) {
153190
delegate.info(var1, var2);
154191
}
155-
public void info(Throwable var1) {
192+
public void mvnInfo(Throwable var1) {
156193
delegate.info(var1);
157194
}
158-
public void warn(CharSequence var1) {
195+
public void mvnWarn(CharSequence var1) {
159196
delegate.warn(var1);
160197
}
161-
public void warn(CharSequence var1, Throwable var2) {
198+
public void mvnWarn(CharSequence var1, Throwable var2) {
162199
delegate.warn(var1, var2);
163200
}
164-
public void warn(Throwable var1) {
201+
public void mvnWarn(Throwable var1) {
165202
delegate.warn(var1);
166203
}
167-
public void error(CharSequence var1) {
204+
public void mvnError(CharSequence var1) {
168205
delegate.error(var1);
169206
}
170-
public void error(CharSequence var1, Throwable var2) {
207+
public void mvnError(CharSequence var1, Throwable var2) {
171208
delegate.error(var1, var2);
172209
}
173-
public void error(Throwable var1) {
210+
public void mvnError(Throwable var1) {
174211
delegate.error(var1);
175212
}
176213
}

graalpython/graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/ManageResourcesMojo.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ public static class PythonHome {
100100

101101
private void manageHome() throws MojoExecutionException {
102102
var homeDirectory = getHomeDirectory(project);
103-
if(pythonHome == null) {
103+
if (pythonHome == null) {
104104
delete(homeDirectory);
105105
return;
106106
}
107-
108107
var tag = homeDirectory.resolve("tagfile");
109108
var graalPyVersion = ExecGraalPyMojo.getGraalPyVersion(project);
110109

@@ -118,7 +117,7 @@ private void manageHome() throws MojoExecutionException {
118117
getLog().info(String.format("Stale GraalPy home, updating to %s", graalPyVersion));
119118
delete(homeDirectory);
120119
}
121-
if(pythonHomeChanged(pythonHomeIncludes, pythonHomeExcludes, lines)) {
120+
if (pythonHomeChanged(pythonHomeIncludes, pythonHomeExcludes, lines)) {
122121
getLog().info(String.format("Deleting GraalPy home due to chenges includes or excludes"));
123122
delete(homeDirectory);
124123
}
@@ -128,7 +127,6 @@ private void manageHome() throws MojoExecutionException {
128127
} else {
129128
getLog().info(String.format("Creating GraalPy %s home", graalPyVersion));
130129
}
131-
132130
if (!Files.exists(homeDirectory)) {
133131
try {
134132
Files.createDirectories(homeDirectory.getParent());
@@ -137,7 +135,6 @@ private void manageHome() throws MojoExecutionException {
137135
}
138136
copy(homeDirectory.toAbsolutePath().toString(), pythonHomeIncludes, pythonHomeExcludes);
139137
}
140-
141138
try {
142139
Files.write(tag, List.of(graalPyVersion), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
143140
write(tag, pythonHomeIncludes, INCLUDE_PREFIX);
@@ -180,15 +177,12 @@ private ArrayList<String> toSortedArrayList(List<String> l) {
180177
private void copy(String targetRootPath, List<String> pythonHomeIncludes, List<String> pythonHomeExcludes) throws MojoExecutionException {
181178
getLog().info(String.format("Copying std lib to '%s'\n", targetRootPath));
182179
try {
183-
File target = new File(targetRootPath + File.separator + "lib-graalpython");
184-
if(!target.exists()) {
185-
target.mkdirs();
186-
}
187-
180+
// get stdlib and core home
188181
String stdlibHome = null;
189182
String coreHome = null;
190183
String pathsOutputPrefix = "<=outputpaths=>";
191-
List<String> homePathsOutput = ExecGraalPyMojo.runGraalPy(project, getLog(), new String[]{"-c", "print('" + pathsOutputPrefix + "', __graalpython__.get_python_home_paths(), sep='')"});
184+
List<String> homePathsOutput = new ArrayList<>();
185+
ExecGraalPyMojo.runGraalPy(project, getLog(), homePathsOutput, new String[]{"-c", "print('" + pathsOutputPrefix + "', __graalpython__.get_python_home_paths(), sep='')"});
192186
for (String l : homePathsOutput) {
193187
if(l.startsWith(pathsOutputPrefix)) {
194188
String[] s = l.substring(pathsOutputPrefix.length()).split(File.pathSeparator);
@@ -200,6 +194,10 @@ private void copy(String targetRootPath, List<String> pythonHomeIncludes, List<S
200194
assert coreHome != null;
201195

202196
// copy core home
197+
File target = new File(targetRootPath + File.separator + "lib-graalpython");
198+
if(!target.exists()) {
199+
target.mkdirs();
200+
}
203201
Path source = Paths.get(coreHome);
204202
Predicate<Path> filter = (f) -> {
205203
if(Files.isDirectory(f)) {
@@ -221,12 +219,11 @@ private void copy(String targetRootPath, List<String> pythonHomeIncludes, List<S
221219
};
222220
copyFolder(source, source, target, filter);
223221

222+
// copy stdlib home
224223
target = new File(targetRootPath + File.separator + "lib-python"+ File.separator + "3");
225224
if(!target.exists()) {
226225
target.mkdirs();
227226
}
228-
229-
// copy stdlib home
230227
source = Paths.get(stdlibHome);
231228
filter = (f) -> {
232229
if(Files.isDirectory(f)) {
@@ -252,22 +249,24 @@ private void copy(String targetRootPath, List<String> pythonHomeIncludes, List<S
252249
}
253250
}
254251

255-
private static boolean isIncluded(String filePath, List<String> includes) {
252+
private boolean isIncluded(String filePath, List<String> includes) {
256253
if(includes == null || includes.isEmpty()) {
257254
return true;
258255
}
259256
return pathMatches(filePath, includes);
260257
}
261258

262-
private static boolean isExcluded(String filePath, List<String> excludes) {
259+
private boolean isExcluded(String filePath, List<String> excludes) {
263260
if(excludes == null || excludes.isEmpty()) {
264261
return false;
265262
}
266263
return pathMatches(filePath, excludes);
267264
}
268265

269-
private static boolean pathMatches(String filePath, List<String> includes) {
270-
filePath.replaceAll(File.separator, "/");
266+
private boolean pathMatches(String filePath, List<String> includes) {
267+
if(File.separator.equals("\\")) {
268+
filePath = filePath.replaceAll("\\\\", "/");
269+
}
271270
for (String i: includes) {
272271
Pattern pattern = Pattern.compile(i);
273272
Matcher matcher = pattern.matcher(filePath);
@@ -278,7 +277,7 @@ private static boolean pathMatches(String filePath, List<String> includes) {
278277
return false;
279278
}
280279

281-
private static void copyFolder(Path sourceRoot, Path file, File targetRoot, Predicate<Path> filter) throws IOException {
280+
private void copyFolder(Path sourceRoot, Path file, File targetRoot, Predicate<Path> filter) throws IOException {
282281
Files.walkFileTree(file, new SimpleFileVisitor<Path>() {
283282
@Override
284283
public FileVisitResult visitFile(Path f, BasicFileAttributes attrs) throws IOException {
@@ -366,7 +365,7 @@ private void manageVenv() throws MojoExecutionException {
366365

367366
var venvDirectory = getVenvDirectory(project);
368367

369-
if(packages == null || packages.isEmpty()) {
368+
if (packages == null || packages.isEmpty()) {
370369
getLog().info(String.format("No venv packages declared, deleting %s", venvDirectory));
371370
delete(venvDirectory);
372371
return;
@@ -394,7 +393,7 @@ private void manageVenv() throws MojoExecutionException {
394393
getLog().info(String.format("Creating GraalPy %s venv", graalPyVersion));
395394
}
396395

397-
if (!Files.exists(venvDirectory)) {
396+
if (!Files.exists(venvDirectory)) {
398397
runLauncher("-m", "venv", venvDirectory.toString(), "--without-pip");
399398
runVenvBin(venvDirectory, "graalpy", List.of("-I", "-m", "ensurepip"));
400399
}
@@ -496,7 +495,7 @@ private void generateLaunchers() throws MojoExecutionException {
496495
from pathlib import Path
497496
vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe')
498497
tl = os.path.join(r'%s')
499-
os.makedirs(Path(tl).parent.absolute())
498+
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
500499
shutil.copy(vl, tl)
501500
cmd = r'mvn.cmd -f "%s" graalpy:exec "-Dexec.workingdir=%s"'
502501
with open(tl, 'ab') as f:

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/GraalPyRunner.java

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
public class GraalPyRunner {
5555

56-
public static List<String> run(Set<String> classpath, Log log, String... args) throws IOException, InterruptedException {
56+
public static void run(Set<String> classpath, Log log, String... args) throws IOException, InterruptedException {
5757
String workdir = System.getProperty("exec.workingdir");
5858
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
5959
List<String> cmd = new ArrayList<>();
@@ -67,65 +67,81 @@ public static List<String> run(Set<String> classpath, Log log, String... args) t
6767
pb.directory(new File(workdir));
6868
}
6969

70-
log.debug(String.format("Running GraalPy: %s", String.join(" ", cmd)));
70+
log.mvnDebug(String.format("Running GraalPy: %s", String.join(" ", cmd)));
7171

72-
List<String> output = new ArrayList<>();
7372
pb.redirectError();
7473
pb.redirectOutput();
75-
Process p = pb.start();
76-
p.waitFor();
77-
try (InputStream is = p.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
78-
String line;
79-
log.debug("=================== graalpy subprocess output start =================== ");
80-
while ((line = reader.readLine()) != null) {
81-
log.debug(line);
82-
output.add(line);
74+
Process process = pb.start();
75+
Thread outputReader = new Thread(() -> {
76+
try (InputStream is = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
77+
String line;
78+
while ((line = reader.readLine()) != null) {
79+
log.subProcessOut(line);
80+
}
81+
} catch (IOException e) {
82+
// Do nothing for now. Probably is not good idea to stop the
83+
// execution at this moment
84+
log.subProcessErr(e);
8385
}
84-
log.debug("=================== graalpy subprocess output end ===================== ");
85-
} catch (IOException e) {
86-
// Do nothing for now. Probably is not good idea to stop the
87-
// execution at this moment
88-
}
89-
90-
if (p.exitValue() != 0) {
91-
// if there are some errors, print the error output
92-
BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
93-
String line;
94-
log.error("=================== graalpy subprocess error output start =================== ");
95-
while ((line = errorBufferedReader.readLine()) != null) {
96-
log.error(line);
86+
});
87+
outputReader.start();
88+
89+
Thread errorReader = new Thread(() -> {
90+
try {
91+
BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
92+
String line;
93+
while ((line = errorBufferedReader.readLine()) != null) {
94+
log.subProcessErr(line);
95+
}
96+
} catch (IOException e) {
97+
// Do nothing for now. Probably is not good idea to stop the
98+
// execution at this moment
99+
log.subProcessErr(e);
97100
}
98-
log.error("=================== graalpy subprocess error output end ===================== ");
99-
// and terminate the build process
100-
throw new RuntimeException(String.format("Running command: '%s' ended with code %d.See the error output above.", String.join(" ", pb.command()), p.exitValue()));
101+
});
102+
errorReader.start();
103+
104+
process.waitFor();
105+
outputReader.join();
106+
errorReader.join();
107+
108+
if (process.exitValue() != 0) {
109+
throw new RuntimeException(String.format("Running command: '%s' ended with code %d.See the error output above.", String.join(" ", pb.command()), process.exitValue()));
101110
}
102-
return output;
103111
}
104112

105113
public static interface Log {
106-
void debug(CharSequence var1);
107114

108-
void debug(CharSequence var1, Throwable var2);
115+
void subProcessOut(CharSequence var1);
109116

110-
void debug(Throwable var1);
117+
void subProcessErr(CharSequence var1);
111118

112-
void info(CharSequence var1);
119+
void subProcessOut(Throwable var1);
113120

114-
void info(CharSequence var1, Throwable var2);
121+
void subProcessErr(Throwable var1);
115122

116-
void info(Throwable var1);
123+
void mvnDebug(CharSequence var1);
117124

118-
void warn(CharSequence var1);
125+
void mvnDebug(CharSequence var1, Throwable var2);
119126

120-
void warn(CharSequence var1, Throwable var2);
127+
void mvnDebug(Throwable var1);
121128

122-
void warn(Throwable var1);
129+
void mvnInfo(CharSequence var1);
123130

124-
void error(CharSequence var1);
131+
void mvnInfo(CharSequence var1, Throwable var2);
125132

126-
void error(CharSequence var1, Throwable var2);
133+
void mvnInfo(Throwable var1);
127134

128-
void error(Throwable var1);
129-
}
135+
void mvnWarn(CharSequence var1);
130136

137+
void mvnWarn(CharSequence var1, Throwable var2);
138+
139+
void mvnWarn(Throwable var1);
140+
141+
void mvnError(CharSequence var1);
142+
143+
void mvnError(CharSequence var1, Throwable var2);
144+
145+
void mvnError(Throwable var1);
146+
}
131147
}

0 commit comments

Comments
 (0)