Skip to content

Commit fab3eb0

Browse files
committed
[GR-62398] Unify error handling in gradle.
PullRequest: graalpython/3721
2 parents 60607ba + aa0cecc commit fab3eb0

File tree

11 files changed

+149
-141
lines changed

11 files changed

+149
-141
lines changed

graalpython/com.oracle.graal.python.test/src/org/graalvm/python/embedding/cext/test/MultiContextCExtTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.graalvm.polyglot.PolyglotException;
5959
import org.graalvm.polyglot.Source;
6060
import org.graalvm.python.embedding.tools.exec.BuildToolLog;
61+
import org.graalvm.python.embedding.tools.vfs.VFSUtils;
6162
import org.junit.Test;
6263

6364
import static org.graalvm.python.embedding.test.EmbeddingTestUtils.deleteDirOnShutdown;
@@ -165,7 +166,7 @@ public void close() {
165166
}
166167

167168
@Test
168-
public void testCreatingVenvForMulticontext() throws IOException {
169+
public void testCreatingVenvForMulticontext() throws IOException, VFSUtils.PackagesChangedException {
169170
var log = new TestLog();
170171
Path tmpdir = Files.createTempDirectory("MultiContextCExtTest");
171172
Path venvDir = tmpdir.resolve("venv");

graalpython/com.oracle.graal.python.test/src/org/graalvm/python/embedding/test/EmbeddingTestUtils.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ public final class EmbeddingTestUtils {
5858
private EmbeddingTestUtils() {
5959
}
6060

61-
public static void createVenv(Path venvDir, String graalPyVersion, BuildToolLog log, String... packages) throws IOException {
62-
createVenv(venvDir, graalPyVersion, log, null, null, null, packages);
61+
public static void createVenv(Path venvDir, String graalPyVersion, BuildToolLog log, String... packages) throws IOException, VFSUtils.PackagesChangedException {
62+
createVenv(venvDir, graalPyVersion, log, null, null, packages);
6363
}
6464

65-
public static void createVenv(Path venvDir, String graalPyVersion, BuildToolLog log, Path lockFile, String missingLockFileWarning, String packageRemovedError, String... packages)
66-
throws IOException {
65+
public static void createVenv(Path venvDir, String graalPyVersion, BuildToolLog log, Path lockFile, String missingLockFileWarning, String... packages)
66+
throws IOException, VFSUtils.PackagesChangedException {
6767
try {
6868
info(log, "<<<<<< create test venv %s <<<<<<", venvDir);
6969

7070
Launcher launcher = createLauncher(venvDir);
7171
if (lockFile != null) {
72-
VFSUtils.createVenv(venvDir, Arrays.asList(packages), lockFile, packageRemovedError, missingLockFileWarning, launcher, graalPyVersion, log);
72+
VFSUtils.createVenv(venvDir, Arrays.asList(packages), lockFile, missingLockFileWarning, launcher, graalPyVersion, log);
7373
} else {
7474
VFSUtils.createVenv(venvDir, Arrays.asList(packages), launcher, graalPyVersion, log);
7575
}

graalpython/com.oracle.graal.python.test/src/org/graalvm/python/embedding/vfs/test/VFSUtilsTest.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.graalvm.python.embedding.test.EmbeddingTestUtils;
4545
import org.graalvm.python.embedding.tools.exec.BuildToolLog;
4646
import org.graalvm.python.embedding.tools.vfs.VFSUtils;
47+
import org.graalvm.python.embedding.tools.vfs.VFSUtils.PackagesChangedException;
4748
import org.junit.Test;
4849

4950
import java.io.IOException;
@@ -86,7 +87,6 @@ public class VFSUtilsTest {
8687

8788
private static final String PACKAGE_WAS_REMOVED = "A package with transitive dependencies was removed since last install, setting up a clean venv";
8889
private static final String LOCK_FILE_HEADER = "generated by graalpy tests\nwith a two line header";
89-
private static final String PACKAGES_CHANGED_ERROR = "packages changed in lock file %s, current packages %s, previous packages %s";
9090
private static final String MISSING_LOCK_FILE_WARNING = "missing lock file";
9191
private static final CharSequence STALE_VENV = "Stale GraalPy virtual environment, updating to";
9292

@@ -184,7 +184,7 @@ private static boolean isVerbose() {
184184
* - packages declared only in plugin config - lock file path is provided, but does not exist
185185
*/
186186
@Test
187-
public void withPackagesOnlyFromPluginConfig() throws IOException {
187+
public void withPackagesOnlyFromPluginConfig() throws IOException, PackagesChangedException {
188188
TestLog log = new TestLog();
189189
Path tmpDir = Files.createTempDirectory("withPackagesOnlyFromPluginConfig");
190190
Path venvDir = tmpDir.resolve("venv");
@@ -262,7 +262,7 @@ public void withPackagesOnlyFromPluginConfig() throws IOException {
262262
* - packages declared only in plugin config - and lock file path is not provided
263263
*/
264264
@Test
265-
public void withoutLockFile() throws IOException {
265+
public void withoutLockFile() throws IOException, PackagesChangedException {
266266
TestLog log = new TestLog();
267267
Path tmpDir = Files.createTempDirectory("withoutLockFile");
268268
Path venvDir = tmpDir.resolve("venv");
@@ -375,7 +375,7 @@ private static List<String> createLockFileHeader(String graalPyVersion, String..
375375
}
376376

377377
@Test
378-
public void installAndLock() throws IOException {
378+
public void installAndLock() throws IOException, PackagesChangedException {
379379
TestLog log = new TestLog();
380380
Path tmpDir = Files.createTempDirectory("installAndLock");
381381
Path venvDir = tmpDir.resolve("venv");
@@ -407,7 +407,7 @@ public void installAndLock() throws IOException {
407407
log.clearOutput();
408408

409409
// reinstall without exact version declared - fails
410-
checkException(IOException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests"));
410+
checkException(PackagesChangedException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests"));
411411
checkVenvCreate(log.getOutput(), false);
412412
assertFalse(log.getOutput().contains("pip install"));
413413
checkInstalledPackages(venvDir.resolve("installed.txt"), "requests==2.32.2", "charset-normalizer", "idna", "urllib3", "certifi");
@@ -446,10 +446,9 @@ public void installAndLock() throws IOException {
446446

447447
// add tiny-tiny - fails because inconsistent with lock file
448448
assert Files.exists(lockFile);
449-
checkException(IOException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests==2.32.1", "tiny-tiny==0.2"));
449+
checkException(PackagesChangedException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests==2.32.1", "tiny-tiny==0.2"));
450450
checkVenvCreate(log.getOutput(), false);
451451
assertFalse(log.getOutput().contains("pip install"));
452-
assertTrue(log.getOutput().contains(String.format(PACKAGES_CHANGED_ERROR, lockFile, "requests==2.32.1, tiny-tiny==0.2", "requests==2.32.1")));
453452
checkInstalledPackages(venvDir.resolve("installed.txt"), "requests==2.32.1", "charset-normalizer", "idna", "urllib3", "certifi");
454453
checkVenvContentsFile(contents, "0.1", "requests==2.32.1");
455454
assertFalse(log.getOutput().contains(MISSING_LOCK_FILE_WARNING));
@@ -481,10 +480,9 @@ public void installAndLock() throws IOException {
481480
log.clearOutput();
482481

483482
// update in declared packages requests version back to 2.32.2 - fails
484-
checkException(IOException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests==2.32.2", "tiny-tiny==0.2"));
483+
checkException(PackagesChangedException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "requests==2.32.2", "tiny-tiny==0.2"));
485484
checkVenvCreate(log.getOutput(), false);
486485
assertFalse(log.getOutput().contains("pip install"));
487-
assertTrue(log.getOutput().contains(String.format(PACKAGES_CHANGED_ERROR, lockFile, "requests==2.32.2, tiny-tiny==0.2", "requests==2.32.1, tiny-tiny==0.2")));
488486
checkInstalledPackages(venvDir.resolve("installed.txt"), "requests==2.32.1", "tiny-tiny==0.2", "charset-normalizer", "idna", "urllib3", "certifi");
489487
checkVenvContentsFile(contents, "0.1", "requests==2.32.1", "tiny-tiny==0.2");
490488
assertFalse(log.getOutput().contains(MISSING_LOCK_FILE_WARNING));
@@ -520,10 +518,9 @@ public void installAndLock() throws IOException {
520518
log.clearOutput();
521519

522520
// remove requests from packages list - fails because it is still in lock
523-
checkException(IOException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "tiny-tiny==0.2"));
521+
checkException(PackagesChangedException.class, () -> createVenv(venvDir, "0.1", log, lockFile, "tiny-tiny==0.2"));
524522
checkVenvCreate(log.getOutput(), false);
525523
assertFalse(log.getOutput().contains("pip install"));
526-
assertTrue(log.getOutput().contains(String.format(PACKAGES_CHANGED_ERROR, lockFile, "tiny-tiny==0.2", "requests==2.32.2, tiny-tiny==0.2")));
527524
log.clearOutput();
528525
// lock only with tiny-tiny
529526
lock(venvDir, lockFile, log, "tiny-tiny==0.2");
@@ -656,12 +653,12 @@ private static void checkPackages(Path file, List<String> linesArg, String... pa
656653
}
657654
}
658655

659-
private static void createVenv(Path venvDir, String graalPyVersion, TestLog log, String... packages) throws IOException {
656+
private static void createVenv(Path venvDir, String graalPyVersion, TestLog log, String... packages) throws IOException, PackagesChangedException {
660657
EmbeddingTestUtils.createVenv(venvDir, graalPyVersion, log, packages);
661658
}
662659

663-
private static void createVenv(Path venvDir, String graalPyVersion, TestLog log, Path lockFile, String... packages) throws IOException {
664-
EmbeddingTestUtils.createVenv(venvDir, graalPyVersion, log, lockFile, MISSING_LOCK_FILE_WARNING, PACKAGES_CHANGED_ERROR, packages);
660+
private static void createVenv(Path venvDir, String graalPyVersion, TestLog log, Path lockFile, String... packages) throws IOException, PackagesChangedException {
661+
EmbeddingTestUtils.createVenv(venvDir, graalPyVersion, log, lockFile, MISSING_LOCK_FILE_WARNING, packages);
665662
}
666663

667664
private static void checkVenvContentsFile(Path contents, String graalPyVersion, String... packages) throws IOException {

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_gradle_plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def check_gradle_empty_packages(self):
429429
cmd = gradle_cmd + ["graalpyLockPackages"]
430430
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
431431
util.check_ouput("BUILD SUCCESS", out, contains=False)
432-
util.check_ouput("In order to run the graalpyLockPackages task there have to be python packages declared in the graalpy-gradle-plugin configuration.", out)
432+
util.check_ouput("In order to run the graalPyLockPackages task there have to be python packages declared in the graalpy-gradle-plugin configuration.", out)
433433
assert not os.path.exists(os.path.join(target_dir, "graalpy.lock"))
434434

435435

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

-35
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,6 @@ public abstract class AbstractGraalPyMojo extends AbstractMojo {
7272
private static final String PYTHON_ARTIFACT_ID = "python";
7373
private static final String GRAALPY_MAVEN_PLUGIN_ARTIFACT_ID = "graalpy-maven-plugin";
7474

75-
protected static final String LOCK_FILE_HEADER = """
76-
This file was generated by maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
77-
78-
WARNING: Any manual changes are done at your own risk and will be overwritten when the goal is executed.
79-
80-
This file is meant to be tracked in a version control system.
81-
82-
This file contains a list of all required Python packages with their specific versions,
83-
based on the packages defined in the plugin configuration and their dependencies.
84-
""";
85-
86-
protected static final String MISSING_LOCK_FILE_WARNING = """
87-
88-
The list of installed Python packages does not match the packages specified in the graalpy-maven-plugin configuration.
89-
This could indicate that either extra dependencies were installed or some packages were installed with a more specific versions than declared.
90-
91-
In such cases, it is strongly recommended to lock the Python dependencies by executing the Maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
92-
93-
For more details on managing Python dependencies, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools#Python-Dependency-Management
94-
95-
""";
96-
97-
protected static final String PACKAGES_CHANGED_ERROR = """
98-
Install of python packages is based on lock file %s,
99-
but packages and their version constraints in graalpy-maven-plugin configuration are different then previously used to generate the lock file.
100-
101-
Packages currently declared in graalpy-maven-plugin configuration: %s
102-
Packages which were used to generate the lock file: %s
103-
104-
The lock file has to be refreshed by running the maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
105-
106-
For more information, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools#Python-Dependency-Management
107-
108-
""";
109-
11075
@Parameter(defaultValue = "${project}", required = true, readonly = true)
11176
MavenProject project;
11277

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.apache.maven.plugins.annotations.Mojo;
4646
import org.apache.maven.plugins.annotations.ResolutionScope;
4747
import org.graalvm.python.embedding.tools.vfs.VFSUtils;
48+
import org.graalvm.python.embedding.tools.vfs.VFSUtils.PackagesChangedException;
4849

4950
import java.io.IOException;
5051
import java.nio.file.Path;
@@ -54,6 +55,30 @@
5455
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
5556
public class InstallPackagesMojo extends AbstractGraalPyMojo {
5657

58+
private static final String PACKAGES_CHANGED_ERROR = """
59+
60+
Install of python packages is based on lock file %s,
61+
but packages and their version constraints in graalpy-maven-plugin configuration are different then previously used to generate the lock file.
62+
63+
Packages currently declared in graalpy-maven-plugin configuration: %s
64+
Packages which were used to generate the lock file: %s
65+
66+
The lock file has to be refreshed by running the maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
67+
68+
For more information, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools#Python-Dependency-Management
69+
""";
70+
71+
protected static final String MISSING_LOCK_FILE_WARNING = """
72+
73+
The list of installed Python packages does not match the packages specified in the graalpy-maven-plugin configuration.
74+
This could indicate that either extra dependencies were installed or some packages were installed with a more specific versions than declared.
75+
76+
In such cases, it is strongly recommended to lock the Python dependencies by executing the Maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
77+
78+
For more details on managing Python dependencies, please refer to https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools#Python-Dependency-Management
79+
80+
""";
81+
5782
public void execute() throws MojoExecutionException {
5883
preExec(true);
5984

@@ -67,9 +92,13 @@ public void execute() throws MojoExecutionException {
6792
private void manageVenv() throws MojoExecutionException {
6893
Path venvDirectory = getVenvDirectory();
6994
MavenDelegateLog log = new MavenDelegateLog(getLog());
70-
Path requirements = getLockFile();
95+
Path lockFile = getLockFile();
7196
try {
72-
VFSUtils.createVenv(venvDirectory, packages, requirements, PACKAGES_CHANGED_ERROR, MISSING_LOCK_FILE_WARNING, createLauncher(), getGraalPyVersion(project), log);
97+
VFSUtils.createVenv(venvDirectory, packages, lockFile, MISSING_LOCK_FILE_WARNING, createLauncher(), getGraalPyVersion(project), log);
98+
} catch(PackagesChangedException pce) {
99+
String pluginPkgsString = pce.getPluginPackages().isEmpty() ? "None" : String.join(", ", pce.getPluginPackages());
100+
String lockFilePkgsString = pce.getLockFilePackages().isEmpty() ? "None" : String.join(", ", pce.getLockFilePackages());
101+
throw new MojoExecutionException(String.format(PACKAGES_CHANGED_ERROR, lockFile, pluginPkgsString, lockFilePkgsString));
73102
} catch (IOException e) {
74103
throw new MojoExecutionException(String.format("failed to create venv %s", venvDirectory), e);
75104
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@
5555
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
5656
public class LockPackagesMojo extends AbstractGraalPyMojo {
5757

58+
protected static final String LOCK_FILE_HEADER = """
59+
This file was generated by maven goal 'org.graalvm.python:graalpy-maven-plugin:lock-packages'.
60+
61+
WARNING: Any manual changes are done at your own risk and will be overwritten when the goal is executed.
62+
63+
This file is meant to be tracked in a version control system.
64+
65+
This file contains a list of all required Python packages with their specific versions,
66+
based on the packages defined in the plugin configuration and their dependencies.
67+
""";
68+
5869
public void execute() throws MojoExecutionException {
5970
preExec(false);
6071
checkEmptyPackages();

0 commit comments

Comments
 (0)