Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ public final class VFSUtils {
""";

private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");

public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : "graalpy.sh";
public static final String LAUNCHER_NAME = IS_WINDOWS ? "graalpy.exe" : IS_MAC ? "graalpy" : "graalpy.sh";

private static final String GRAALPY_MAIN_CLASS = "com.oracle.graal.python.shell.GraalPythonMain";

Expand Down Expand Up @@ -848,9 +849,9 @@ private static Path ensureLauncher(Launcher launcherArgs, BuildToolLog log) thro
}
}

private static boolean checkWinLauncherJavaPath(Path venvCfg, Path java) {
private static boolean checkPyVenvCfgFile(Path pyVenvCfg, Path java) {
try {
for (String line : Files.readAllLines(venvCfg)) {
for (String line : Files.readAllLines(pyVenvCfg)) {
if (line.trim().startsWith("venvlauncher_command = " + java)) {
return true;
}
Expand All @@ -871,30 +872,16 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t
Path java = Paths.get(System.getProperty("java.home"), "bin", "java");
String classpath = String.join(File.pathSeparator, launcherArgs.computeClassPath());
String extraJavaOptions = String.join(" ", GraalPyRunner.getExtraJavaOptions());
if (!IS_WINDOWS) {
// we do not bother checking if it exists and has correct java home, since it is
// simple
// to regenerate the launcher
var script = formatMultiline("""
#!/usr/bin/env bash
%s --enable-native-access=ALL-UNNAMED %s -classpath %s %s --python.Executable="$0" "$@"
""", java, extraJavaOptions, String.join(File.pathSeparator, classpath), GRAALPY_MAIN_CLASS);
try {
Files.writeString(launcherArgs.launcherPath, script);
var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath);
perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE,
PosixFilePermission.OTHERS_EXECUTE));
Files.setPosixFilePermissions(launcherArgs.launcherPath, perms);
} catch (IOException e) {
throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e);
if (IS_MAC || IS_WINDOWS) {
if (Files.exists(launcherArgs.launcherPath) && checkPyVenvCfgFile(launcherArgs.launcherPath.getParent().resolve("pyvenv.cfg"), java)) {
return;
}
} else if (!Files.exists(launcherArgs.launcherPath)
|| !checkWinLauncherJavaPath(launcherArgs.launcherPath.getParent().resolve("pyenv.cfg"), java)) {
// on windows, generate a venv launcher that executes the java command
var launcherFolder = IS_WINDOWS ? "nt" : "macos";
var launcherName = IS_WINDOWS ? "graalpy.exe" : "graalpy";
var script = formatMultiline("""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share this code with the Windows branch? Seems like only this line in the script would need parametrization:

vl = os.path.join(venv.__path__[0], 'scripts', 'macos', 'graalpy')

otherwise it is the same?

On Windows we try to avoid regenerating the launcher to speed up the build:

(!Files.exists(launcherArgs.launcherPath)
				|| !checkWinLauncherJavaPath(launcherArgs.launcherPath.getParent().resolve("pyenv.cfg"), java)) {

do you see any issue with doing that on MacOS? checkWinLauncherJavaPath should be renamed, but what it does inside should apply just fine to the MacOS launcher AFAICS.

Up to your consideration: since we now have more conditions, I would find this if-else structure more readable:

if (IS_WINDOWS || IS_MAC_OS) {
    if (...condition if we need to regenerate the launcher...) {
        ...
    }    
} else {
    // we do not bother checking if it exists and has correct java home, since it is
    // simple to regenerate the launcher
    ...
}

import os, shutil, struct, venv
from pathlib import Path
vl = os.path.join(venv.__path__[0], 'scripts', 'nt', 'graalpy.exe')
vl = os.path.join(venv.__path__[0], 'scripts', '%s', '%s')
tl = os.path.join(r'%s')
os.makedirs(Path(tl).parent.absolute(), exist_ok=True)
shutil.copy(vl, tl)
Expand All @@ -903,13 +890,14 @@ private static void generateLaunchers(Launcher launcherArgs, BuildToolLog log) t
with open(pyvenvcfg, 'w', encoding='utf-8') as f:
f.write('venvlauncher_command = ')
f.write(cmd)
""", launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
""", launcherFolder, launcherName, launcherArgs.launcherPath, java, extraJavaOptions, classpath, GRAALPY_MAIN_CLASS);
File tmp;
try {
tmp = File.createTempFile("create_launcher", ".py");
} catch (IOException e) {
throw new IOException("failed to create tmp launcher", e);
}
System.out.println("Created temporary launcher file: " + tmp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left-out prinlnt?

tmp.deleteOnExit();
try (var wr = new FileWriter(tmp, StandardCharsets.UTF_8)) {
wr.write(script);
Expand All @@ -923,6 +911,24 @@ with open(pyvenvcfg, 'w', encoding='utf-8') as f:
throw new IOException("failed to run Graalpy launcher", e);
}
}
else {
// we do not bother checking if it exists and has correct java home, since it is
// simple
// to regenerate the launcher
var script = formatMultiline("""
#!/usr/bin/env bash
%s --enable-native-access=ALL-UNNAMED %s -classpath %s %s --python.Executable="$0" "$@"
""", java, extraJavaOptions, String.join(File.pathSeparator, classpath), GRAALPY_MAIN_CLASS);
try {
Files.writeString(launcherArgs.launcherPath, script);
var perms = Files.getPosixFilePermissions(launcherArgs.launcherPath);
perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE,
PosixFilePermission.OTHERS_EXECUTE));
Files.setPosixFilePermissions(launcherArgs.launcherPath, perms);
} catch (IOException e) {
throw new IOException(String.format("failed to create launcher %s", launcherArgs.launcherPath), e);
}
}
}

private static boolean installWantedPackages(Path venvDirectory, List<String> packages,
Expand Down
Loading