setup_path_on_import: Replace with setup_path function.

isort 5 knows not to reorder imports across function calls, so this
will stop isort from breaking our code.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-01-07 15:06:39 -08:00 committed by Tim Abbott
parent 5ba593f124
commit 687553a661
16 changed files with 59 additions and 32 deletions

View File

@ -184,8 +184,8 @@ highlighting. The system is largely managed by the code in
amounts of disk over time.
* **Scripts**. Often, we want a script running in production to use
the Zulip virtualenv. To make that work without a lot of duplicated
code, we have a helpful library,
`scripts/lib/setup_path_on_import.py`, which on import will put the
code, we have a helpful function,
`scripts.lib.setup_path.setup_path`, which on import will put the
currently running Python script into the Zulip virtualenv. This is
called by `./manage.py` to ensure that our Django code always uses
the correct virtualenv as well.

View File

@ -10,7 +10,10 @@ if sys.version_info <= (3, 0):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
from scripts.lib.zulip_tools import assert_not_running_as_root
if __name__ == "__main__":

View File

@ -17,7 +17,9 @@ import os
sys.path.append('.')
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
import django

View File

@ -6,7 +6,9 @@ Nagios plugin to check the length of the FTS update log.
import sys
sys.path.append('/home/zulip/deployments/current')
try:
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
except ImportError:
pass

View File

@ -9,14 +9,16 @@
import sys
# We want to use a virtualenv in production, which will be in /home/zulip/deployments/current.
# So we should add that path to sys.path and then import scripts.lib.setup_path_on_import.
# So we should add that path to sys.path and then call setup_path.
# But this file is also used in development, where the above path will not exist.
# So `import scripts.lib.setup_path_on_import` will raise an ImportError.
# So `from scripts.lib.setup_path import setup_path` will raise an ImportError.
# In development, we just want to skip this step since we know that virtualenv will already be in use.
# So catch the ImportError and do nothing.
sys.path.append('/home/zulip/deployments/current')
try:
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
except ImportError:
pass

View File

@ -11,7 +11,9 @@ import os
import sys
sys.path.append('/home/zulip/deployments/current')
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
import django
from django.utils.timezone import now as timezone_now

View File

@ -4,7 +4,9 @@ import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.conf import settings

View File

@ -6,7 +6,9 @@ import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'

16
scripts/lib/setup_path.py Normal file
View File

@ -0,0 +1,16 @@
"""
Use libraries from a virtualenv (by modifying sys.path) in production.
"""
import os
import sys
def setup_path() -> None:
if os.path.basename(sys.prefix) != "zulip-py3-venv":
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
venv = os.path.join(BASE_DIR, "zulip-py3-venv")
activate_this = os.path.join(venv, "bin", "activate_this.py")
activate_locals = dict(__file__=activate_this)
exec(open(activate_this).read(), activate_locals)
if not os.path.exists(activate_locals["site_packages"]):
raise RuntimeError(venv + " was not set up for this Python version")

View File

@ -1,15 +0,0 @@
"""
Use libraries from a virtualenv (by modifying sys.path) in production.
"""
import os
import sys
if os.path.basename(sys.prefix) != "zulip-py3-venv":
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
venv = os.path.join(BASE_DIR, "zulip-py3-venv")
activate_this = os.path.join(venv, "bin", "activate_this.py")
activate_locals = dict(__file__=activate_this)
exec(open(activate_this).read(), activate_locals)
if not os.path.exists(activate_locals["site_packages"]):
raise RuntimeError(venv + " was not set up for this Python version")

View File

@ -6,7 +6,10 @@ import sys
BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../..")
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
from zproject import settings
import pylibmc

View File

@ -8,7 +8,9 @@ from typing import Dict, List
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'

View File

@ -24,7 +24,9 @@ def restore_backup(tarball_file):
su_to_zulip(save_suid=True)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
# First, we unpack the /etc/zulip configuration, so we know how
# this server is supposed to be configured (and can import

View File

@ -12,7 +12,6 @@ def check_pyflakes(files, options):
# type: (List[str], argparse.Namespace) -> bool
suppress_patterns = [
("scripts/lib/pythonrc.py", "imported but unused"),
('', "'scripts.lib.setup_path_on_import' imported but unused"),
# Intentionally imported by zerver/lib/webhooks/common.py
('', "'zerver.lib.exceptions.UnexpectedWebhookEventType' imported but unused"),

View File

@ -9,7 +9,10 @@ import sys
# We need settings so we can figure out where the prod-static directory is.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
os.environ['DJANGO_SETTINGS_MODULE'] = 'zproject.settings'
from django.conf import settings
from scripts.lib.node_cache import setup_node_modules

View File

@ -18,7 +18,9 @@ import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
import scripts.lib.setup_path_on_import
from scripts.lib.setup_path import setup_path
setup_path()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")