|
| 1 | +SHELL = /bin/bash |
| 2 | + |
| 3 | +.DEFAULT_GOAL := help |
| 4 | + |
| 5 | +CURRENT_DIR := $(shell pwd) |
| 6 | +INSTALL_DIR := $(CURRENT_DIR)/.venv |
| 7 | +PYTHON_FILES := $(shell find ./* -type f -name "*.py" -print) |
| 8 | + |
| 9 | +#FIXME: put this variable in config file |
| 10 | +PYTHON_VERSION=3.7.4 |
| 11 | +CURRENT_PYTHON_VERSION := $(shell python3 -c "import sys;t='{v[0]}.{v[1]}.{v[2]}'.format(v=list(sys.version_info[:]));sys.stdout.write(t)") |
| 12 | +PYTHON_VERSION_OK := $(shell python3 -c "import sys; t=sys.version_info[0:3]; print(int('{}.{}.{}'.format(*t) == '$(PYTHON_VERSION)'))") |
| 13 | + |
| 14 | +# Commands |
| 15 | +SYSTEM_PYTHON_CMD := $(shell which python3) |
| 16 | +PYTHON_CMD := $(INSTALL_DIR)/bin/python3 |
| 17 | +PIP_CMD := $(INSTALL_DIR)/bin/pip3 |
| 18 | +FLASK_CMD := $(INSTALL_DIR)/bin/flask |
| 19 | +YAPF_CMD := $(INSTALL_DIR)/bin/yapf |
| 20 | +all: help |
| 21 | + |
| 22 | +# This bit check define the build/python "target": if the system has an acceptable version of python, there will be no need to install python locally. |
| 23 | + |
| 24 | +ifeq ($(PYTHON_VERSION_OK),1) |
| 25 | +PYTHON_BINDIR := $(shell dirname $(PYTHON_CMD)) |
| 26 | +PYTHONHOME := $(shell eval "cd $(PYTHON_BINDIR); pwd; cd > /dev/null") |
| 27 | +build/python: |
| 28 | + @echo $(CURRENT_PYTHON_VERSION) |
| 29 | + @echo $(shell $(PYTHON_CMD) -c "print('OK')") |
| 30 | + mkdir -p build |
| 31 | + touch build/python; |
| 32 | +else |
| 33 | +build/python: local/bin/python3.7 |
| 34 | + mkdir -p build |
| 35 | + touch build/python; |
| 36 | + |
| 37 | +SYSTEM_PYTHON_CMD := $(CURRENT_DIR)/local/bin/python3.7 |
| 38 | +endif |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +.PHONY: help |
| 43 | +help: |
| 44 | + @echo "Usage: make <target>" |
| 45 | + @echo |
| 46 | + @echo "Possible targets:" |
| 47 | + @echo -e " \033[1mBUILD TARGETS\033[0m " |
| 48 | + @echo "- setup Create the python virtual environment" |
| 49 | + @echo -e " \033[1mLINTING TOOLS TARGETS\033[0m " |
| 50 | + @echo "- lint Lint the python source code" |
| 51 | + @echo -e " \033[1mLOCAL SERVER TARGETS\033[0m " |
| 52 | + @echo "- serve Run the project using the flask debug server" |
| 53 | + @echo "- gunicornserve Run the project using the gunicorn WSGI server" |
| 54 | + @echo "- dockerrun Run the project using the gunicorn WSGI server inside a container. Env variable HTTP_PORT should be set" |
| 55 | + @echo "- shutdown Stop the aforementioned container" |
| 56 | + @echo -e " \033[1mCLEANING TARGETS\033[0m " |
| 57 | + @echo "- clean Clean genereated files" |
| 58 | + @echo "- clean_venv Clean python venv" |
| 59 | + |
| 60 | +# Build targets. Calling setup is all that is needed for the local files to be installed as needed. Bundesnetz may cause problem. |
| 61 | + |
| 62 | +python: build/python |
| 63 | + @echo "Python installed" |
| 64 | + |
| 65 | +.PHONY: setup |
| 66 | +setup: python .venv/build.timestamp |
| 67 | + |
| 68 | + |
| 69 | +local/bin/python3.7: |
| 70 | + mkdir -p $(CURRENT_DIR)/local; |
| 71 | + curl -z $(CURRENT_DIR)/local/Python-$(PYTHON_VERSION).tar.xz https://www.python.org/ftp/python/$(PYTHON_VERSION)/Python-$(PYTHON_VERSION).tar.xz -o $(CURRENT_DIR)/local/Python-$(PYTHON_VERSION).tar.xz; |
| 72 | + cd $(CURRENT_DIR)/local && tar -xf Python-$(PYTHON_VERSION).tar.xz && Python-$(PYTHON_VERSION)/configure --prefix=$(CURRENT_DIR)/local/ && make altinstall |
| 73 | + |
| 74 | +.venv/build.timestamp: build/python |
| 75 | + $(SYSTEM_PYTHON_CMD) -m venv $(INSTALL_DIR) && $(PIP_CMD) install --upgrade pip setuptools |
| 76 | + ${PIP_CMD} install -r dev_requirements.txt |
| 77 | + $(PIP_CMD) install -r requirements.txt |
| 78 | + touch .venv/build.timestamp |
| 79 | + |
| 80 | +# linting target, calls upon yapf to make sure your code is easier to read and respects some conventions. |
| 81 | + |
| 82 | +.PHONY: lint |
| 83 | +lint: .venv/build.timestamp |
| 84 | + $(YAPF_CMD) -i --style .style.yapf $(PYTHON_FILES) |
| 85 | + |
| 86 | +# Serve targets. Using these will run the application on your local machine. You can either serve with a wsgi front (like it would be within the container), or without. |
| 87 | +.PHONY: serve |
| 88 | +serve: .venv/build.timestamp |
| 89 | + FLASK_APP=service_launcher FLASK_DEBUG=1 ${FLASK_CMD} run --host=0.0.0.0 --port=${HTTP_PORT} |
| 90 | + |
| 91 | +.PHONY: gunicornserve |
| 92 | +gunicornserve: .venv/build.timestamp |
| 93 | + ${SYSTEM_PYTHON_CMD} wsgi.py |
| 94 | + |
| 95 | + |
| 96 | +# Docker related functions. |
| 97 | + |
| 98 | +.PHONY: dockerrun |
| 99 | +dockerrun: |
| 100 | + docker-compose up -d; |
| 101 | + sleep 10 |
| 102 | + |
| 103 | +.PHONY: shutdown |
| 104 | +shutdown: |
| 105 | + docker-compose down |
| 106 | + |
| 107 | +# Cleaning functions. clean_venv will only remove the virtual environment, while clean will also remove the local python installation. |
| 108 | + |
| 109 | +.PHONY: clean |
| 110 | +clean: clean_venv |
| 111 | + rm -rf local; |
| 112 | + |
| 113 | +.PHONY: clean_venv |
| 114 | +clean_venv: |
| 115 | + rm -rf ${INSTALL_DIR}; |
0 commit comments