Sync install_venv_common from oslo

Change-Id: I0a57c658e0f89d13963862013793e12ae208c05b
This commit is contained in:
Monty Taylor 2013-07-05 22:31:15 -04:00
parent d7501c352d
commit 4022e41c64

View File

@ -1,6 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 OpenStack, LLC # Copyright 2013 OpenStack Foundation
# Copyright 2013 IBM Corp. # Copyright 2013 IBM Corp.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -18,36 +18,34 @@
"""Provides methods needed by installation script for OpenStack development """Provides methods needed by installation script for OpenStack development
virtual environments. virtual environments.
Since this script is used to bootstrap a virtualenv from the system's Python
environment, it should be kept strictly compatible with Python 2.6.
Synced in from openstack-common Synced in from openstack-common
""" """
from __future__ import print_function
import optparse
import os import os
import subprocess import subprocess
import sys import sys
possible_topdir = os.getcwd()
if os.path.exists(os.path.join(possible_topdir, "openstackclient",
"__init__.py")):
sys.path.insert(0, possible_topdir)
from openstackclient.openstack.common import cfg
class InstallVenv(object): class InstallVenv(object):
def __init__(self, root, venv, pip_requires, test_requires, py_version, def __init__(self, root, venv, requirements,
test_requirements, py_version,
project): project):
self.root = root self.root = root
self.venv = venv self.venv = venv
self.pip_requires = pip_requires self.requirements = requirements
self.test_requires = test_requires self.test_requirements = test_requirements
self.py_version = py_version self.py_version = py_version
self.project = project self.project = project
def die(self, message, *args): def die(self, message, *args):
print >> sys.stderr, message % args print(message % args, file=sys.stderr)
sys.exit(1) sys.exit(1)
def check_python_version(self): def check_python_version(self):
@ -58,7 +56,7 @@ class InstallVenv(object):
check_exit_code=True): check_exit_code=True):
"""Runs a command in an out-of-process shell. """Runs a command in an out-of-process shell.
Returns the output of that command. Working directory is ROOT. Returns the output of that command. Working directory is self.root.
""" """
if redirect_output: if redirect_output:
stdout = subprocess.PIPE stdout = subprocess.PIPE
@ -78,11 +76,13 @@ class InstallVenv(object):
def get_distro(self): def get_distro(self):
if (os.path.exists('/etc/fedora-release') or if (os.path.exists('/etc/fedora-release') or
os.path.exists('/etc/redhat-release')): os.path.exists('/etc/redhat-release')):
return Fedora(self.root, self.venv, self.pip_requires, return Fedora(
self.test_requires, self.py_version, self.project) self.root, self.venv, self.requirements,
self.test_requirements, self.py_version, self.project)
else: else:
return Distro(self.root, self.venv, self.pip_requires, return Distro(
self.test_requires, self.py_version, self.project) self.root, self.venv, self.requirements,
self.test_requirements, self.py_version, self.project)
def check_dependencies(self): def check_dependencies(self):
self.get_distro().install_virtualenv() self.get_distro().install_virtualenv()
@ -94,20 +94,15 @@ class InstallVenv(object):
virtual environment. virtual environment.
""" """
if not os.path.isdir(self.venv): if not os.path.isdir(self.venv):
print 'Creating venv...', print('Creating venv...', end=' ')
if no_site_packages: if no_site_packages:
self.run_command(['virtualenv', '-q', '--no-site-packages', self.run_command(['virtualenv', '-q', '--no-site-packages',
self.venv]) self.venv])
else: else:
self.run_command(['virtualenv', '-q', self.venv]) self.run_command(['virtualenv', '-q', self.venv])
print 'done.' print('done.')
print 'Installing pip in virtualenv...',
if not self.run_command(['tools/with_venv.sh', 'easy_install',
'pip>1.0']).strip():
self.die("Failed to install pip.")
print 'done.'
else: else:
print "venv already exists..." print("venv already exists...")
pass pass
def pip_install(self, *args): def pip_install(self, *args):
@ -116,40 +111,27 @@ class InstallVenv(object):
redirect_output=False) redirect_output=False)
def install_dependencies(self): def install_dependencies(self):
print 'Installing dependencies with pip (this can take a while)...' print('Installing dependencies with pip (this can take a while)...')
# First things first, make sure our venv has the latest pip and # First things first, make sure our venv has the latest pip and
# distribute. # setuptools.
# NOTE: we keep pip at version 1.1 since the most recent version causes self.pip_install('pip>=1.3')
# the .venv creation to fail. See: self.pip_install('setuptools')
# https://bugs.launchpad.net/nova/+bug/1047120
self.pip_install('pip==1.1')
self.pip_install('distribute')
# Install greenlet by hand - just listing it in the requires file does self.pip_install('-r', self.requirements)
# not self.pip_install('-r', self.test_requirements)
# get it installed in the right order
self.pip_install('greenlet')
self.pip_install('-r', self.pip_requires)
self.pip_install('-r', self.test_requires)
def post_process(self): def post_process(self):
self.get_distro().post_process() self.get_distro().post_process()
def parse_args(self, argv): def parse_args(self, argv):
"""Parses command-line arguments.""" """Parses command-line arguments."""
cli_opts = [ parser = optparse.OptionParser()
cfg.BoolOpt('no-site-packages', parser.add_option('-n', '--no-site-packages',
default=False, action='store_true',
short='n', help="Do not inherit packages from global Python "
help="Do not inherit packages from global Python" "install")
"install"), return parser.parse_args(argv[1:])[0]
]
CLI = cfg.ConfigOpts()
CLI.register_cli_opts(cli_opts)
CLI(argv[1:])
return CLI
class Distro(InstallVenv): class Distro(InstallVenv):
@ -163,12 +145,12 @@ class Distro(InstallVenv):
return return
if self.check_cmd('easy_install'): if self.check_cmd('easy_install'):
print 'Installing virtualenv via easy_install...', print('Installing virtualenv via easy_install...', end=' ')
if self.run_command(['easy_install', 'virtualenv']): if self.run_command(['easy_install', 'virtualenv']):
print 'Succeeded' print('Succeeded')
return return
else: else:
print 'Failed' print('Failed')
self.die('ERROR: virtualenv not found.\n\n%s development' self.die('ERROR: virtualenv not found.\n\n%s development'
' requires virtualenv, please install it using your' ' requires virtualenv, please install it using your'
@ -193,19 +175,16 @@ class Fedora(Distro):
return self.run_command_with_code(['rpm', '-q', pkg], return self.run_command_with_code(['rpm', '-q', pkg],
check_exit_code=False)[1] == 0 check_exit_code=False)[1] == 0
def yum_install(self, pkg, **kwargs):
print "Attempting to install '%s' via yum" % pkg
self.run_command(['sudo', 'yum', 'install', '-y', pkg], **kwargs)
def apply_patch(self, originalfile, patchfile): def apply_patch(self, originalfile, patchfile):
self.run_command(['patch', originalfile, patchfile]) self.run_command(['patch', '-N', originalfile, patchfile],
check_exit_code=False)
def install_virtualenv(self): def install_virtualenv(self):
if self.check_cmd('virtualenv'): if self.check_cmd('virtualenv'):
return return
if not self.check_pkg('python-virtualenv'): if not self.check_pkg('python-virtualenv'):
self.yum_install('python-virtualenv', check_exit_code=False) self.die("Please install 'python-virtualenv'.")
super(Fedora, self).install_virtualenv() super(Fedora, self).install_virtualenv()
@ -218,12 +197,13 @@ class Fedora(Distro):
This can be removed when the fix is applied upstream. This can be removed when the fix is applied upstream.
Nova: https://bugs.launchpad.net/nova/+bug/884915 Nova: https://bugs.launchpad.net/nova/+bug/884915
Upstream: https://bitbucket.org/which_linden/eventlet/issue/89 Upstream: https://bitbucket.org/eventlet/eventlet/issue/89
RHEL: https://bugzilla.redhat.com/958868
""" """
# Install "patch" program if it's not there # Install "patch" program if it's not there
if not self.check_pkg('patch'): if not self.check_pkg('patch'):
self.yum_install('patch') self.die("Please install 'patch'.")
# Apply the eventlet patch # Apply the eventlet patch
self.apply_patch(os.path.join(self.venv, 'lib', self.py_version, self.apply_patch(os.path.join(self.venv, 'lib', self.py_version,