From 8730ede63e3a066a35b933944c4fd41cf1c9c5e0 Mon Sep 17 00:00:00 2001 From: Charlie Date: Mon, 27 Jul 2020 16:39:21 -0500 Subject: [PATCH] Generate requirements without pipenv in your project's virtualenv (#56) * In requirements.txt targets, use bh venv so that we can use automatically installed pipenv * Use docker kennethreitz/pipenv to build requirements files instead of installing it * Remove BH_PIPENV --- modules/python/Makefile.env | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/python/Makefile.env b/modules/python/Makefile.env index f1309bbc..964dd74c 100644 --- a/modules/python/Makefile.env +++ b/modules/python/Makefile.env @@ -1,4 +1,5 @@ -PY_PIPENV := $(shell which pipenv 2>/dev/null) +PY_PIPENV:=$(shell which pipenv 2>/dev/null) + ifneq ($(PY_PIPENV),) ifneq ($(WORKON_HOME),) # If WORKON_HOME is set, assume we're going to create ".venv" next to the Pipfile. @@ -86,15 +87,19 @@ pipenv/clean: ## Generate requirements files based on a Pipfile pipenv/generate_requirements: $(PIPENV_PIPFILE_DIR)/requirements.txt $(PIPENV_PIPFILE_DIR)/requirements-dev.txt +define py-generate-requirements +from pipenv.project import Project; section = "$(1)"; reqs = {package: (ver["version"] if isinstance(ver, dict) else ver) for package, ver in Project().parsed_pipfile[section].items()}; fd = open("$(2).txt", mode="w"); fd.writelines((r if reqs[r] == "*" else "{} {}".format(r, reqs[r])) + "\n" for r in reqs.keys()); fd.close() +endef + $(PIPENV_PIPFILE_DIR)/requirements.txt: $(PIPENV_PIPFILE_DIR)/Pipfile $(call assert-set,PIPENV_PIPFILE_DIR) @echo "Writing requirements from Pipfile to requirements.txt" - @$(PIPENV_RUN) python -c 'from pipenv.project import Project; section = "packages"; reqs = {package: (ver["version"] if isinstance(ver, dict) else ver) for package, ver in Project().parsed_pipfile[section].items()}; fd = open("requirements.txt", mode="w"); fd.writelines((r if reqs[r] == "*" else "{} {}".format(r, reqs[r])) + "\n" for r in reqs.keys()); fd.close()' + @$(DOCKER) run --rm --mount type=bind,src=$$(pwd),dst=/app kennethreitz/pipenv python3 -c '$(call py-generate-requirements,packages,requirements)' $(PIPENV_PIPFILE_DIR)/requirements-dev.txt: $(PIPENV_PIPFILE_DIR)/Pipfile $(call assert-set,PIPENV_PIPFILE_DIR) @echo "Writing dev requirements from Pipfile to requirements-dev.txt" - @$(PIPENV_RUN) python -c 'from pipenv.project import Project; section = "dev-packages"; reqs = {package: (ver["version"] if isinstance(ver, dict) else ver) for package, ver in Project().parsed_pipfile[section].items()}; fd = open("requirements-dev.txt", mode="w"); fd.writelines((r if reqs[r] == "*" else "{} {}".format(r, reqs[r])) + "\n" for r in reqs.keys()); fd.close()' + @$(DOCKER) run --rm --mount type=bind,src=$$(pwd),dst=/app kennethreitz/pipenv python3 -c '$(call py-generate-requirements,dev-packages,requirements-dev)' # The requirements*.txt files will be next to the Pipfile. # If setup.py and Pipfile are in separate directories, create symlinks.