Skip to content

Commit 20a5d36

Browse files
horakivosteve-s
authored andcommitted
Merging generation of launchers for MacOS and Windows
1 parent 15adac2 commit 20a5d36

File tree

1 file changed

+11
-40
lines changed
  • org.graalvm.python.embedding.tools/src/main/java/org/graalvm/python/embedding/tools/vfs

1 file changed

+11
-40
lines changed

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

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,9 @@ private static Path ensureLauncher(Launcher launcherArgs, BuildToolLog log) thro
895895
}
896896
}
897897

898-
private static boolean checkWinLauncherJavaPath(Path venvCfg, Path java) {
898+
private static boolean checkPyVenvCfgFile(Path pyVenvCfg, Path java) {
899899
try {
900-
for (String line : Files.readAllLines(venvCfg)) {
900+
for (String line : Files.readAllLines(pyVenvCfg)) {
901901
if (line.trim().startsWith("venvlauncher_command = " + java)) {
902902
return true;
903903
}
@@ -918,11 +918,16 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t
918918
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
919919
String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath());
920920
String extraJavaOptions = String.join(" ", GraalPyRunner.getExtraJavaOptions());
921-
if (IS_MAC) {
921+
if (IS_MAC || IS_WINDOWS) {
922+
if (Files.exists(launcherArgs.launcherPath) && checkPyVenvCfgFile(launcherArgs.launcherPath.getParent().resolve("pyvenv.cfg"), java)) {
923+
return;
924+
}
925+
var launcherFolder = IS_WINDOWS ? "nt" : "macos";
926+
var launcherName = IS_WINDOWS ? "graalpy.exe" : "graalpy";
922927
var script = formatMultiline("""
923928
import os, shutil, struct, venv
924929
from pathlib import Path
925-
vl = os.path.join(venv.__path__[0], 'scripts', 'macos', 'graalpy')
930+
vl = os.path.join(venv.__path__[0], 'scripts', '%s', '%s')
926931
tl = os.path.join(r'%s')
927932
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
928933
shutil.copy(vl, tl)
@@ -931,7 +936,7 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t
931936
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
932937
f.write('venvlauncher_command = ')
933938
f.write(cmd)
934-
""", launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
939+
""", launcherFolder, launcherName, launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
935940
File tmp;
936941
try {
937942
tmp = File.createTempFile("create_launcher", ".py");
@@ -952,7 +957,7 @@ with open(pyvenvcfg, 'w', encoding='utf-8') as f:
952957
throw new IOException("failed to run Graalpy launcher", e);
953958
}
954959
}
955-
else if (!IS_WINDOWS) {
960+
else {
956961
// we do not bother checking if it exists and has correct java home, since it is
957962
// simple
958963
// to regenerate the launcher
@@ -969,40 +974,6 @@ else if (!IS_WINDOWS) {
969974
} catch (IOException e) {
970975
throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e);
971976
}
972-
} else if (!Files.exists(launcherArgs.launcherPath)
973-
|| !checkWinLauncherJavaPath(launcherArgs.launcherPath.getParent().resolve("pyenv.cfg"), java)) {
974-
// on windows, generate a venv launcher that executes the java command
975-
var script = formatMultiline("""
976-
import os, shutil, struct, venv
977-
from pathlib import Path
978-
vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe')
979-
tl = os.path.join(r'%s')
980-
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
981-
shutil.copy(vl, tl)
982-
cmd = r'%s --enable-native-access=ALL-UNNAMED %s -classpath "%s" %s'
983-
pyvenvcfg = os.path.join(os.path.dirname(tl), "pyvenv.cfg")
984-
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
985-
f.write('venvlauncher_command = ')
986-
f.write(cmd)
987-
""", launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
988-
File tmp;
989-
try {
990-
tmp = File.createTempFile("create_launcher", ".py");
991-
} catch (IOException e) {
992-
throw new IOException("failed to create tmp launcher", e);
993-
}
994-
tmp.deleteOnExit();
995-
try (var wr = new FileWriter(tmp, StandardCharsets.UTF_8)) {
996-
wr.write(script);
997-
} catch (IOException e) {
998-
throw new IOException(String.format("failed to write tmp launcher %s", tmp), e);
999-
}
1000-
1001-
try {
1002-
GraalPyRunner.run(classpath, log, tmp.getAbsolutePath());
1003-
} catch (InterruptedException e) {
1004-
throw new IOException("failed to run Graalpy launcher", e);
1005-
}
1006977
}
1007978
}
1008979

0 commit comments

Comments
 (0)