Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for conda, venv or pyenv-virtualenv usage in cookiecutter #207

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
"repo_name": "{{ cookiecutter.project_name.lstrip('0123456789.- ').replace(' ', '_').replace('-', '_').lower() }}",
"author_name": "Nesta",
"description": "A short description of the project.",
"openness": ["public", "private"]
}
"openness": [
"public",
"private"
],
"venv_type": [
"conda",
"venv",
"pyenv-virtualenv"
]
}
30 changes: 30 additions & 0 deletions {{ cookiecutter.repo_name }}/.cookiecutter/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,33 @@ export PROJECT_NAME={{ cookiecutter.project_name }}
export REPO_NAME={{ cookiecutter.repo_name }}
export DESCRIPTION={{ cookiecutter.description }}
export PROJECT_OPENNESS={{ cookiecutter.openness }}
export ENVIRONMENT_TYPE={{ cookiecutter.venv_type }}

{% if cookiecutter.venv_type == 'conda' %}
# Conda configuration
ENVIRONMENT_NAME=${REPO_NAME}
HarrisonWilde marked this conversation as resolved.
Show resolved Hide resolved
ENV_CREATE=conda env create -n ${ENVIRONMENT_NAME} -f environment.yaml
ENV_ACTIVATE=conda activate ${ENVIRONMENT_NAME}
ENV_DEACTIVATE=conda deactivate
ENV_REMOVE=conda env remove -n ${ENVIRONMENT_NAME}
ENV_UPDATE=conda env update -n ${ENVIRONMENT_NAME} -f environment.yaml
ENV_INSTALL_EXTRAS=conda install pip
{% elif cookiecutter.venv_type == 'venv' %}
# venv configuration
VENV_PATH=.venv
ENV_CREATE=python -m venv ${VENV_PATH}
ENV_ACTIVATE=source ${VENV_PATH}/bin/activate
ENV_DEACTIVATE=deactivate
ENV_REMOVE=rm -rf ${VENV_PATH}
ENV_UPDATE=pip install -r requirements.txt
ENV_INSTALL_EXTRAS=true
{% else %}
# pyenv-virtualenv configuration
ENVIRONMENT_NAME=${REPO_NAME}
ENV_CREATE=pyenv virtualenv ${PYTHON_VERSION} ${ENVIRONMENT_NAME}
ENV_ACTIVATE=pyenv activate ${ENVIRONMENT_NAME}
ENV_DEACTIVATE=pyenv deactivate
ENV_REMOVE=pyenv virtualenv-delete ${ENVIRONMENT_NAME}
ENV_UPDATE=pip install -r requirements.txt
ENV_INSTALL_EXTRAS=true
{% endif %}
84 changes: 48 additions & 36 deletions {{ cookiecutter.repo_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ include .cookiecutter/config

# Ensure directory to track and log setup state exists
$(shell mkdir -p .cookiecutter/state)
$(shell touch .cookiecutter/state/conda-create.log)
$(shell touch .cookiecutter/state/python-env-create.log)

.PHONY: install
## Install a project: remove existing conda env (if exists); create conda env; install local package; setup git hooks
install: conda-remove .cookiecutter/state/conda-create .cookiecutter/state/setup-git
@direnv reload # Now the conda env exists, reload to activate it

## Install the project: setup environment, install dependencies, and configure git hooks
install:
$(MAKE) python-env-remove .cookiecutter/state/python-env-create .cookiecutter/state/setup-git
HarrisonWilde marked this conversation as resolved.
Show resolved Hide resolved
@direnv reload

.PHONY: check-bucket-path
check-bucket-path:
Expand Down Expand Up @@ -50,24 +50,33 @@ docs-clean:
docs-open:
$(OPEN) docs/_build/index.html

.PHONY: conda-update
## Update the conda-environment based on changes to `environment.yaml`
conda-update:
conda env update -n ${REPO_NAME} -f environment.yaml
$(MAKE) -s pip-install
direnv reload
.PHONY: python-env-create
## Create the virtual environment
python-env-create:
@echo "Creating ${ENVIRONMENT_TYPE} environment..."
@${ENV_CREATE}

.PHONY: python-env-remove
## Remove the virtual environment
python-env-remove:
@echo "Removing ${ENVIRONMENT_TYPE} environment..."
-@${ENV_DEACTIVATE} 2>/dev/null || true
-@${ENV_REMOVE} 2>/dev/null || true
@rm -f .cookiecutter/state/python-env-create*
@direnv reload

.PHONY: python-env-update
HarrisonWilde marked this conversation as resolved.
Show resolved Hide resolved
## Update the environment based on dependency files
python-env-update:
@echo "Updating ${ENVIRONMENT_TYPE} environment..."
@${ENV_UPDATE}
$(MAKE) -s pip-install
@direnv reload

.PHONY: pip-install
## Install our package and requirements in editable mode (including development dependencies)
## Install package in editable mode with dev dependencies
pip-install:
@pip install -e ".[dev]"

.PHONY: conda-remove
## Remove the conda-environment cleanly, using -f so works even if no environment to be removed
conda-remove:
conda env remove -n ${REPO_NAME}
rm -f .cookiecutter/state/conda-create*
@direnv reload
@pip install -e ".[dev]"

.PHONY: clean
## Delete all compiled Python files
Expand All @@ -84,24 +93,27 @@ define err
(echo "$1, check [email protected] for more info" && exit 1)
endef

.cookiecutter/state/conda-create:
@echo -n "Creating environment ${REPO_NAME} and installing all dependencies"
@(conda env create -q -n ${REPO_NAME} -f environment.yaml\
&& eval "$$(conda shell.bash activate "${REPO_NAME}")"\
&& pip install -e ".[dev]")\
> [email protected] 2>&1\
|| $(call err,Python environment setup failed)
@touch $@
@echo " DONE"
.cookiecutter/state/python-env-create:
@echo -n "Creating ${ENVIRONMENT_TYPE} environment and installing dependencies"
@( \
${ENV_CREATE} && \
${ENV_ACTIVATE} && \
${ENV_INSTALL_EXTRAS} && \
pip install -e ".[dev]" \
) > [email protected] 2>&1 \
|| (echo "Environment setup failed, check [email protected] for details" && exit 1)
@touch $@
@echo " DONE"

.cookiecutter/state/setup-git:
@echo -n "Installing and configuring git pre-commit hooks"
@(eval "$$(conda shell.bash activate "${REPO_NAME}")"\
&&pre-commit install --install-hooks)\
> [email protected] 2>&1\
|| $(call err,Git pre-commit setup failed)
@touch $@
@echo " DONE"
@echo -n "Installing and configuring git pre-commit hooks"
@( \
${ENV_ACTIVATE} && \
pre-commit install --install-hooks \
) > [email protected] 2>&1 \
|| (echo "Git pre-commit setup failed, check [email protected] for details" && exit 1)
@touch $@
@echo " DONE"


#################################################################################
Expand Down
Loading