Skip to content

Commit 4022e41

Browse files
committed
Sync install_venv_common from oslo
Change-Id: I0a57c658e0f89d13963862013793e12ae208c05b
1 parent d7501c3 commit 4022e41

1 file changed

Lines changed: 43 additions & 63 deletions

File tree

tools/install_venv_common.py

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vim: tabstop=4 shiftwidth=4 softtabstop=4
22

3-
# Copyright 2013 OpenStack, LLC
3+
# Copyright 2013 OpenStack Foundation
44
# Copyright 2013 IBM Corp.
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -18,36 +18,34 @@
1818
"""Provides methods needed by installation script for OpenStack development
1919
virtual environments.
2020
21+
Since this script is used to bootstrap a virtualenv from the system's Python
22+
environment, it should be kept strictly compatible with Python 2.6.
23+
2124
Synced in from openstack-common
2225
"""
2326

27+
from __future__ import print_function
28+
29+
import optparse
2430
import os
2531
import subprocess
2632
import sys
2733

2834

29-
possible_topdir = os.getcwd()
30-
if os.path.exists(os.path.join(possible_topdir, "openstackclient",
31-
"__init__.py")):
32-
sys.path.insert(0, possible_topdir)
33-
34-
35-
from openstackclient.openstack.common import cfg
36-
37-
3835
class InstallVenv(object):
3936

