2 # vim: tabstop=4 shiftwidth=4 softtabstop=4
4 # Copyright 2010 United States Government as represented by the
5 # Administrator of the National Aeronautics and Space Administration.
8 # Copyright 2010 OpenStack LLC.
10 # Licensed under the Apache License, Version 2.0 (the "License"); you may
11 # not use this file except in compliance with the License. You may obtain
12 # a copy of the License at
14 # http://www.apache.org/licenses/LICENSE-2.0
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 # License for the specific language governing permissions and limitations
23 Installation script for Quantum's development virtualenv
31 ROOT = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
32 VENV = os.path.join(ROOT, '.venv')
33 PIP_REQUIRES = os.path.join(ROOT, 'tools', 'pip-requires')
34 OPTIONAL_REQUIRES = os.path.join(ROOT, 'tools', 'optional-requires')
35 TEST_REQUIRES = os.path.join(ROOT, 'tools', 'test-requires')
36 PY_VERSION = "python%s.%s" % (sys.version_info[0], sys.version_info[1])
38 VENV_EXISTS = bool(os.path.exists(VENV))
40 def die(message, *args):
41 print >> sys.stderr, message % args
45 def run_command(cmd, redirect_output=True, check_exit_code=True):
47 Runs a command in an out-of-process shell, returning the
48 output of that command. Working directory is ROOT.
51 stdout = subprocess.PIPE
54 proc = subprocess.Popen(cmd, cwd=ROOT, stdout=stdout)
55 output = proc.communicate()[0]
56 if check_exit_code and proc.returncode != 0:
57 raise Exception('Command "%s" failed.\n%s' % (' '.join(cmd), output))
61 HAS_EASY_INSTALL = bool(run_command(['which', 'easy_install'],
62 check_exit_code=False).strip())
63 HAS_VIRTUALENV = bool(run_command(['which', 'virtualenv'],
64 check_exit_code=False).strip())
67 def check_dependencies():
68 """Make sure virtualenv is in the path."""
70 if not HAS_VIRTUALENV:
71 raise Exception('Virtualenv not found. ' + \
72 'Try installing python-virtualenv')
76 def create_virtualenv(venv=VENV, install_pip=False):
77 """Creates the virtual environment and installs PIP only into the
80 print 'Creating venv...',
82 install = ['virtualenv', '-q', venv]
86 print 'Installing pip in virtualenv...',
88 not run_command(['tools/with_venv.sh', 'easy_install',
90 die("Failed to install pip.")
94 def install_dependencies(venv=VENV):
95 print 'Installing dependencies with pip (this can take a while)...'
96 run_command(['tools/with_venv.sh', 'pip', 'install', '-r',
97 PIP_REQUIRES], redirect_output=False)
98 run_command(['tools/with_venv.sh', 'pip', 'install', '-r',
99 OPTIONAL_REQUIRES], redirect_output=False)
100 run_command(['tools/with_venv.sh', 'pip', 'install', '-r',
101 TEST_REQUIRES], redirect_output=False)
103 # Tell the virtual env how to "import quantum"
104 pthfile = os.path.join(venv, "lib", PY_VERSION, "site-packages",
106 f = open(pthfile, 'w')
107 f.write("%s\n" % ROOT)
112 Quantum development environment setup is complete.
114 Quantum development uses virtualenv to track and manage Python dependencies
115 while in development and testing.
117 To activate the Quantum virtualenv for the extent of your current shell
120 $ source .venv/bin/activate
122 Or, if you prefer, you can run commands in the virtualenv on a case by case
125 $ tools/with_venv.sh <your command>
127 Also, make test will automatically use the virtualenv.
135 install_dependencies()
138 if __name__ == '__main__':