pervio a la presentacion
[vsorcdistro/.git] / ryu / tools / install_venv.py
1 #!/usr/bin/env python
2 # vim: tabstop=4 shiftwidth=4 softtabstop=4
3
4 # Copyright 2010 United States Government as represented by the
5 # Administrator of the National Aeronautics and Space Administration.
6 # All Rights Reserved.
7 #
8 # Copyright 2010 OpenStack LLC.
9 #
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
13 #
14 #         http://www.apache.org/licenses/LICENSE-2.0
15 #
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
20 #    under the License.
21
22 """
23 Installation script for Quantum's development virtualenv
24 """
25
26 import os
27 import subprocess
28 import sys
29
30
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])
37
38 VENV_EXISTS = bool(os.path.exists(VENV))
39
40 def die(message, *args):
41     print >> sys.stderr, message % args
42     sys.exit(1)
43
44
45 def run_command(cmd, redirect_output=True, check_exit_code=True):
46     """
47     Runs a command in an out-of-process shell, returning the
48     output of that command.  Working directory is ROOT.
49     """
50     if redirect_output:
51         stdout = subprocess.PIPE
52     else:
53         stdout = None
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))
58     return output
59
60
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())
65
66
67 def check_dependencies():
68     """Make sure virtualenv is in the path."""
69
70     if not HAS_VIRTUALENV:
71         raise Exception('Virtualenv not found. ' + \
72                          'Try installing python-virtualenv')
73     print 'done.'
74
75
76 def create_virtualenv(venv=VENV, install_pip=False):
77     """Creates the virtual environment and installs PIP only into the
78     virtual environment
79     """
80     print 'Creating venv...',
81
82     install = ['virtualenv', '-q', venv]
83     run_command(install)
84
85     print 'done.'
86     print 'Installing pip in virtualenv...',
87     if install_pip and \
88             not run_command(['tools/with_venv.sh', 'easy_install',
89                              'pip>1.0']):
90         die("Failed to install pip.")
91     print 'done.'
92
93
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)
102
103     # Tell the virtual env how to "import quantum"
104     pthfile = os.path.join(venv, "lib", PY_VERSION, "site-packages",
105                                  "quantum.pth")
106     f = open(pthfile, 'w')
107     f.write("%s\n" % ROOT)
108
109
110 def print_help():
111     help = """
112  Quantum development environment setup is complete.
113
114  Quantum development uses virtualenv to track and manage Python dependencies
115  while in development and testing.
116
117  To activate the Quantum virtualenv for the extent of your current shell
118  session you can run:
119
120  $ source .venv/bin/activate
121
122  Or, if you prefer, you can run commands in the virtualenv on a case by case
123  basis by running:
124
125  $ tools/with_venv.sh <your command>
126
127  Also, make test will automatically use the virtualenv.
128     """
129     print help
130
131
132 def main(argv):
133     check_dependencies()
134     create_virtualenv()
135     install_dependencies()
136     print_help()
137
138 if __name__ == '__main__':
139     main(sys.argv)