forked from charlieparkes/build-harness
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile.venv
84 lines (74 loc) · 4 KB
/
Makefile.venv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
BUILD_HARNESS_REQ_TEMPLATE:=$(BUILD_HARNESS_PATH)/requirements.template
BUILD_HARNESS_REQ_NAME:=.build-harness.requirements.txt
BUILD_HARNESS_REQ:=$(CURDIR)/$(BUILD_HARNESS_REQ_NAME)
BUILD_HARNESS_PIPFILE_NAME:=.build-harness.Pipfile
BUILD_HARNESS_PIPFILE:=$(CURDIR)/$(BUILD_HARNESS_PIPFILE_NAME)
BUILD_HARNESS_VENV_NAME:=.build-harness.venv
BUILD_HARNESS_VENV:=$(CURDIR)/$(BUILD_HARNESS_VENV_NAME)
WITH_BH_VENV=$(shell test -f $(BUILD_HARNESS_REQ) && echo "source $(BUILD_HARNESS_VENV)/bin/activate 2>/dev/null &&")
LOCAL_PYTHON_VERSION=$(shell python -c 'import platform; ver=int(platform.python_version()[0]); print(ver)' 2>/dev/null)
BUILD_HARNESS_VENV_SKIP_EXISTING=false
# This doesn't conform to extreme edge-use-cases of modules/python/Makefile.env, but should cover >99% of use.
LOCAL_PROJECT_PYTHON_VERSION=$(shell if [ -e $(CURDIR)/Pipfile ]; then awk '/python_version*([0-9\.]*)/' Pipfile | awk 'match($$0, /[0-9\.]+/) {print substr($$0, RSTART, RLENGTH)}'; else echo ""; fi)
LOCAL_PROJECT_PYTHON_VERSION_MAJOR=$(shell echo "$(LOCAL_PROJECT_PYTHON_VERSION)" | cut -c1)
define which
$(shell $(WITH_BH_VENV) which $1 2>/dev/null)
endef
.PHONY: bh/venv/debug
bh/venv/debug:
$(call print-var,LOCAL_PYTHON_VERSION)
$(call print-var,LOCAL_PROJECT_PYTHON_VERSION)
$(call print-var,LOCAL_PROJECT_PYTHON_VERSION_MAJOR)
$(call print-var,BUILD_HARNESS_REQ)
$(call print-var,BUILD_HARNESS_VENV)
$(call print-var,BUILD_HARNESS_PIPFILE)
$(call print-var,BUMPVERSION)
$(call print-var,BLACK)
$(call print-var,ISORT)
.PHONY: bh/venv
bh/venv: $(BUILD_HARNESS_VENV)
$(BUILD_HARNESS_REQ_TEMPLATE):
@echo "[build-harness] MISSING: $@"
$(BUILD_HARNESS_REQ): $(BUILD_HARNESS_REQ_TEMPLATE)
@if [ -f $@ ]; then echo "Removing previously generated build-harness requirements" && rm $@; fi
@echo "Generating $@"
@cat $(BUILD_HARNESS_REQ_TEMPLATE) | xargs -I % bash -c 'if [ -z "$$(which % 2>/dev/null)" ]; then echo "%" >> $@; fi'
$(BUILD_HARNESS_PIPFILE): $(BUILD_HARNESS_REQ)
@if [ -f $@ ]; then echo "Removing previously generated build-harness Pipfile" && rm $@; fi
@if [ -n "$$(which pipenv 2>/dev/null)" ]; then \
echo "Generating $@"; \
if [ -n "$(LOCAL_PROJECT_PYTHON_VERSION)" ]; then \
echo "[requires]" >> $@; \
echo "python_version = \"$(LOCAL_PROJECT_PYTHON_VERSION)\"" >> $@; \
fi; \
echo "[packages]" >> $@; \
if [ "$(BUILD_HARNESS_VENV_SKIP_EXISTING)" = true ] ; then \
echo "Will skip required packages that exist in your path..."; \
cat $(BUILD_HARNESS_REQ) | xargs -I % bash -c 'if [ -z "$$(which % 2>/dev/null)" ]; then echo "% = \"*\"" >> $@; fi'; \
else \
cat $(BUILD_HARNESS_REQ) | xargs -I % bash -c 'echo "% = \"*\"" >> $@'; \
fi; \
fi
$(BUILD_HARNESS_VENV): $(BUILD_HARNESS_REQ) $(BUILD_HARNESS_PIPFILE)
@if [ $(LOCAL_PYTHON_VERSION) -eq 2 ]; then \
if [ $(LOCAL_PROJECT_PYTHON_VERSION_MAJOR) -eq 3 ]; then \
echo "WARNING: Building a Python 3 project with Python 2."; \
echo "'It might..just..work.' - Carrot"; \
else \
echo "ERROR: Python $(LOCAL_PYTHON_VERSION) is unsupported by build-harness! Unable to install build-harness python dependencies automatically."; exit 1; \
fi; \
fi
@if [ -s $(BUILD_HARNESS_REQ) ]; then echo "Installing (Python $(LOCAL_PROJECT_PYTHON_VERSION) project via Python $(LOCAL_PYTHON_VERSION)): $$(cat $(BUILD_HARNESS_REQ) | tr '\n' ' ')"; fi
@if [ -n "$$(which pipenv 2>/dev/null)" ]; then \
echo "Installing build-harness requirements with pipenv..."; \
( \
if [ $(LOCAL_PYTHON_VERSION) -eq 2 ]; then virtualenv $@; else python -m venv $@; fi \
&& source $@/bin/activate && PIPENV_PIPFILE=$(BUILD_HARNESS_PIPFILE) PIPENV_QUIET=1 pipenv --bare install --dev --pre \
); if [ $$? -ne 0 ]; then rm -rf $@; fi; \
else \
echo "WARNING: Installing build-harness requirements with venv + pip because pipenv was not installed..."; \
( \
if [ $(LOCAL_PYTHON_VERSION) -eq 2 ]; then virtualenv $@; else python -m venv $@; fi \
&& source $@/bin/activate && pip install -r $(BUILD_HARNESS_REQ) --ignore-installed \
); if [ $$? -ne 0 ]; then rm -rf $@; fi; \
fi