diff --git a/fabfile.py b/fabfile.py index 2894fe477..c8ca9dc64 100644 --- a/fabfile.py +++ b/fabfile.py @@ -1,14 +1,36 @@ # coding: utf-8 from __future__ import unicode_literals, print_function +import contextlib +from pathlib import Path from fabric.api import local, lcd, env, settings, prefix -from fabtools.python import virtualenv from os import path, environ PWD = path.dirname(__file__) ENV = environ['VENV_DIR'] if 'VENV_DIR' in environ else '.env' -VENV_DIR = path.join(PWD, ENV) +VENV_DIR = Path(PWD) / ENV + + +@contextlib.contextmanager +def virtualenv(name, create=False, python='/usr/bin/python3.6'): + python = Path(python).resolve() + env_path = VENV_DIR + if create: + if env_path.exists(): + shutil.rmtree(str(env_path)) + local('{python} -m venv {env_path}'.format(python=python, + env_path=VENV_DIR)) + def wrapped_local(cmd, env_vars=[]): + env_py = env_path / 'bin' / 'python' + env_vars = ' '.join(env_vars) + if cmd.split()[0] == 'python': + cmd = cmd.replace('python', str(env_py)) + return local(env_vars + ' ' + cmd) + else: + return local('{env_vars} {env_py} -m {cmd}'.format( + env_py=env_py, cmd=cmd, env_vars=env_vars)) + yield wrapped_local def env(lang='python2.7'): @@ -19,31 +41,39 @@ def env(lang='python2.7'): def install(): - with virtualenv(VENV_DIR): - local('pip install --upgrade setuptools') - local('pip install dist/*.tar.gz') - local('pip install pytest') + with virtualenv(VENV_DIR) as venv_local: + venv_local('pip install --upgrade setuptools') + venv_local('pip install dist/*.tar.gz') + venv_local('pip install pytest') def make(): - with virtualenv(VENV_DIR): - with lcd(path.dirname(__file__)): - local('pip install cython') - local('pip install murmurhash') - local('pip install -r requirements.txt') - local('python setup.py build_ext --inplace') + with lcd(path.dirname(__file__)): + with virtualenv(VENV_DIR) as venv_local: + venv_local('pip install cython') + venv_local('pip install murmurhash') + venv_local('pip install -r requirements.txt') + venv_local('python setup.py build_ext --inplace') def sdist(): - with virtualenv(VENV_DIR): - with lcd(path.dirname(__file__)): + with lcd(path.dirname(__file__)): + with virtualenv(VENV_DIR) as venv_local: local('python setup.py sdist') -def clean(): +def wheel(): with lcd(path.dirname(__file__)): - local('python setup.py clean --all') + with virtualenv(VENV_DIR) as venv_local: + venv_local('pip install wheel') + venv_local('python setup.py bdist_wheel') + + +def clean(): + with virtualenv(VENV_DIR) as venv_local: + with lcd(path.dirname(__file__)): + local('python setup.py clean --all') def test(): - with virtualenv(VENV_DIR): - with lcd(path.dirname(__file__)): + with lcd(path.dirname(__file__)): + with virtualenv(VENV_DIR) as venv_local: local('py.test -x spacy/tests')