Skip to content

Commit e9a951b

Browse files
committed
improved error handling
1 parent aa66383 commit e9a951b

File tree

2 files changed

+70
-75
lines changed
  • graalpython

2 files changed

+70
-75
lines changed

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,55 +160,49 @@ protected void listGraalPyResources() throws MojoExecutionException {
160160
}
161161
}
162162

163-
protected void preExec(boolean checkFields) throws MojoExecutionException {
163+
protected void preExec(boolean enableWarnings) throws MojoExecutionException {
164164
pythonResourcesDirectory = normalizeEmpty(pythonResourcesDirectory);
165165
externalDirectory = normalizeEmpty(externalDirectory);
166166
resourceDirectory = normalizeEmpty(resourceDirectory);
167167
graalPyLockFile = normalizeEmpty(graalPyLockFile);
168168
packages = packages != null ? packages.stream().filter(p -> p != null && !p.trim().isEmpty()).toList() : Collections.EMPTY_LIST;
169169

170170
if(pythonResourcesDirectory != null) {
171-
if(checkFields) {
172-
if (externalDirectory != null) {
173-
throw new MojoExecutionException(
174-
"Cannot use <externalDirectory> and <resourceDirectory> at the same time. " +
175-
"New option <externalDirectory> is a replacement for deprecated <pythonResourcesDirectory>. " +
176-
"If you want to deploy the virtual environment into physical filesystem, use <externalDirectory>. " +
177-
"The deployment of the external directory alongside the application is not handled by the GraalPy Maven plugin in such case." +
178-
"If you want to bundle the virtual filesystem in Java resources, use <resourceDirectory>. " +
179-
"For more details, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools. ");
180-
}
181-
getLog().warn("Option <pythonResourcesDirectory> is deprecated and will be removed. Use <externalDirectory> instead.");
171+
if (externalDirectory != null) {
172+
throw new MojoExecutionException(
173+
"Cannot use <externalDirectory> and <resourceDirectory> at the same time. " +
174+
"New option <externalDirectory> is a replacement for deprecated <pythonResourcesDirectory>. " +
175+
"If you want to deploy the virtual environment into physical filesystem, use <externalDirectory>. " +
176+
"The deployment of the external directory alongside the application is not handled by the GraalPy Maven plugin in such case." +
177+
"If you want to bundle the virtual filesystem in Java resources, use <resourceDirectory>. " +
178+
"For more details, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools. ");
182179
}
180+
getLog().warn("Option <pythonResourcesDirectory> is deprecated and will be removed. Use <externalDirectory> instead.");
183181
externalDirectory = pythonResourcesDirectory;
184182
}
185183

186184
if (resourceDirectory != null) {
187-
if(checkFields) {
188-
if (resourceDirectory.startsWith("/") || resourceDirectory.endsWith("/")) {
189-
throw new MojoExecutionException(
190-
"Value of <resourceDirectory> should be relative resources path, i.e., without the leading '/', and it also must not end with trailing '/'");
191-
}
185+
if (resourceDirectory.startsWith("/") || resourceDirectory.endsWith("/")) {
186+
throw new MojoExecutionException(
187+
"Value of <resourceDirectory> should be relative resources path, i.e., without the leading '/', and it also must not end with trailing '/'");
192188
}
193189
}
194190

195191
if (resourceDirectory == null) {
196-
if(checkFields) {
197-
if (externalDirectory == null) {
198-
getLog().info(String.format("Virtual filesystem is deployed to default resources directory '%s'. " +
199-
"This can cause conflicts if used with other Java libraries that also deploy GraalPy virtual filesystem. " +
200-
"Consider adding <resourceDirectory>GRAALPY-VFS/${project.groupId}/${project.artifactId}</resourceDirectory> to your pom.xml, " +
201-
"moving any existing sources from '%s' to '%s', and using VirtualFileSystem$Builder#resourceDirectory." +
202-
"For more details, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools. ",
203-
VFS_ROOT,
204-
Path.of(VFS_ROOT, "src"),
205-
Path.of("GRAALPY-VFS", project.getGroupId(), project.getArtifactId())));
206-
}
192+
if (enableWarnings && externalDirectory == null) {
193+
getLog().info(String.format("Virtual filesystem is deployed to default resources directory '%s'. " +
194+
"This can cause conflicts if used with other Java libraries that also deploy GraalPy virtual filesystem. " +
195+
"Consider adding <resourceDirectory>GRAALPY-VFS/${project.groupId}/${project.artifactId}</resourceDirectory> to your pom.xml, " +
196+
"moving any existing sources from '%s' to '%s', and using VirtualFileSystem$Builder#resourceDirectory." +
197+
"For more details, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools. ",
198+
VFS_ROOT,
199+
Path.of(VFS_ROOT, "src"),
200+
Path.of("GRAALPY-VFS", project.getGroupId(), project.getArtifactId())));
207201
}
208202
resourceDirectory = VFS_ROOT;
209203
}
210204

211-
if(checkFields && pythonHome != null) {
205+
if(enableWarnings && pythonHome != null) {
212206
getLog().warn("The GraalPy plugin <pythonHome> configuration setting was deprecated and has no effect anymore.\n" +
213207
"For execution in jvm mode, the python language home is always available.\n" +
214208
"When building a native executable using GraalVM Native Image, then the full python language home is by default embedded into the native executable.\n" +

graalpython/org.graalvm.python.embedding.tools/src/org/graalvm/python/embedding/tools/vfs/VFSUtils.java

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -293,54 +293,55 @@ private LockFile(Path path, List<String> inputPackages, List<String> packages) {
293293
static LockFile fromFile(Path file, BuildToolLog log) throws IOException {
294294
List<String> packages = new ArrayList<>();
295295
List<String> inputPackages = null;
296-
if (Files.isReadable(file)) {
297-
List<String> lines = Files.readAllLines(file);
298-
if (lines.isEmpty()) {
299-
throw wrongFormat(file, lines, log);
300-
}
301-
// format:
302-
// 1.) a multiline header comment
303-
// 2.) graalpy version - 1 line (starting with comment #)
304-
// 2.) input packages - 1 line (starting with comment #)
305-
// 3.) locked packages - 1 line each (as input for pip install)
306-
// see also LockFile.write()
307-
Iterator<String> it = lines.iterator();
308-
try {
309-
// graalpy version, we don't care about it for now, but with future versions the
310-
// file format might change, and we will need to know to parse differently
311-
String graalPyVersion = null;
312-
while (it.hasNext()) {
313-
String line = it.next();
314-
if (line.startsWith(GRAALPY_VERSION_PREFIX)) {
315-
graalPyVersion = line.substring(GRAALPY_VERSION_PREFIX.length()).trim();
316-
if (graalPyVersion.isEmpty()) {
317-
throw wrongFormat(file, lines, log);
318-
}
319-
break;
320-
}
321-
}
322-
if (graalPyVersion == null) {
323-
throw wrongFormat(file, lines, log);
324-
}
325-
// input packages
296+
List<String> lines;
297+
try {
298+
lines = Files.readAllLines(file);
299+
} catch (IOException e) {
300+
throw new IOException(String.format("Cannot read the lock file from '%s'", file), e);
301+
}
302+
if (lines.isEmpty()) {
303+
throw wrongFormat(file, lines, log);
304+
}
305+
// format:
306+
// 1.) a multiline header comment
307+
// 2.) graalpy version - 1 line (starting with comment #)
308+
// 2.) input packages - 1 line (starting with comment #)
309+
// 3.) locked packages - 1 line each (as input for pip install)
310+
// see also LockFile.write()
311+
Iterator<String> it = lines.iterator();
312+
try {
313+
// graalpy version, we don't care about it for now, but with future versions the
314+
// file format might change, and we will need to know to parse differently
315+
String graalPyVersion = null;
316+
while (it.hasNext()) {
326317
String line = it.next();
327-
if (!line.startsWith(INPUT_PACKAGES_PREFIX)) {
328-
throw wrongFormat(file, lines, log);
329-
}
330-
String pkgs = line.substring(INPUT_PACKAGES_PREFIX.length()).trim();
331-
if (pkgs.isEmpty()) {
332-
throw wrongFormat(file, lines, log);
333-
}
334-
inputPackages = Arrays.asList(pkgs.split(INPUT_PACKAGES_DELIMITER));
335-
// locked packages
336-
while (it.hasNext()) {
337-
packages.add(it.next());
318+
if (line.startsWith(GRAALPY_VERSION_PREFIX)) {
319+
graalPyVersion = line.substring(GRAALPY_VERSION_PREFIX.length()).trim();
320+
if (graalPyVersion.isEmpty()) {
321+
throw wrongFormat(file, lines, log);
322+
}
323+
break;
338324
}
339-
} catch (NoSuchElementException e) {
325+
}
326+
if (graalPyVersion == null) {
340327
throw wrongFormat(file, lines, log);
341328
}
342-
} else {
343-
throw new IOException("can't read lock file");
329+
// input packages
330+
String line = it.next();
331+
if (!line.startsWith(INPUT_PACKAGES_PREFIX)) {
332+
throw wrongFormat(file, lines, log);
333+
}
334+
String pkgs = line.substring(INPUT_PACKAGES_PREFIX.length()).trim();
335+
if (pkgs.isEmpty()) {
336+
throw wrongFormat(file, lines, log);
337+
}
338+
inputPackages = Arrays.asList(pkgs.split(INPUT_PACKAGES_DELIMITER));
339+
// locked packages
340+
while (it.hasNext()) {
341+
packages.add(it.next());
342+
}
343+
} catch (NoSuchElementException e) {
344+
throw wrongFormat(file, lines, log);
344345
}
345346
return new LockFile(file, inputPackages, packages);
346347
}
@@ -353,7 +354,7 @@ private static IOException wrongFormat(Path file, List<String> lines, BuildToolL
353354
}
354355
log.debug("");
355356
}
356-
return new IOException("invalid lock file format");
357+
return new IOException(String.format("Cannot read the lock file from '%s'\n(turn on debug log level to see the contents)", file));
357358
}
358359

359360
private static void write(Path venvDirectory, Path lockFile, String lockFileHeader, List<String> inputPackages, String graalPyVersion, BuildToolLog log) throws IOException {
@@ -836,7 +837,7 @@ private static void lifecycle(BuildToolLog log, String txt, Object... args) {
836837
}
837838

838839
private static void debug(BuildToolLog log, String txt) {
839-
log.debug(txt);
840+
log.debug(txt);
840841
}
841842

842843
private static void logDebug(BuildToolLog log, List<String> l, String msg, Object... args) {

0 commit comments

Comments
 (0)