From fb742473e29c9e291ce7a48e9079ecaf8820a042 Mon Sep 17 00:00:00 2001 From: Glauber Costa Date: Wed, 6 Feb 2019 00:21:04 -0500 Subject: [PATCH] replace /usr/local as a source of packages in the python relocatable interpreter I was playing with the python3 interpreter trying to get pip to work, just to see how far we can go. We don't really need pip, but I figured it would be a good stress test to make sure that the process is working and robust. And it didn't really work, because although pip will correctly install things into $relocatable_root/local/lib, sys.path will still refer to a hardcoded /usr/local. While this should not affect Scylla, since we expect to have all our modules in out path anyway -- and that path is searched before /usr/local, it is still dangerous to make an absolute reference like this. Unfortunately, /usr/local/ it is included unconditionally by site.py, which is executed when the interpreter is started and there is no environment variable I found to change that (the help string refers to PYTHONNOUSERSITE, but I found no mention of that in site.py whatsoever) There is a way to tell site.py not to bother to add user sites, by passing the -s flag, which this patch does. Aside from doing that, we also enhance PYTHONPATH to include a reference to ./local/{lib,lib64}/python/site-packages. After applying this patch, I was able to build an interpreter containing only python3-pip and python3-setuptools, and build the relocatable environment from there. Signed-off-by: Glauber Costa Message-Id: <20190206052104.25927-1-glauber@scylladb.com> --- scripts/create-relocatable-python.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/scripts/create-relocatable-python.py b/scripts/create-relocatable-python.py index d95d35d479..7b9a71d4ce 100755 --- a/scripts/create-relocatable-python.py +++ b/scripts/create-relocatable-python.py @@ -102,16 +102,21 @@ def fix_dynload(ar, binpath, targetpath): ar.add(patched_binary, arcname=targetpath, recursive=False) def gen_python_thunk(ar, pybin): - thunk=b'''\ + base_thunk='''\ #!/bin/bash x="$(readlink -f "$0")" b="$(basename "$x")" d="$(dirname "$x")/.." ldso="$d/libexec/ld.so" realexe="$d/libexec/$b.bin" -exec -a "$0" "$ldso" "$realexe" "$@" +PYTHONPATH="$d/{sitepackages}:$d/{sitepackages64}:$PYTHONPATH" exec -a "$0" "$ldso" "$realexe" -s "$@" ''' + sitepackages = os.path.join("local/lib/", pybin, "site-packages") + sitepackages64 = os.path.join("local/lib64/", pybin, "site-packages") + + thunk = base_thunk.format(sitepackages=sitepackages, sitepackages64=sitepackages64).encode() + ti = tarfile.TarInfo(name=os.path.join("bin", pybin)) ti.size = len(thunk) ti.mode = 0o755