Allow use of pip and mkvirtualenv for non-root users

i.e. don't always use sudo, per caller preference
This commit is contained in:
Lance Edgar 2018-09-06 11:41:35 -05:00
parent a920eebc4c
commit a5b364a239

View file

@ -66,10 +66,12 @@ def pip(*packages, **kwargs):
upgrade_strategy = '--upgrade-strategy {}'.format(upgrade_strategy) upgrade_strategy = '--upgrade-strategy {}'.format(upgrade_strategy)
else: else:
upgrade_strategy = '' upgrade_strategy = ''
use_sudo = kwargs.pop('use_sudo', True)
if kwargs: if kwargs:
abort("Unknown kwargs for pip(): {}".format(kwargs)) abort("Unknown kwargs for pip(): {}".format(kwargs))
packages = ["'{}'".format(p) for p in packages] packages = ["'{}'".format(p) for p in packages]
sudo('pip install {} {} {}'.format(upgrade, upgrade_strategy, ' '.join(packages))) run_ = sudo if use_sudo else run
run_('pip install {} {} {}'.format(upgrade, upgrade_strategy, ' '.join(packages)))
def install_virtualenvwrapper(workon_home=None, user='root', use_apt=False): def install_virtualenvwrapper(workon_home=None, user='root', use_apt=False):
@ -109,13 +111,14 @@ def configure_virtualenvwrapper(user, workon_home=None, wrapper='/usr/local/bin/
update('.bashrc') update('.bashrc')
def mkvirtualenv(name, python=None, user=None, workon_home=None, def mkvirtualenv(name, python=None, use_sudo=True, user=None, workon_home=None,
upgrade_pip=True, upgrade_six=True, upgrade_setuptools=True, upgrade_strategy=None): upgrade_pip=True, upgrade_six=True, upgrade_setuptools=True, upgrade_strategy=None):
""" """
Make a new Python virtual environment. Make a new Python virtual environment.
""" """
workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs') workon_home = workon_home or getattr(env, 'python_workon_home', '/srv/envs')
sudo('mkvirtualenv {} {}'.format('--python={}'.format(python) if python else '', name)) run_ = sudo if use_sudo else run
run_('mkvirtualenv {} {}'.format('--python={}'.format(python) if python else '', name))
if upgrade_pip: if upgrade_pip:
if isinstance(upgrade_pip, six.string_types): if isinstance(upgrade_pip, six.string_types):
pip_req = upgrade_pip pip_req = upgrade_pip
@ -123,10 +126,12 @@ def mkvirtualenv(name, python=None, user=None, workon_home=None,
pip_req = 'pip' pip_req = 'pip'
with workon(name): with workon(name):
if upgrade_six: if upgrade_six:
pip('six', upgrade=True) pip('six', upgrade=True, use_sudo=use_sudo)
pip(pip_req, upgrade=True) pip(pip_req, upgrade=True, use_sudo=use_sudo)
if upgrade_setuptools: if upgrade_setuptools:
pip('setuptools', 'wheel', 'ndg-httpsclient', upgrade=True, upgrade_strategy=upgrade_strategy) pip('setuptools', 'wheel', 'ndg-httpsclient',
upgrade=True, upgrade_strategy=upgrade_strategy,
use_sudo=use_sudo)
if user: if user:
with cdvirtualenv(name, workon_home=workon_home): with cdvirtualenv(name, workon_home=workon_home):
mkdir('app/log', owner='{0}:{0}'.format(user)) mkdir('app/log', owner='{0}:{0}'.format(user))