Skip to content

Commit 481212c

Browse files
committed
set graalpy paths depending on platform
1 parent 24e53f0 commit 481212c

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

graalpython/com.oracle.graal.python.test.integration/src/org/graalvm/python/embedding/utils/test/VirtualFileSystemTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import org.graalvm.polyglot.Context.Builder;
8383
import org.graalvm.polyglot.HostAccess;
8484
import org.graalvm.polyglot.PolyglotException;
85+
import org.graalvm.polyglot.Value;
8586
import org.graalvm.polyglot.io.FileSystem;
8687
import org.graalvm.polyglot.io.IOAccess;
8788
import org.graalvm.python.embedding.utils.GraalPyResources;
@@ -886,6 +887,62 @@ public void vfsMountPointTest() {
886887
}
887888
}
888889

890+
@SuppressWarnings("unchecked")
891+
@Test
892+
public void pythonPathsTest() throws IOException {
893+
Context ctx = GraalPyResources.createContext();
894+
String getPathsSource = "import sys; [__graalpython__.get_python_home_paths(), sys.path, sys.executable]";
895+
896+
Value paths = ctx.eval("python", getPathsSource);
897+
String defaultMountPoint = VirtualFileSystem.newBuilder().build().getMountPoint();
898+
assertEquals(IS_WINDOWS ? "X:\\graalpy_vfs" : "/graalpy_vfs", defaultMountPoint);
899+
checkPaths(paths.as(List.class), defaultMountPoint);
900+
901+
ctx = GraalPyResources.contextBuilder().build();
902+
paths = ctx.eval("python", getPathsSource);
903+
checkPaths(paths.as(List.class), defaultMountPoint);
904+
905+
VirtualFileSystem vfs = VirtualFileSystem.newBuilder().//
906+
unixMountPoint(VFS_UNIX_MOUNT_POINT).//
907+
windowsMountPoint(VFS_WIN_MOUNT_POINT).build();
908+
assertEquals(VFS_MOUNT_POINT, vfs.getMountPoint());
909+
ctx = GraalPyResources.contextBuilder(vfs).build();
910+
paths = ctx.eval("python", getPathsSource);
911+
checkPaths(paths.as(List.class), vfs.getMountPoint(), true);
912+
913+
Path resourcesDir = Files.createTempDirectory("python-resources");
914+
ctx = GraalPyResources.contextBuilder(resourcesDir).build();
915+
paths = ctx.eval("python", getPathsSource);
916+
checkPaths(paths.as(List.class), resourcesDir.toString());
917+
}
918+
919+
private static void checkPaths(List<Object> l, String pathPrefix) {
920+
checkPaths(l, pathPrefix, false);
921+
}
922+
923+
@SuppressWarnings("unchecked")
924+
private static void checkPaths(List<Object> l, String pathPrefix, boolean checkHome) {
925+
// python.PythonHome
926+
// TODO how to check python.PythonHome?
927+
// /org.graalvm.python.vfs/home has to be in fileslist.txt
928+
// - if it is in fileslist.txt, than it also has to contain the whole stdlib,
929+
// or other tests will fail when trying to load python
930+
// - the pythonHome value is implicitly covered in maven and gradle plugin tests
931+
// if (checkHome) {
932+
// assertTrue(((String) l.get(0)).contains(pathPrefix + File.separator + "home" +
933+
// File.separator +
934+
// "lib-graalpython"));
935+
// assertTrue(((String) l.get(0)).contains(pathPrefix + File.separator + "home" +
936+
// File.separator +
937+
// "lib-python" + File.separator + "3"));
938+
// }
939+
940+
// option python.PythonPath
941+
assertTrue(((List<Object>) l.get(1)).contains(pathPrefix + File.separator + "src"));
942+
// option python.Executable
943+
assertEquals(l.get(2), pathPrefix + (IS_WINDOWS ? "\\venv\\Scripts\\python.exe" : "/venv/bin/python"));
944+
}
945+
889946
private static Builder addTestOptions(Builder builder) {
890947
return builder.option("engine.WarnInterpreterOnly", "false");
891948
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def test_gradle_generated_app_external_resources(self):
304304
"package org.example;\nimport java.nio.file.Path;")
305305
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "org", "example", "GraalPy.java"),
306306
"GraalPyResources.createContext()",
307-
"GraalPyResources.contextBuilder(Path.of(\"python-resources\")).build()")
307+
"GraalPyResources.contextBuilder(Path.of(\"" + (resources_dir if "win32" != sys.platform else resources_dir.replace("\\", "\\\\")) + "\")).build()")
308308

309309
# patch build.gradle
310310
append(build_file, self.packages_termcolor_resource_dir(resources_dir))
@@ -756,9 +756,11 @@ def test_generated_app_external_resources(self):
756756
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
757757
"package it.pkg;",
758758
"package it.pkg;\nimport java.nio.file.Path;")
759+
if "win32" == sys.platform:
760+
resources_dir.replace("\\", "\\\\")
759761
util.replace_in_file(os.path.join(target_dir, "src", "main", "java", "it", "pkg", "GraalPy.java"),
760762
"GraalPyResources.createContext()",
761-
"GraalPyResources.contextBuilder(Path.of(\"python-resources\")).build()")
763+
"GraalPyResources.contextBuilder(Path.of(\"" + (resources_dir if "win32" != sys.platform else resources_dir.replace("\\", "\\\\")) + "\")).build()")
762764

763765
# patch pom.xml
764766
util.replace_in_file(os.path.join(target_dir, "pom.xml"),

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ public static Context.Builder contextBuilder(VirtualFileSystem vfs) {
307307
* @param resourcesDirectory the root directory with GraalPy specific embedding resources
308308
*/
309309
public static Context.Builder contextBuilder(Path resourcesDirectory) {
310-
String execPath = resourcesDirectory.resolve(VirtualFileSystem.VFS_VENV + "/bin/python").toAbsolutePath().toString();
310+
String execPath;
311+
if (VirtualFileSystem.isWindows()) {
312+
execPath = resourcesDirectory.resolve(VirtualFileSystem.VFS_VENV).resolve("Scripts").resolve("python.exe").toAbsolutePath().toString();
313+
} else {
314+
execPath = resourcesDirectory.resolve(VirtualFileSystem.VFS_VENV).resolve("bin").resolve("python").toAbsolutePath().toString();
315+
}
316+
311317
String homePath = resourcesDirectory.resolve(VirtualFileSystem.VFS_HOME).toAbsolutePath().toString();
312318
String srcPath = resourcesDirectory.resolve(VirtualFileSystem.VFS_SRC).toAbsolutePath().toString();
313319
return createContextBuilder().

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public synchronized String format(LogRecord lr) {
100100
/*
101101
* Root of the virtual filesystem in the resources.
102102
*/
103-
static final String VFS_ROOT = "/org.graalvm.python.vfs";
103+
private static final String VFS_ROOT = "/org.graalvm.python.vfs";
104104

105105
static final String VFS_HOME = "home";
106106
static final String VFS_VENV = "venv";

0 commit comments

Comments
 (0)