|
| 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() |
0 commit comments