Skip to content

Instantly share code, notes, and snippets.

@kelleyk
Last active August 29, 2015 14:21
Show Gist options
  • Save kelleyk/08323ee12d74fbac4415 to your computer and use it in GitHub Desktop.
Save kelleyk/08323ee12d74fbac4415 to your computer and use it in GitHub Desktop.
Update the wheels that are used to install pip and setuptools in each newly-created virtualenv
#!/usr/bin/env python3.4
import re
import sys
import os.path
import logging
import subprocess
from pathlib import Path
from six import text_type
log = logging.getLogger('venv_support_update')
# Will be passed to each invocation of 'pip'. (If you need e.g. '--trusted-host ...', this is the place for it.)
PIP_ARGS = []
def main():
# For pyenv/pyenv-virtualenvwrapper installations.
for path in Path(os.path.expanduser('~/.pyenv/versions')).iterdir():
if re.match(r'^\d+(\.\d+)+$', path.name):
log.debug('{}...'.format(path.name))
version_info = tuple(int(x) for x in path.name.split('.'))
pip_path = path / 'bin/pip'
venv_support_path = path / 'lib/python{}.{}/site-packages/virtualenv_support'.format(*version_info[:2])
update_virtualenv_support(pip_path, venv_support_path)
# For user-level installations of virtualenv without pyenv.
for path in Path(os.path.expanduser('~/.local/lib')).iterdir():
m = re.match(r'^python(\d+)\.(\d+)$', path.name)
if m:
pip_path = '/usr/local/bin/pip{}.{}'.format(m.group(1), m.group(2)) # XXX: This is probably not true in every environment.
update_virtualenv_support(pip_path, path / 'site-packages/virtualenv_support')
def update_virtualenv_support(pip_path, venv_support_path):
if not venv_support_path.exists():
print('WARNING: No virtualenv_support directory: {}'.format(venv_support_path), file=sys.stderr)
return
cmd = [text_type(pip_path)] + list(PIP_ARGS) + ['install', '-d', '.']
log.debug(' - Checking setuptools...')
subprocess.check_call(cmd + ['setuptools'], cwd=text_type(venv_support_path))
log.debug(' - Checking pip...')
subprocess.check_call(cmd + ['pip'], cwd=text_type(venv_support_path))
# TODO: Check if we upgraded anything. (You'll see a "file already downloaded" message if not.)
# TODO: Clean up old versions (though it seems that the most-recent one is automatically used;
# small miracles, right?).
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment