Skip to content

Instantly share code, notes, and snippets.

@rsyring
Created March 22, 2018 18:29
Show Gist options
  • Save rsyring/5d9c89b88e378447334eb11e0fc16eab to your computer and use it in GitHub Desktop.
Save rsyring/5d9c89b88e378447334eb11e0fc16eab to your computer and use it in GitHub Desktop.
Script for initializing virtualenvs from app's that have wheelhouses
#!/usr/bin/env python3
#PROJ_PATH=$(git rev-parse --show-toplevel)
#PROJ=${PROJ_PATH##*/}
#rmvirtualenv $PROJ
#vex -m $PROJ pip install "git+https://github.com/level12/wheelhouse#egg=Wheelhouse"
#vex $PROJ wheelhouse install -- -r requirements/dev-env.txt
#vex $PROJ pip install -e .
#vex -r $PROJ
import argparse
from os import environ
import pathlib
import warnings
import sh
warnings.filterwarnings('ignore', r"unclosed file", category=ResourceWarning)
def is_wheelhouse_empty(project_dpath):
wheels = list(project_dpath.joinpath('requirements/wheelhouse').glob('*.whl'))
return not bool(len(wheels))
def is_virtualenv_active():
return 'VIRTUAL_ENV' in environ
def git_hooks(project_dpath):
# link any git hooks
for scripts_hook in project_dpath.joinpath('scripts').glob('git-hook-*'):
hook_name = scripts_hook.name.replace('git-hook-', '', 1)
git_hook_fpath = project_dpath.joinpath('.git', 'hooks', hook_name)
if not git_hook_fpath.exists():
git_hook_fpath.symlink_to(scripts_hook)
print('Linked {} git hook'.format(hook_name))
def loadit(args):
try:
grp_result = sh.git('rev-parse', '--show-toplevel')
except sh.ErrorReturnCode_2 as exc:
if 'Not a git repository' not in str(exc):
raise
sh.git('init', _fg=True)
grp_result = sh.git('rev-parse', '--show-toplevel')
# determine the project name
project_dpath = pathlib.Path(grp_result.stdout.strip().decode())
project_name = project_dpath.name
if project_name.endswith('-src'):
project_name = project_name[0:-4]
print('Project: {}'.format(project_name))
if not is_virtualenv_active():
# remove the virtualenv if it exists
bash_cmd = '. /usr/local/bin/virtualenvwrapper.sh; rmvirtualenv {}'.format(project_name)
sh.bash('-c', bash_cmd, _fg=True)
# bootstrap the virtualenv and install wheelhouse
wh_url = "git+https://github.com/level12/wheelhouse#egg=Wheelhouse"
sh.vex('--python', args.pybin, '-m', project_name, 'pip', 'install', wh_url, _fg=True)
# If the wheelhouse is not empty, go ahead and install the requirements and app. If it is
# empty, assume this is the first pass, and the requirements aren't there yet. The dev will
# have to build the wheelhouse to get things going.
if is_wheelhouse_empty(project_dpath):
sh.vex(project_name, 'wheelhouse', 'build', _fg=True)
sh.vex(project_name, 'wheelhouse', 'install', '--', '-r', 'requirements/dev-env.txt',
_fg=True)
sh.vex(project_name, 'pip', 'install', '-e', '.', _fg=True)
git_hooks(project_dpath)
if not is_virtualenv_active():
sh.vex(project_name, _fg=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("pybin", nargs='?', default='python3')
args = parser.parse_args()
loadit(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment