Skip to content

Commit

Permalink
Generate requirements without pipenv in your project's virtualenv (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
charlieparkes authored Jul 27, 2020
1 parent 737fc85 commit 8730ede
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/python/Makefile.env
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 8730ede

Please sign in to comment.