2016-06-21 19:37:36 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from os.path import dirname, abspath
|
|
|
|
import subprocess
|
|
|
|
from zulip_tools import run
|
|
|
|
|
|
|
|
ZULIP_PATH = dirname(dirname(dirname(abspath(__file__))))
|
|
|
|
VENV_CACHE_PATH = "/srv/zulip-venv-cache"
|
|
|
|
|
2016-06-28 06:35:19 +02:00
|
|
|
if 'TRAVIS' in os.environ:
|
2016-06-21 19:37:36 +02:00
|
|
|
# In Travis CI, we don't have root access
|
2016-06-28 06:35:19 +02:00
|
|
|
VENV_CACHE_PATH = "/home/travis/zulip-venv-cache"
|
2016-06-21 19:37:36 +02:00
|
|
|
|
|
|
|
if False:
|
|
|
|
# Don't add a runtime dependency on typing
|
2016-06-22 20:04:14 +02:00
|
|
|
from typing import List, Optional
|
2016-06-21 19:37:36 +02:00
|
|
|
|
2016-06-22 18:17:46 +02:00
|
|
|
VENV_DEPENDENCIES = [
|
2016-06-24 18:09:36 +02:00
|
|
|
"build-essential",
|
2016-06-22 18:17:46 +02:00
|
|
|
"libffi-dev",
|
2016-07-20 23:42:49 +02:00
|
|
|
"libfreetype6-dev", # Needed for image types with Pillow
|
|
|
|
"libz-dev", # Needed to handle compressed PNGs with Pillow
|
|
|
|
"libjpeg-dev", # Needed to handle JPEGs with Pillow
|
2016-06-22 18:17:46 +02:00
|
|
|
"libldap2-dev",
|
|
|
|
"libmemcached-dev",
|
|
|
|
"python3-dev", # Needed to install typed-ast dependency of mypy
|
|
|
|
"python-dev",
|
2016-06-24 18:09:36 +02:00
|
|
|
"python-pip",
|
2016-06-22 18:17:46 +02:00
|
|
|
"python-virtualenv",
|
2016-07-12 14:55:02 +02:00
|
|
|
"libxml2-dev", # Used for installing talon
|
|
|
|
"libxslt1-dev", # Used for installing talon
|
2016-07-20 18:45:06 +02:00
|
|
|
"libpq-dev", # Needed by psycopg2
|
2016-06-22 18:17:46 +02:00
|
|
|
]
|
|
|
|
|
2016-07-20 15:34:31 +02:00
|
|
|
def patch_activate_script(venv_path):
|
|
|
|
# venv_path should be what we want to have in VIRTUAL_ENV after patching
|
|
|
|
script_path = os.path.join(venv_path, "bin", "activate")
|
|
|
|
|
|
|
|
file_obj = open(script_path)
|
|
|
|
lines = file_obj.readlines()
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if line.startswith('VIRTUAL_ENV='):
|
|
|
|
lines[i] = 'VIRTUAL_ENV="%s"\n' % (venv_path,)
|
|
|
|
file_obj.close()
|
|
|
|
|
|
|
|
file_obj = open(script_path, 'w')
|
|
|
|
file_obj.write("".join(lines))
|
|
|
|
file_obj.close()
|
|
|
|
|
2016-06-21 19:37:36 +02:00
|
|
|
def setup_virtualenv(target_venv_path, requirements_file, virtualenv_args=None):
|
2016-06-22 20:04:14 +02:00
|
|
|
# type: (Optional[str], str, Optional[List[str]]) -> str
|
2016-06-21 19:37:36 +02:00
|
|
|
|
|
|
|
# Check if a cached version already exists
|
2016-06-22 14:42:08 +02:00
|
|
|
path = os.path.join(ZULIP_PATH, 'scripts', 'lib', 'hash_reqs.py')
|
2016-07-02 21:41:13 +02:00
|
|
|
output = subprocess.check_output([path, requirements_file], universal_newlines=True)
|
2016-06-21 19:37:36 +02:00
|
|
|
sha1sum = output.split()[0]
|
2016-06-22 20:04:14 +02:00
|
|
|
if target_venv_path is None:
|
|
|
|
cached_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, 'venv')
|
|
|
|
else:
|
|
|
|
cached_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, os.path.basename(target_venv_path))
|
2016-06-21 19:37:36 +02:00
|
|
|
success_stamp = os.path.join(cached_venv_path, "success-stamp")
|
|
|
|
if not os.path.exists(success_stamp):
|
|
|
|
do_setup_virtualenv(cached_venv_path, requirements_file, virtualenv_args or [])
|
|
|
|
run(["touch", success_stamp])
|
|
|
|
|
|
|
|
print("Using cached Python venv from %s" % (cached_venv_path,))
|
2016-06-22 20:04:14 +02:00
|
|
|
if target_venv_path is not None:
|
|
|
|
run(["sudo", "ln", "-nsf", cached_venv_path, target_venv_path])
|
2016-07-20 15:34:31 +02:00
|
|
|
patch_activate_script(target_venv_path)
|
2016-06-22 20:04:14 +02:00
|
|
|
activate_this = os.path.join(cached_venv_path, "bin", "activate_this.py")
|
2016-06-21 19:37:36 +02:00
|
|
|
exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
|
2016-06-22 20:04:14 +02:00
|
|
|
return cached_venv_path
|
2016-06-21 19:37:36 +02:00
|
|
|
|
|
|
|
def do_setup_virtualenv(venv_path, requirements_file, virtualenv_args):
|
|
|
|
# type: (str, str, List[str]) -> None
|
|
|
|
|
|
|
|
# Setup Python virtualenv
|
|
|
|
run(["sudo", "rm", "-rf", venv_path])
|
|
|
|
run(["sudo", "mkdir", "-p", venv_path])
|
|
|
|
run(["sudo", "chown", "{}:{}".format(os.getuid(), os.getgid()), venv_path])
|
|
|
|
run(["virtualenv"] + virtualenv_args + [venv_path])
|
|
|
|
|
|
|
|
# Switch current Python context to the virtualenv.
|
|
|
|
activate_this = os.path.join(venv_path, "bin", "activate_this.py")
|
|
|
|
exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
|
|
|
|
|
2016-07-02 13:02:11 +02:00
|
|
|
run(["pip", "install", "--upgrade", "pip", "wheel"])
|
2016-06-21 19:37:36 +02:00
|
|
|
run(["pip", "install", "--no-deps", "--requirement", requirements_file])
|
2016-06-28 21:02:53 +02:00
|
|
|
run(["sudo", "chmod", "-R", "a+rX", venv_path])
|