2016-06-25 17:07:13 +02:00
|
|
|
"""
|
|
|
|
Use libraries from a virtualenv (by modifying sys.path) in production.
|
|
|
|
"""
|
|
|
|
|
2017-09-22 18:30:18 +02:00
|
|
|
import os
|
2016-06-25 17:07:13 +02:00
|
|
|
import sys
|
|
|
|
|
2017-09-22 08:15:01 +02:00
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
2019-01-10 01:57:27 +01:00
|
|
|
|
|
|
|
venv = os.path.join(BASE_DIR, "zulip-py3-venv")
|
|
|
|
if sys.prefix != venv:
|
|
|
|
activate_this = os.path.join(venv, "bin", "activate_this.py")
|
2016-06-25 17:07:13 +02:00
|
|
|
# this file will exist in production
|
2019-01-10 01:57:27 +01:00
|
|
|
if os.path.exists(activate_this):
|
|
|
|
activate_locals = dict(__file__=activate_this)
|
scripts: Fix exec invocation for in-process virtualenv activation.
activate_this.py has always documented that it should be exec()ed with
locals = globals, and in virtualenv 16.0.0 it raises a NameError
otherwise.
As a simplified demonstration of the weird things that can go wrong
when locals ≠ globals:
>>> exec('a = 1; print([a])', {}, {})
[1]
>>> exec('a = 1; print([a for b in [1]])', {}, {})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'a' is not defined
>>> exec('a = 1; print([a for b in [1]])', {})
[1]
Top-level assignments go into locals, but from inside a new scope like
a list comprehension, they’re read out of globals, which doesn’t work.
Fixes #12030.
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-04-17 01:17:57 +02:00
|
|
|
exec(open(activate_this).read(), activate_locals)
|
2019-01-10 01:58:50 +01:00
|
|
|
if not os.path.exists(activate_locals["site_packages"]):
|
|
|
|
raise RuntimeError(venv + " was not set up for this Python version")
|