Skip to content

Commit

Permalink
setup_path_on_import: Replace with setup_path function.
Browse files Browse the repository at this point in the history
isort 5 knows not to reorder imports across function calls, so this
will stop isort from breaking our code.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and timabbott committed Feb 25, 2020
1 parent 5ba593f commit 687553a
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 32 deletions.
4 changes: 2 additions & 2 deletions docs/subsystems/dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

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__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions puppet/zulip/files/postgresql/process_fts_updates
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion scripts/get-django-setting
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion scripts/lib/queue_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

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'

Expand Down
16 changes: 16 additions & 0 deletions scripts/lib/setup_path.py
Original file line number Diff line number Diff line change
@@ -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")
15 changes: 0 additions & 15 deletions scripts/lib/setup_path_on_import.py

This file was deleted.

5 changes: 4 additions & 1 deletion scripts/setup/flush-memcached
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion scripts/setup/generate_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

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'

Expand Down
4 changes: 3 additions & 1 deletion scripts/setup/restore-backup
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tools/linter_lib/pyflakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),

Expand Down
5 changes: 4 additions & 1 deletion tools/update-prod-static
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion zproject/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

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")

Expand Down

0 comments on commit 687553a

Please sign in to comment.