1- # Copyright 2010 United States Government as represented by the
2- # Administrator of the National Aeronautics and Space Administration.
3- # All Rights Reserved.
1+ # Copyright 2013 OpenStack, LLC.
42#
5- # Copyright 2010 OpenStack, LLC
3+ # Licensed under the Apache License, Version 2.0 (the "License"); you may
4+ # not use this file except in compliance with the License. You may obtain
5+ # a copy of the License at
66#
7- # Licensed under the Apache License, Version 2.0 (the "License"); you may
8- # not use this file except in compliance with the License. You may obtain
9- # a copy of the License at
7+ # http://www.apache.org/licenses/LICENSE-2.0
108#
11- # http://www.apache.org/licenses/LICENSE-2.0
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+ # License for the specific language governing permissions and limitations
13+ # under the License.
1214#
13- # Unless required by applicable law or agreed to in writing, software
14- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16- # License for the specific language governing permissions and limitations
17- # under the License.
1815
1916"""
2017Installation script for python-openstackclient's development virtualenv
2118"""
2219
23- import optparse
2420import os
25- import subprocess
2621import sys
27- import platform
2822
29-
30- ROOT = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
31- VENV = os .path .join (ROOT , '.venv' )
32- PIP_REQUIRES = os .path .join (ROOT , 'tools' , 'pip-requires' )
33- TEST_REQUIRES = os .path .join (ROOT , 'tools' , 'test-requires' )
34- PY_VERSION = "python%s.%s" % (sys .version_info [0 ], sys .version_info [1 ])
35-
36-
37- def die (message , * args ):
38- print >> sys .stderr , message % args
39- sys .exit (1 )
40-
41-
42- def check_python_version ():
43- if sys .version_info < (2 , 6 ):
44- die ("Need Python Version >= 2.6" )
45-
46-
47- def run_command_with_code (cmd , redirect_output = True , check_exit_code = True ):
48- """
49- Runs a command in an out-of-process shell, returning the
50- output of that command. Working directory is ROOT.
51- """
52- if redirect_output :
53- stdout = subprocess .PIPE
54- else :
55- stdout = None
56-
57- proc = subprocess .Popen (cmd , cwd = ROOT , stdout = stdout )
58- output = proc .communicate ()[0 ]
59- if check_exit_code and proc .returncode != 0 :
60- die ('Command "%s" failed.\n %s' , ' ' .join (cmd ), output )
61- return (output , proc .returncode )
62-
63-
64- def run_command (cmd , redirect_output = True , check_exit_code = True ):
65- return run_command_with_code (cmd , redirect_output , check_exit_code )[0 ]
66-
67-
68- class Distro (object ):
69-
70- def check_cmd (self , cmd ):
71- return bool (run_command (['which' , cmd ], check_exit_code = False ).strip ())
72-
73- def install_virtualenv (self ):
74- if self .check_cmd ('virtualenv' ):
75- return
76-
77- if self .check_cmd ('easy_install' ):
78- print 'Installing virtualenv via easy_install...' ,
79- if run_command (['easy_install' , 'virtualenv' ]):
80- print 'Succeeded'
81- return
82- else :
83- print 'Failed'
84-
85- die ('ERROR: virtualenv not found.\n \n Development'
86- ' requires virtualenv, please install it using your'
87- ' favorite package management tool' )
88-
89- def post_process (self ):
90- """Any distribution-specific post-processing gets done here.
91-
92- In particular, this is useful for applying patches to code inside
93- the venv."""
94- pass
95-
96-
97- class Debian (Distro ):
98- """This covers all Debian-based distributions."""
99-
100- def check_pkg (self , pkg ):
101- return run_command_with_code (['dpkg' , '-l' , pkg ],
102- check_exit_code = False )[1 ] == 0
103-
104- def apt_install (self , pkg , ** kwargs ):
105- run_command (['sudo' , 'apt-get' , 'install' , '-y' , pkg ], ** kwargs )
106-
107- def apply_patch (self , originalfile , patchfile ):
108- run_command (['patch' , originalfile , patchfile ])
109-
110- def install_virtualenv (self ):
111- if self .check_cmd ('virtualenv' ):
112- return
113-
114- if not self .check_pkg ('python-virtualenv' ):
115- self .apt_install ('python-virtualenv' , check_exit_code = False )
116-
117- super (Debian , self ).install_virtualenv ()
118-
119-
120- class Fedora (Distro ):
121- """This covers all Fedora-based distributions.
122-
123- Includes: Fedora, RHEL, CentOS, Scientific Linux"""
124-
125- def check_pkg (self , pkg ):
126- return run_command_with_code (['rpm' , '-q' , pkg ],
127- check_exit_code = False )[1 ] == 0
128-
129- def yum_install (self , pkg , ** kwargs ):
130- run_command (['sudo' , 'yum' , 'install' , '-y' , pkg ], ** kwargs )
131-
132- def apply_patch (self , originalfile , patchfile ):
133- run_command (['patch' , originalfile , patchfile ])
134-
135- def install_virtualenv (self ):
136- if self .check_cmd ('virtualenv' ):
137- return
138-
139- if not self .check_pkg ('python-virtualenv' ):
140- self .yum_install ('python-virtualenv' , check_exit_code = False )
141-
142- super (Fedora , self ).install_virtualenv ()
143-
144-
145- def get_distro ():
146- if os .path .exists ('/etc/fedora-release' ) or \
147- os .path .exists ('/etc/redhat-release' ):
148- return Fedora ()
149- elif os .path .exists ('/etc/debian_version' ):
150- return Debian ()
151- else :
152- return Distro ()
153-
154-
155- def check_dependencies ():
156- get_distro ().install_virtualenv ()
157-
158-
159- def create_virtualenv (venv = VENV , no_site_packages = True ):
160- """Creates the virtual environment and installs PIP only into the
161- virtual environment
162- """
163- print 'Creating venv...' ,
164- if no_site_packages :
165- run_command (['virtualenv' , '-q' , '--no-site-packages' , VENV ])
166- else :
167- run_command (['virtualenv' , '-q' , VENV ])
168- print 'done.'
169- print 'Installing pip in virtualenv...' ,
170- if not run_command (['tools/with_venv.sh' , 'easy_install' ,
171- 'pip>1.0' ]).strip ():
172- die ("Failed to install pip." )
173- print 'done.'
174-
175-
176- def pip_install (* args ):
177- run_command (['tools/with_venv.sh' ,
178- 'pip' , 'install' , '--upgrade' ] + list (args ),
179- redirect_output = False )
180-
181-
182- def install_dependencies (venv = VENV ):
183- print 'Installing dependencies with pip (this can take a while)...'
184-
185- # First things first, make sure our venv has the latest pip and distribute.
186- pip_install ('pip' )
187- pip_install ('distribute' )
188-
189- pip_install ('-r' , PIP_REQUIRES )
190- pip_install ('-r' , TEST_REQUIRES )
191-
192- # Tell the virtual env how to "import openstackclient"
193- pthfile = os .path .join (venv , "lib" , PY_VERSION , "site-packages" ,
194- "openstackclient.pth" )
195- f = open (pthfile , 'w' )
196- f .write ("%s\n " % ROOT )
197-
198-
199- def post_process ():
200- get_distro ().post_process ()
23+ import install_venv_common as install_venv
20124
20225
20326def print_help ():
@@ -222,23 +45,23 @@ def print_help():
22245 print help
22346
22447
225- def parse_args ():
226- """Parse command-line arguments"""
227- parser = optparse .OptionParser ()
228- parser .add_option ("-n" , "--no-site-packages" , dest = "no_site_packages" ,
229- default = False , action = "store_true" ,
230- help = "Do not inherit packages from global Python install" )
231- return parser .parse_args ()
232-
233-
23448def main (argv ):
235- (options , args ) = parse_args ()
236- check_python_version ()
237- check_dependencies ()
238- create_virtualenv (no_site_packages = options .no_site_packages )
239- install_dependencies ()
240- post_process ()
49+ root = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
50+ venv = os .path .join (root , ".venv" )
51+ pip_requires = os .path .join (root , "tools" , "pip-requires" )
52+ test_requires = os .path .join (root , "tools" , "test-requires" )
53+ py_version = "python%s.%s" % (sys .version_info [0 ], sys .version_info [1 ])
54+ project = "python-openstackclient"
55+ install = install_venv .InstallVenv (root , venv , pip_requires , test_requires ,
56+ py_version , project )
57+ options = install .parse_args (argv )
58+ install .check_python_version ()
59+ install .check_dependencies ()
60+ install .create_virtualenv (no_site_packages = options .no_site_packages )
61+ install .install_dependencies ()
62+ install .post_process ()
24163 print_help ()
24264
243- if __name__ == '__main__' :
65+
66+ if __name__ == "__main__" :
24467 main (sys .argv )
0 commit comments