install_venv: add support for distro specific code

... and use it to install virtualenv with yum if it's not available.

Change-Id: Ibfeeb9c23324724bc26895504e6229076d793c6d
This commit is contained in:
Mark McLoughlin
2011-09-29 15:06:54 +01:00
parent a5fc312810
commit 9f529afd47

View File

@@ -44,7 +44,7 @@ def check_python_version():
die("Need Python Version >= 2.6")
def run_command(cmd, redirect_output=True, check_exit_code=True):
def run_command_with_code(cmd, redirect_output=True, check_exit_code=True):
"""
Runs a command in an out-of-process shell, returning the
output of that command. Working directory is ROOT.
@@ -58,30 +58,63 @@ def run_command(cmd, redirect_output=True, check_exit_code=True):
output = proc.communicate()[0]
if check_exit_code and proc.returncode != 0:
die('Command "%s" failed.\n%s', ' '.join(cmd), output)
return output
return (output, proc.returncode)
HAS_EASY_INSTALL = bool(run_command(['which', 'easy_install'],
check_exit_code=False).strip())
HAS_VIRTUALENV = bool(run_command(['which', 'virtualenv'],
check_exit_code=False).strip())
def run_command(cmd, redirect_output=True, check_exit_code=True):
return run_command_with_code(cmd, redirect_output, check_exit_code)[0]
class Distro(object):
def check_cmd(self, cmd):
return bool(run_command(['which', cmd], check_exit_code=False).strip())
def install_virtualenv(self):
if self.check_cmd('virtualenv'):
return
if self.check_cmd('easy_install'):
print 'Installing virtualenv via easy_install...',
if run_command(['easy_install', 'virtualenv']):
print 'Succeeded'
return
else:
print 'Failed'
die('ERROR: virtualenv not found.\n\nNova development'
' requires virtualenv, please install it using your'
' favorite package management tool')
class Fedora(Distro):
def check_pkg(self, pkg):
return run_command_with_code(['rpm', '-q', pkg],
check_exit_code=False)[1] == 0
def yum_install(self, pkg, **kwargs):
run_command(['sudo', 'yum', 'install', '-y', pkg], **kwargs)
def install_virtualenv(self):
if self.check_cmd('virtualenv'):
return
if not self.check_pkg('python-virtualenv'):
self.yum_install('python-virtualenv', check_exit_code=False)
super(Fedora, self).install_virtualenv()
def get_distro():
if os.path.exists('/etc/fedora-release'):
return Fedora()
else:
return Distro()
def check_dependencies():
"""Make sure virtualenv is in the path."""
if not HAS_VIRTUALENV:
print 'not found.'
# Try installing it via easy_install...
if HAS_EASY_INSTALL:
print 'Installing virtualenv via easy_install...',
if not (run_command(['which', 'easy_install']) and
run_command(['easy_install', 'virtualenv'])):
die('ERROR: virtualenv not found.\n\nNova development'
' requires virtualenv, please install it using your'
' favorite package management tool')
print 'done.'
print 'done.'
get_distro().install_virtualenv()
def create_virtualenv(venv=VENV):