Skip to content

Commit da87661

Browse files
committed
modify our venv scripts to set GRAAL_PYTHON_OPTIONS on activation
1 parent 4e2a1d3 commit da87661

File tree

7 files changed

+104
-1
lines changed

7 files changed

+104
-1
lines changed

graalpython/lib-python/3/venv/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def __init__(self, system_site_packages=False, clear=False,
4646
self.symlinks = symlinks
4747
self.upgrade = upgrade
4848
self.with_pip = with_pip
49+
# Truffle change
50+
if with_pip:
51+
logger.warning("Not install pip on GraalPython")
52+
self.with_pip = False
53+
# End Truffle change
4954
self.prompt = prompt
5055

5156
def create(self, env_dir):
@@ -113,6 +118,48 @@ def create_if_needed(d):
113118
context.executable = executable
114119
context.python_dir = dirname
115120
context.python_exe = exename
121+
122+
# Truffle change: when our executable is not just a file (e.g. we're
123+
# running through java), we need to provide a script for launching
124+
if hasattr(sys, "executable_list"):
125+
# our 'sys.executable' is really a full commandline, so we need a
126+
# script to launch it
127+
import atexit, tempfile
128+
tempdir = tempfile.mkdtemp()
129+
script = os.path.join(tempdir, "graalpython")
130+
if sys.platform == 'win32':
131+
script += ".bat"
132+
133+
with open(script, "w") as f:
134+
if sys.platform != "win32":
135+
f.write("#!/bin/sh\n")
136+
for s in sys.executable_list:
137+
if " " in s:
138+
f.write('"')
139+
f.write(s.replace('"', '\\"'))
140+
f.write('" ')
141+
else:
142+
f.write(s)
143+
f.write(" ")
144+
if sys.platform == "win32":
145+
f.write("%*")
146+
else:
147+
f.write("$@")
148+
149+
if sys.platform != "win32":
150+
os.chmod(script, 0o777)
151+
152+
atexit.register(lambda: os.unlink(script))
153+
atexit.register(lambda: os.rmdir(tempdir))
154+
155+
dirname, exename = context.python_dir, context.python_exe = sys.graal_python_home, "graalpython"
156+
context.executable = script
157+
158+
if self.symlinks:
159+
logger.warning("We're not using symlinks on GraalPython")
160+
self.symlinks = False
161+
# End of Truffle change
162+
116163
if sys.platform == 'win32':
117164
binname = 'Scripts'
118165
incpath = 'Include'
@@ -285,6 +332,16 @@ def replace_variables(self, text, context):
285332
text = text.replace('__VENV_PROMPT__', context.prompt)
286333
text = text.replace('__VENV_BIN_NAME__', context.bin_name)
287334
text = text.replace('__VENV_PYTHON__', context.env_exe)
335+
# Truffle change: we need to set some extra options for the launcher to work
336+
text = text.replace(
337+
'__VENV_GRAAL_PYTHON_OPTIONS__',
338+
"--python.CoreHome='%s' --python.StdLibHome='%s' --python.ExecutablePath='%s'" % (
339+
sys.graal_python_core_home,
340+
sys.graal_python_stdlib_home,
341+
context.env_exe,
342+
)
343+
)
344+
# End truffle change
288345
return text
289346

290347
def install_scripts(self, context, path):

graalpython/lib-python/3/venv/scripts/common/activate

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ deactivate () {
1414
unset _OLD_VIRTUAL_PYTHONHOME
1515
fi
1616

17+
if [ -n "${_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS:-}" ] ; then
18+
GRAAL_PYTHON_OPTIONS="${_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS:-}"
19+
export GRAAL_PYTHON_OPTIONS
20+
unset _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS
21+
else
22+
unset GRAAL_PYTHON_OPTIONS
23+
fi
24+
1725
# This should detect bash and zsh, which have a hash command that must
1826
# be called to get it to forget past commands. Without forgetting
1927
# past commands the $PATH changes we made may not be respected
@@ -44,6 +52,10 @@ _OLD_VIRTUAL_PATH="$PATH"
4452
PATH="$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH"
4553
export PATH
4654

55+
_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS="$GRAAL_PYTHON_OPTIONS"
56+
GRAAL_PYTHON_OPTIONS="__VENV_GRAAL_PYTHON_OPTIONS__ $GRAAL_PYTHON_OPTIONS"
57+
export GRAAL_PYTHON_OPTIONS
58+
4759
# unset PYTHONHOME if set
4860
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
4961
# could use `if (set -u; : $PYTHONHOME) ;` in bash

graalpython/lib-python/3/venv/scripts/nt/Activate.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ function global:deactivate ([switch]$NonDestructive) {
1515
remove-item env:_OLD_VIRTUAL_PATH
1616
}
1717

18+
remove-item env:GRAAL_PYTHON_OPTIONS
19+
if (Test-Path env:_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS) {
20+
copy-item env:_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS env:GRAAL_PYTHON_OPTIONS
21+
remove-item env:_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS
22+
}
23+
1824
if (Test-Path env:VIRTUAL_ENV) {
1925
remove-item env:VIRTUAL_ENV
2026
}
@@ -49,3 +55,6 @@ if (Test-Path env:PYTHONHOME) {
4955
# Add the venv to the PATH
5056
copy-item env:PATH env:_OLD_VIRTUAL_PATH
5157
$env:PATH = "$env:VIRTUAL_ENV\__VENV_BIN_NAME__;$env:PATH"
58+
59+
copy-item env:GRAAL_PYTHON_OPTIONS env:_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS
60+
$env:GRAAL_PYTHON_OPTIONS = "$env:__VENV_GRAAL_PYTHON_OPTIONS__ $env:GRAAL_PYTHON_OPTIONS"

graalpython/lib-python/3/venv/scripts/nt/activate.bat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ if defined _OLD_VIRTUAL_PATH (
3838

3939
set "PATH=%VIRTUAL_ENV%\__VENV_BIN_NAME__;%PATH%"
4040

41+
if defined _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS (
42+
set "GRAAL_PYTHON_OPTIONS=%_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS%"
43+
) else (
44+
set "_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS=%GRAAL_PYTHON_OPTIONS%"
45+
)
46+
47+
set "GRAAL_PYTHON_OPTIONS=__VENV_GRAAL_PYTHON_OPTIONS__ %GRAAL_PYTHON_OPTIONS%"
48+
4149
:END
4250
if defined _OLD_CODEPAGE (
4351
"%SystemRoot%\System32\chcp.com" %_OLD_CODEPAGE% > nul

graalpython/lib-python/3/venv/scripts/nt/deactivate.bat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ if defined _OLD_VIRTUAL_PATH (
1616

1717
set _OLD_VIRTUAL_PATH=
1818

19+
set GRAAL_PYTHON_OPTIONS=
20+
if defined _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS (
21+
set "GRAAL_PYTHON_OPTIONS=%_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS%"
22+
)
23+
24+
set _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS=
25+
1926
set VIRTUAL_ENV=
2027

2128
:END

graalpython/lib-python/3/venv/scripts/posix/activate.csh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Created by Davide Di Blasi <[email protected]>.
44
# Ported to Python 3.3 venv by Andrew Svetlov <[email protected]>
55

6-
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate'
6+
alias deactivate 'unset GRAAL_PYTHON_OPTIONS; test $?_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS != 0 && setenv GRAAL_PYTHON_OPTIONS "$_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS" && unset _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS; test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate'
77

88
# Unset irrelevant variables.
99
deactivate nondestructive
@@ -13,6 +13,8 @@ setenv VIRTUAL_ENV "__VENV_DIR__"
1313
set _OLD_VIRTUAL_PATH="$PATH"
1414
setenv PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH"
1515

16+
set _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS="$GRAAL_PYTHON_OPTIONS"
17+
setenv GRAAL_PYTHON_OPTIONS "__VENV_GRAAL_PYTHON_OPTIONS__ $GRAAL_PYTHON_OPTIONS"
1618

1719
set _OLD_VIRTUAL_PROMPT="$prompt"
1820

graalpython/lib-python/3/venv/scripts/posix/activate.fish

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ function deactivate -d "Exit virtualenv and return to normal shell environment"
77
set -gx PATH $_OLD_VIRTUAL_PATH
88
set -e _OLD_VIRTUAL_PATH
99
end
10+
set -e GRAAL_PYTHON_OPTIONS
11+
if test -n "$_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS"
12+
set -gx GRAAL_PYTHON_OPTIONS $_OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS
13+
set -e _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS
14+
end
1015
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
1116
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
1217
set -e _OLD_VIRTUAL_PYTHONHOME
@@ -34,6 +39,9 @@ set -gx VIRTUAL_ENV "__VENV_DIR__"
3439
set -gx _OLD_VIRTUAL_PATH $PATH
3540
set -gx PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__" $PATH
3641

42+
set -gx _OLD_VIRTUAL_GRAAL_PYTHON_OPTIONS $GRAAL_PYTHON_OPTIONS
43+
set -gx GRAAL_PYTHON_OPTIONS "__VENV_GRAAL_PYTHON_OPTIONS__ " $GRAAL_PYTHON_OPTIONS
44+
3745
# unset PYTHONHOME if set
3846
if set -q PYTHONHOME
3947
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME

0 commit comments

Comments
 (0)