Commit 92bb5bcc authored by Kirill Smelkov's avatar Kirill Smelkov

gpython: Detect buildout and don't expect exedir to be on sys.path[0] in that case

When buildout generates scripts, it injects the following prologue for
them:

   #!/path/to/underlying/python

   import sys
   sys.path[0:0] = [
     'path/to/egg1',
     'path/to/egg2',
     ...
   ]

   import SCRIPT
   SCRIPT.main()

This way path to eggs are inserted before what was originally
sys.path[0] and it breaks gpython check that `sys.path[0] == exedir`.

-> Fix it by detecting buildout and in that case verifying that exedir
is only present in sys.path, not located in sys.path[0].
parent 483df486
...@@ -284,10 +284,20 @@ def main(): ...@@ -284,10 +284,20 @@ def main():
# /path/to/gpython is unneccessary and would create difference in behaviour # /path/to/gpython is unneccessary and would create difference in behaviour
# in between gpython and python. # in between gpython and python.
exedir = exe[:exe.rindex(os.sep)] # dirname, but os.path cannot be imported yet exedir = exe[:exe.rindex(os.sep)] # dirname, but os.path cannot be imported yet
if sys.path[0] != exedir: if sys.path[0] == exedir:
raise RuntimeError('gpython: internal error: sys.path[0] was not set by underlying python to dirname(gpython):' del sys.path[0]
'\n\n\tgpython:\t%s\n\tsys.path[0]:\t%s' % (exe, sys.path[0])) else:
del sys.path[0] # buildout injects `sys.path[0:0] = eggs` into python scripts.
# detect that and remove sys.path entry corresponding to exedir.
if not _is_buildout_script(exe):
raise RuntimeError('gpython: internal error: sys.path[0] was not set by underlying python to dirname(gpython):'
'\n\n\tgpython:\t%s\n\tsys.path[0]:\t%s' % (exe, sys.path[0]))
else:
if exedir in sys.path:
sys.path.remove(exedir)
else:
raise RuntimeError('gpython: internal error: sys.path does not contain dirname(gpython):'
'\n\n\tgpython:\t%s\n\tsys.path:\t%s' % (exe, sys.path))
# extract and process -X # extract and process -X
# -X gpython.runtime=(gevent|threads) + $GPYTHON_RUNTIME # -X gpython.runtime=(gevent|threads) + $GPYTHON_RUNTIME
...@@ -360,5 +370,17 @@ def main(): ...@@ -360,5 +370,17 @@ def main():
# tail to pymain # tail to pymain
pymain(argv, init) pymain(argv, init)
# _is_buildout_script returns whether file @path is generated as python buildout script.
def _is_buildout_script(path):
with open(path, 'r') as f:
src = f.read()
# buildout injects the following prologues into python scripts:
# sys.path[0:0] = [
# ...
# ]
return ('\nsys.path[0:0] = [\n' in src)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment