Skip to content

Commit 867ec65

Browse files
committed
Add optional virtual environment configuration scripts to easy running
lwr.
1 parent f4004cb commit 867ec65

5 files changed

Lines changed: 168 additions & 0 deletions

File tree

setup_venv.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
project_directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
cd $project_directory
5+
if [ ! -e .venv ]
6+
then
7+
# If virtualenv is not already installed, install it locally
8+
command -v virtualenv >/dev/null 2>&1 || { . tools/install_virtualenv.sh; }
9+
10+
# Install venv
11+
python tools/install_venv.py
12+
fi
13+

tools/install_venv.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# File copied OpenStack Horzion project.
2+
# https://github.com/openstack/horizon
3+
4+
# Copyright 2012 United States Government as represented by the
5+
# Administrator of the National Aeronautics and Space Administration.
6+
# All Rights Reserved.
7+
#
8+
# Copyright 2012 OpenStack, LLC
9+
#
10+
# Copyright 2012 Nebula, Inc.
11+
#
12+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
13+
# not use this file except in compliance with the License. You may obtain
14+
# a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing, software
19+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
21+
# License for the specific language governing permissions and limitations
22+
# under the License.
23+
24+
import os
25+
import subprocess
26+
import sys
27+
28+
29+
ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
30+
VENV = os.path.join(ROOT, '.venv')
31+
WITH_VENV = os.path.join(ROOT, 'tools', 'with_venv.sh')
32+
PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
33+
34+
def die(message, *args):
35+
print >> sys.stderr, message % args
36+
sys.exit(1)
37+
38+
39+
def run_command(cmd, redirect_output=True, check_exit_code=True, cwd=ROOT,
40+
die_message=None):
41+
"""
42+
Runs a command in an out-of-process shell, returning the
43+
output of that command. Working directory is ROOT.
44+
"""
45+
if redirect_output:
46+
stdout = subprocess.PIPE
47+
else:
48+
stdout = None
49+
50+
proc = subprocess.Popen(cmd, cwd=cwd, stdout=stdout)
51+
output = proc.communicate()[0]
52+
if check_exit_code and proc.returncode != 0:
53+
if die_message is None:
54+
die('Command "%s" failed.\n%s', ' '.join(cmd), output)
55+
else:
56+
die(die_message)
57+
return output
58+
59+
60+
HAS_EASY_INSTALL = bool(run_command(['which', 'easy_install'],
61+
check_exit_code=False).strip())
62+
HAS_VIRTUALENV = bool(run_command(['which', 'virtualenv'],
63+
check_exit_code=False).strip())
64+
65+
66+
def check_dependencies():
67+
"""Make sure virtualenv is in the path."""
68+
69+
print 'Checking dependencies...'
70+
if not HAS_VIRTUALENV:
71+
print 'Virtual environment not found.'
72+
# Try installing it via easy_install...
73+
if HAS_EASY_INSTALL:
74+
print 'Installing virtualenv via easy_install...',
75+
run_command(['easy_install', 'virtualenv'],
76+
die_message='easy_install failed to install virtualenv'
77+
'\ndevelopment requires virtualenv, please'
78+
' install it using your favorite tool')
79+
if not run_command(['which', 'virtualenv']):
80+
die('ERROR: virtualenv not found in path.\n\ndevelopment '
81+
' requires virtualenv, please install it using your'
82+
' favorite package management tool and ensure'
83+
' virtualenv is in your path')
84+
print 'virtualenv installation done.'
85+
else:
86+
die('easy_install not found.\n\nInstall easy_install'
87+
' (python-setuptools in ubuntu) or virtualenv by hand,'
88+
' then rerun.')
89+
print 'dependency check done.'
90+
91+
92+
def create_virtualenv(venv=VENV):
93+
"""Creates the virtual environment and installs PIP only into the
94+
virtual environment
95+
"""
96+
print 'Creating venv...',
97+
run_command(['virtualenv', '-q', '--no-site-packages', VENV])
98+
print 'done.'
99+
print 'Installing pip in virtualenv...',
100+
if not run_command([WITH_VENV, 'easy_install', 'pip']).strip():
101+
die("Failed to install pip.")
102+
print 'done.'
103+
print 'Installing distribute in virtualenv...'
104+
pip_install('distribute>=0.6.24')
105+
print 'done.'
106+
107+
108+
def pip_install(*args):
109+
args = [WITH_VENV, 'pip', 'install', '--upgrade'] + list(args)
110+
run_command(args, redirect_output=False)
111+
112+
113+
def install_dependencies(venv=VENV):
114+
print "Installing dependencies..."
115+
print "(This may take several minutes, don't panic)"
116+
pip_install('-r', PIP_REQUIRES)
117+
118+
def print_summary():
119+
summary = """
120+
galaxy-vm-launcher environment setup is complete.
121+
122+
To activate the virtualenv for the extent of your current shell session you
123+
can run:
124+
125+
$ source .venv/bin/activate
126+
"""
127+
print summary
128+
129+
130+
def main():
131+
check_dependencies()
132+
create_virtualenv()
133+
install_dependencies()
134+
print_summary()
135+
136+
if __name__ == '__main__':
137+
main()

tools/install_virtualenv.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
export LOCAL_PYTHON=$HOME/.gvl_python
3+
export PYTHON_VERSION=${PYTHON_VERSION:-`python -c "import sys; rev = sys.version_info; str = '%d.%d' % (rev[0], rev[1]); print str"`}
4+
mkdir -p $LOCAL_PYTHON/lib/python$PYTHON_VERSION/site-packages
5+
export PYTHONPATH=$LOCAL_PYTHON/lib/python$PYTHON_VERSION/site-packages:$PYTHONPATH
6+
export PATH=$LOCAL_PYTHON/bin:$PATH
7+
easy_install --prefix=$LOCAL_PYTHON pip virtualenv

tools/pip-requires

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
paste
2+
wsgiutils
3+
PasteScript
4+
PasteDeploy
5+
simplejson
6+
webob
7+
pyOpenSSL

tools/with_venv.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
TOOLS=`dirname $0`
3+
VENV=$TOOLS/../.venv
4+
source $VENV/bin/activate && $@

0 commit comments

Comments
 (0)