40-
def __init__(self, root, venv, pip_requires, test_requires, py_version,
37+
def __init__(self, root, venv, requirements,
38+
test_requirements, py_version,
4139
project):
4240
self.root = root
4341
self.venv = venv
44-
self.pip_requires = pip_requires
45-
self.test_requires = test_requires
42+
self.requirements = requirements
43+
self.test_requirements = test_requirements
4644
self.py_version = py_version
4745
self.project = project
4846

4947
def die(self, message, *args):
50-
print >> sys.stderr, message % args
48+
print(message % args, file=sys.stderr)
5149
sys.exit(1)
5250

5351
def check_python_version(self):
@@ -58,7 +56,7 @@ def run_command_with_code(self, cmd, redirect_output=True,
5856
check_exit_code=True):
5957
"""Runs a command in an out-of-process shell.
6058
61-
Returns the output of that command. Working directory is ROOT.
59+
Returns the output of that command. Working directory is self.root.
6260
"""
6361
if redirect_output:
6462
stdout = subprocess.PIPE
@@ -78,11 +76,13 @@ def run_command(self, cmd, redirect_output=True, check_exit_code=True):
7876
def get_distro(self):
7977
if (os.path.exists('/etc/fedora-release') or
8078
os.path.exists('/etc/redhat-release')):
81-
return Fedora(self.root, self.venv, self.pip_requires,
82-
self.test_requires, self.py_version, self.project)
79+
return Fedora(
80+
self.root, self.venv, self.requirements,
81+
self.test_requirements, self.py_version, self.project)
8382
else:
84-
return Distro(self.root, self.venv, self.pip_requires,
85-
self.test_requires, self.py_version, self.project)
83+
return Distro(
84+
self.root, self.venv, self.requirements,
85+
self.test_requirements, self.py_version, self.project)
8686

8787
def check_dependencies(self):
8888
self.get_distro().install_virtualenv()
@@ -94,20 +94,15 @@ def create_virtualenv(self, no_site_packages=True):
9494
virtual environment.
9595
"""
9696
if not os.path.isdir(self.venv):
97-
print 'Creating venv...',
97+
print('Creating venv...', end=' ')
9898
if no_site_packages:
9999
self.run_command(['virtualenv', '-q', '--no-site-packages',
100100
self.venv])
101101
else:
102102
self.run_command(['virtualenv', '-q', self.venv])
103-
print 'done.'
104-
print 'Installing pip in virtualenv...',
105-
if not self.run_command(['tools/with_venv.sh', 'easy_install',
106-
'pip>1.0']).strip():
107-
self.die("Failed to install pip.")
108-
print 'done.'
103+
print('done.')
109104
else:
110-
print "venv already exists..."
105+
print("venv already exists...")
111106
pass
112107

113108
def pip_install(self, *args):
@@ -116,40 +111,27 @@ def pip_install(self, *args):
116111
redirect_output=False)
117112

118113
def install_dependencies(self):
119-
print 'Installing dependencies with pip (this can take a while)...'
114+
print('Installing dependencies with pip (this can take a while)...')
120115

121116
# First things first, make sure our venv has the latest pip and
122-
# distribute.
123-
# NOTE: we keep pip at version 1.1 since the most recent version causes
124-
# the .venv creation to fail. See:
125-
# https://bugs.launchpad.net/nova/+bug/1047120
126-
self.pip_install('pip==1.1')
127-
self.pip_install('distribute')
117+
# setuptools.
118+
self.pip_install('pip>=1.3')
119+
self.pip_install('setuptools')
128120

129-
# Install greenlet by hand - just listing it in the requires file does
130-
# not
131-
# get it installed in the right order
132-
self.pip_install('greenlet')
133-
134-
self.pip_install('-r', self.pip_requires)
135-
self.pip_install('-r', self.test_requires)
121+
self.pip_install('-r', self.requirements)
122+
self.pip_install('-r', self.test_requirements)
136123

137124
def post_process(self):
138125
self.get_distro().post_process()
139126

140127
def parse_args(self, argv):
141128
"""Parses command-line arguments."""
142-
cli_opts = [
143-
cfg.BoolOpt('no-site-packages',
144-
default=False,
145-
short='n',
146-
help="Do not inherit packages from global Python"
147-
"install"),
148-
]
149-
CLI = cfg.ConfigOpts()
150-
CLI.register_cli_opts(cli_opts)
151-
CLI(argv[1:])
152-
return CLI
129+
parser = optparse.OptionParser()
130+
parser.add_option('-n', '--no-site-packages',
131+
action='store_true',
132+
help="Do not inherit packages from global Python "
133+
"install")
134+
return parser.parse_args(argv[1:])[0]
153135

154136

155137
class Distro(InstallVenv):
@@ -163,12 +145,12 @@ def install_virtualenv(self):
163145
return
164146

165147
if self.check_cmd('easy_install'):
166-
print 'Installing virtualenv via easy_install...',
148+
print('Installing virtualenv via easy_install...', end=' ')
167149
if self.run_command(['easy_install', 'virtualenv']):
168-
print 'Succeeded'
150+
print('Succeeded')
169151
return
170152
else:
171-
print 'Failed'
153+
print('Failed')
172154

173155
self.die('ERROR: virtualenv not found.\n\n%s development'
174156
' requires virtualenv, please install it using your'
@@ -193,19 +175,16 @@ def check_pkg(self, pkg):
193175
return self.run_command_with_code(['rpm', '-q', pkg],
194176
check_exit_code=False)[1] == 0
195177

196-
def yum_install(self, pkg, **kwargs):
197-
print "Attempting to install '%s' via yum" % pkg
198-
self.run_command(['sudo', 'yum', 'install', '-y', pkg], **kwargs)
199-
200178
def apply_patch(self, originalfile, patchfile):
201-
self.run_command(['patch', originalfile, patchfile])
179+
self.run_command(['patch', '-N', originalfile, patchfile],
180+
check_exit_code=False)
202181

203182
def install_virtualenv(self):
204183
if self.check_cmd('virtualenv'):
205184
return
206185

207186
if not self.check_pkg('python-virtualenv'):
208-
self.yum_install('python-virtualenv', check_exit_code=False)
187+
self.die("Please install 'python-virtualenv'.")
209188

210189
super(Fedora, self).install_virtualenv()
211190

@@ -218,12 +197,13 @@ def post_process(self):
218197
This can be removed when the fix is applied upstream.
219198
220199
Nova: https://bugs.launchpad.net/nova/+bug/884915
221-
Upstream: https://bitbucket.org/which_linden/eventlet/issue/89
200+
Upstream: https://bitbucket.org/eventlet/eventlet/issue/89
201+
RHEL: https://bugzilla.redhat.com/958868
222202
"""
223203

224204
# Install "patch" program if it's not there
225205
if not self.check_pkg('patch'):
226-
self.yum_install('patch')
206+
self.die("Please install 'patch'.")
227207

228208
# Apply the eventlet patch
229209
self.apply_patch(os.path.join(self.venv, 'lib', self.py_version,

0 commit comments

Comments
 (0)