mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Factor out venv-creating code from provision.py.
Move setup_virtualenv and do_setup_virtualenv from provision.py to scripts/lib/setup_venv.py.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							f68a392250
						
					
				
				
					commit
					fc8d4f9ef5
				
			
							
								
								
									
										44
									
								
								provision.py
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								provision.py
									
									
									
									
									
								
							@@ -5,14 +5,11 @@ import logging
 | 
			
		||||
import platform
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
if False:
 | 
			
		||||
    # Don't add a runtime dependency on typing
 | 
			
		||||
    from typing import List
 | 
			
		||||
 | 
			
		||||
os.environ["PYTHONUNBUFFERED"] = "y"
 | 
			
		||||
 | 
			
		||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
 | 
			
		||||
from zulip_tools import run
 | 
			
		||||
from scripts.lib.setup_venv import setup_virtualenv
 | 
			
		||||
 | 
			
		||||
SUPPORTED_PLATFORMS = {
 | 
			
		||||
    "Ubuntu": [
 | 
			
		||||
@@ -25,13 +22,8 @@ NPM_VERSION = '3.9.3'
 | 
			
		||||
VENV_PATH = "/srv/zulip-venv"
 | 
			
		||||
PY3_VENV_PATH = "/srv/zulip-py3-venv"
 | 
			
		||||
ZULIP_PATH = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
VENV_CACHE_PATH = "/srv/zulip-venv-cache"
 | 
			
		||||
TRAVIS_NODE_PATH = os.path.join(os.environ['HOME'], 'node')
 | 
			
		||||
 | 
			
		||||
if '--travis' in sys.argv:
 | 
			
		||||
    # In Travis CI, we don't have root access
 | 
			
		||||
    VENV_CACHE_PATH = os.path.join(os.environ['HOME'], "zulip-venv-cache")
 | 
			
		||||
 | 
			
		||||
if not os.path.exists(os.path.join(ZULIP_PATH, ".git")):
 | 
			
		||||
    print("Error: No Zulip git repository present!")
 | 
			
		||||
    print("To setup the Zulip development environment, you should clone the code")
 | 
			
		||||
@@ -154,40 +146,6 @@ def install_npm():
 | 
			
		||||
    run(['sudo', 'ln', '-sf', npm_exe, travis_npm])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def setup_virtualenv(target_venv_path, requirements_file, virtualenv_args=None):
 | 
			
		||||
    # type: (str, str, List[str]) -> None
 | 
			
		||||
 | 
			
		||||
    # Check if a cached version already exists
 | 
			
		||||
    path = os.path.join(ZULIP_PATH, 'tools', 'hash_reqs.py')
 | 
			
		||||
    output = subprocess.check_output([path, requirements_file])
 | 
			
		||||
    sha1sum = output.split()[0]
 | 
			
		||||
    cached_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, os.path.basename(target_venv_path))
 | 
			
		||||
    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,))
 | 
			
		||||
    run(["sudo", "ln", "-nsf", cached_venv_path, target_venv_path])
 | 
			
		||||
    activate_this = os.path.join(target_venv_path, "bin", "activate_this.py")
 | 
			
		||||
    exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
    run(["pip", "install", "--upgrade", "pip"])
 | 
			
		||||
    run(["pip", "install", "--no-deps", "--requirement", requirements_file])
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    # type: () -> int
 | 
			
		||||
    run(["sudo", "apt-get", "update"])
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								scripts/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								scripts/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										0
									
								
								scripts/lib/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								scripts/lib/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										52
									
								
								scripts/lib/setup_venv.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								scripts/lib/setup_venv.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
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"
 | 
			
		||||
 | 
			
		||||
if '--travis' in sys.argv:
 | 
			
		||||
    # In Travis CI, we don't have root access
 | 
			
		||||
    VENV_CACHE_PATH = os.path.join(os.environ['HOME'], "zulip-venv-cache")
 | 
			
		||||
 | 
			
		||||
if False:
 | 
			
		||||
    # Don't add a runtime dependency on typing
 | 
			
		||||
    from typing import List
 | 
			
		||||
 | 
			
		||||
def setup_virtualenv(target_venv_path, requirements_file, virtualenv_args=None):
 | 
			
		||||
    # type: (str, str, List[str]) -> None
 | 
			
		||||
 | 
			
		||||
    # Check if a cached version already exists
 | 
			
		||||
    path = os.path.join(ZULIP_PATH, 'tools', 'hash_reqs.py')
 | 
			
		||||
    output = subprocess.check_output([path, requirements_file])
 | 
			
		||||
    sha1sum = output.split()[0]
 | 
			
		||||
    cached_venv_path = os.path.join(VENV_CACHE_PATH, sha1sum, os.path.basename(target_venv_path))
 | 
			
		||||
    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,))
 | 
			
		||||
    run(["sudo", "ln", "-nsf", cached_venv_path, target_venv_path])
 | 
			
		||||
    activate_this = os.path.join(target_venv_path, "bin", "activate_this.py")
 | 
			
		||||
    exec(open(activate_this).read(), {}, dict(__file__=activate_this)) # type: ignore # https://github.com/python/mypy/issues/1577
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
    run(["pip", "install", "--upgrade", "pip"])
 | 
			
		||||
    run(["pip", "install", "--no-deps", "--requirement", requirements_file])
 | 
			
		||||
		Reference in New Issue
	
	Block a user