Skip to content

Commit d14a5f7

Browse files
committed
generate launchers in standalone tool (prep for binary launchers)
(cherry picked from commit 22c84e2)
1 parent cc9324f commit d14a5f7

File tree

2 files changed

+65
-66
lines changed

2 files changed

+65
-66
lines changed

graalpython/lib-graalpython/modules/standalone/__main__.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import argparse
5353
import os
5454
import shutil
55+
import stat
5556
import subprocess
5657
import sys
5758
import tempfile
@@ -118,6 +119,62 @@
118119
MVN_CODE_PREFIX = "src/main/java"
119120
MVN_RESOURCE_PREFIX = "src/main/resources"
120121

122+
POSIX_LAUNCHER_SCRIPT = r"""
123+
#!/usr/bin/env bash
124+
125+
source="${BASH_SOURCE[0]}"
126+
while [ -h "$source" ] ; do
127+
prev_source="$source"
128+
source="$(readlink "$source")";
129+
if [[ "$source" != /* ]]; then
130+
# if the link was relative, it was relative to where it came from
131+
dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
132+
source="$dir/$source"
133+
fi
134+
done
135+
location="$( cd -P "$( dirname "$source" )" && pwd )"
136+
137+
if [ -z "$JAVA_HOME" ]; then
138+
JAVA=java
139+
else
140+
JAVA="${JAVA_HOME}/bin/java"
141+
fi
142+
143+
for var in "$@"; do
144+
args="${args}$(printf "\v")${var}"
145+
done
146+
147+
curdir=`pwd`
148+
export GRAAL_PYTHON_ARGS="${args}$(printf "\v")"
149+
mvn -f "${location}/pom.xml" exec:exec -Dexec.executable="${JAVA}" -Dexec.workingdir="${curdir}" -Dexec.args="--module-path %classpath '-Dorg.graalvm.launcher.executablename=$0' --module org.graalvm.py.launcher/com.oracle.graal.python.shell.GraalPythonMain"
150+
"""
151+
152+
WIN32_LAUNCHER_SCRIPT = r"""
153+
@echo off
154+
REM Invoke the GraalPy launcher through Maven, passing any arguments passed to
155+
REM this script via GRAAL_PYTHON_ARGS. To avoid having to deal with multiple
156+
REM layers of escaping, we store the arguments into GRAAL_PYTHON_ARGS delimited
157+
REM with vertical tabs.
158+
159+
REM Since BAT files cannot easily generate vertical tabs, we create a helper
160+
REM script to do it for us. We're calling Maven soon anyway, so Java must be
161+
REM available.
162+
set JAVA="%JAVA_HOME%/bin/java"
163+
if not defined JAVA_HOME set JAVA=java
164+
echo class VTabCreator { public static void main(String[] args) { System.out.print('\013'); } } > VTabCreator.java
165+
for /f "delims=" %%i in ('%JAVA% VTabCreator.java') do set VTAB=%%i
166+
del VTabCreator.java
167+
168+
REM Store each argument separated by vtab
169+
set GRAAL_PYTHON_ARGS=
170+
:loop
171+
set GRAAL_PYTHON_ARGS=%GRAAL_PYTHON_ARGS%%VTAB%%~1
172+
shift /1
173+
if not "%~1"=="" goto loop
174+
175+
mvn -f "%~dp0pom.xml" exec:exec -Dexec.executable=java -Dexec.args="--module-path %%classpath -Dorg.graalvm.launcher.executablename=%~0 --module org.graalvm.py.launcher/com.oracle.graal.python.shell.GraalPythonMain"
176+
"""
177+
121178
def get_file(*paths):
122179
return os.path.join(os.path.dirname(__file__), *paths)
123180

@@ -209,6 +266,14 @@ def create(self):
209266
# java sources
210267
shutil.copytree(os.path.join(os.path.dirname(__file__), "app"), os.path.join(target_dir))
211268

269+
# create launcher scripts to run graalpy
270+
posix_launcher = os.path.join(target_dir, "graalpy.sh")
271+
with open(posix_launcher, "w") as f:
272+
f.write(POSIX_LAUNCHER_SCRIPT)
273+
os.chmod(posix_launcher, os.stat(posix_launcher).st_mode | stat.S_IEXEC)
274+
with open(os.path.join(target_dir, "graalpy.cmd"), "w") as f:
275+
f.write(WIN32_LAUNCHER_SCRIPT)
276+
212277
virtual_filesystem_java_file = os.path.join(target_dir, MVN_CODE_PREFIX, "com", "mycompany", "javapython", VFS_JAVA_FILE)
213278
self.create_virtual_filesystem_file(virtual_filesystem_java_file, POLYGLOT_APP_JAVA_PKG)
214279

graalpython/lib-graalpython/modules/standalone/app/graalpy.sh

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)