Skip to content

Instantly share code, notes, and snippets.

@mbiemann
Last active November 15, 2022 12:25
Show Gist options
  • Save mbiemann/f00121fe7dc190cf1eec6030be366172 to your computer and use it in GitHub Desktop.
Save mbiemann/f00121fe7dc190cf1eec6030be366172 to your computer and use it in GitHub Desktop.
Some Tips and Examples of MAKEFILE
#!make
MAKEFLAGS += --always-make
MAKEFILE_VERSION = 0.0.2 2022-11-15 12:17 PM
# ==============================================================================
.EXPORT_ALL_VARIABLES:
ifneq ($(wildcard ~/.makeconfig),)
include ~/.makeconfig
endif
ifneq ($(wildcard Makeparams),)
include Makeparams
endif
PYPINSTALL_REQUIREMENTS=yes
PYPINSTALL_REQUIREMENTS_DEV=yes
PYPINSTALL_PYTEST=yes
PYPINSTALL_BEHAVE=yes
PYPINSTALL_PRECOMMIT=yes
TWINE_USERNAME_CLN=$(shell echo ${TWINE_USERNAME} | tr -d '\n')
TWINE_PASSWORD_CLN=$(shell echo ${TWINE_PASSWORD} | tr -d '\n')
TWINE_REPOSITORY_STG=$(shell echo ${TWINE_REPOSITORY_URL} | tr -d '\n' | awk -F 'https://' '{print $$2}' | sed 's/pypi-local/pypi\/simple/g')
TWINE_REPOSITORY_CLN=$(shell echo " -i https://${TWINE_USERNAME_CLN}:${TWINE_PASSWORD_CLN}@${TWINE_REPOSITORY_STG}")
TWINE_PIP_INDEX_URL=$(if ${TWINE_REPOSITORY_URL},${TWINE_REPOSITORY_CLN},)
PYTHON_EXEC_VENV=$(if ${PYTHON_EXEC},${PYTHON_EXEC},$(shell echo "pyenv exec python"))
# ==============================================================================
ifdef TERM
TPUT_COLS=$$(tput cols)
TPUT_BOLD=$$(tput bold)
TPUT_0=$$(tput setaf 0)
TPUT_1=$$(tput setaf 1)
TPUT_2=$$(tput setaf 2)
TPUT_3=$$(tput setaf 3)
TPUT_6=$$(tput setaf 6)
TPUT_END=$$(tput sgr0)
else
TPUT_COLS=80
endif
help: version
@echo "${TPUT_BOLD}Commands:${TPUT_END}"
@sed -n -e "/^## / {h; s/.*//; :doc" -e "H; n; s/^## //; t doc" -e "s/:.*//; G; s/\\n## /---/; s/\\n/ /g; p;}" ${MAKEFILE_LIST} \
| awk -F '---' -v ncol=${TPUT_COLS} -v col_on="${TPUT_6}" -v col_off="${TPUT_END}" '{ \
printf "%s\t%*s%s -> ", col_on, -1, $$1, col_off; n = split($$2, words, " "); line_length = ncol - indent; \
for (i = 1; i <= n; i++) { \
line_length -= length(words[i]) + 2; \
if (line_length <= 0) { line_length = ncol - length(words[i]); printf "\n%*s ", - 7, " "; } \
printf "%s ", words[i]; \
} \
printf "\n"; }'
@echo ""
@echo "${TPUT_0}For more ways to work, run ${TPUT_6}make more${TPUT_END}"
@echo ""
version:
@echo "${TPUT_0}Makefile Version: ${MAKEFILE_VERSION}${TPUT_END}\n"
more: version
@echo "${TPUT_BOLD}Variables:${TPUT_END}"
@echo "\t${TPUT_3}ENV${TPUT_END} -> infra environment name: ${ENV}"
@echo "\t${TPUT_3}PYTEST${TPUT_END} -> folder, file or method to test: ${PYTEST}"
@echo "\t${TPUT_3}PYBEHAVE${TPUT_END} -> folder, file or method to behave: ${PYBEHAVE}"
@echo "\t${TPUT_3}PYTHON_EXEC${TPUT_END} -> executable to override pyenv: ${PYTHON_EXEC}"
@echo "\t${TPUT_3}PYPINSTALL_REQUIREMENTS${TPUT_END} -> install requirements.txt: ${PYPINSTALL_REQUIREMENTS}"
@echo "\t${TPUT_3}PYPINSTALL_REQUIREMENTS_DEV${TPUT_END} -> install requirements-dev.txt: ${PYPINSTALL_REQUIREMENTS_DEV}"
@echo "\t${TPUT_3}PYPINSTALL_PYTEST${TPUT_END} -> install pytest and pytest-cov: ${PYPINSTALL_PYTEST}"
@echo "\t${TPUT_3}PYPINSTALL_BEHAVE${TPUT_END} -> install behave and allure: ${PYPINSTALL_BEHAVE}"
@echo "\t${TPUT_3}PYPINSTALL_PRECOMMIT${TPUT_END} -> install pre-commit: ${PYPINSTALL_PRECOMMIT}"
@echo "\t${TPUT_3}TWINE_USERNAME${TPUT_END} -> Twine Username for PIP Index: ${TWINE_USERNAME}"
@echo "\t${TPUT_3}TWINE_PASSWORD${TPUT_END} -> Twine Password for PIP Index: ${TWINE_PASSWORD}"
@echo "\t${TPUT_3}TWINE_REPOSITORY_URL${TPUT_END} -> Twine Repository URL for PIP Index: ${TWINE_REPOSITORY_URL}"
@echo ""
@echo "\t${TPUT_0}Create file ${TPUT_2}~/.makeconfig${TPUT_0} for global environment variables used accross all projects.${TPUT_END}"
@echo "\t${TPUT_0}Create file ${TPUT_2}./Makeparams${TPUT_0} for enviroment variables specified for this project.${TPUT_END}"
@echo ""
@echo "${TPUT_BOLD}Connected to AWS${TPUT_END} (after make connect):"
@echo "\t${TPUT_6}aws <command> --profile gfdev${TPUT_END} -> execute aws cli command with a specified profile"
@echo "\t${TPUT_6}export AWS_PROFILE=gfdev${TPUT_END} -> set default profile to your current terminal"
@echo ""
@echo "${TPUT_BOLD}Installing Python libs:${TPUT_END}"
@echo "\t${TPUT_6}make install PYPINSTALL_PRECOMMIT=no${TPUT_END} -> run all installation but pre-commit (check variables for more options)"
@echo "\t${TPUT_6}.venv/bin/pip freeze${TPUT_END} -> list the installed libs"
@echo ""
@echo "${TPUT_BOLD}Using Python Virtual Environment:${TPUT_END}"
@echo "\t${TPUT_6}.venv/bin/python${TPUT_END} -> open the Python Code Interactive session"
@echo "\t${TPUT_6}.venv/bin/python --version${TPUT_END} -> check version"
@echo "\t${TPUT_6}.venv/bin/python path/to/file.py${TPUT_END} -> run script"
@echo "\t${TPUT_6}source .venv/bin/activate${TPUT_END} -> active the virtual environment in your current terminal"
@echo "\t${TPUT_6}deactivate${TPUT_END} -> deactive the virtual environment if is activated"
@echo ""
@echo "${TPUT_BOLD}Unit Testing:${TPUT_END}"
@echo "\t${TPUT_6}make test PYTEST=path/to/test_file.py::method${TPUT_END} -> test only a specified method"
@echo "\t${TPUT_6}make test PYTEST=path/to/test_file.py${TPUT_END} -> test a specified file and all methods"
@echo "\t${TPUT_6}make test PYTEST=path${TPUT_END} -> test all files in the path"
@echo ""
@echo "${TPUT_BOLD}Behaviour Testing:${TPUT_END}"
@echo "\t${TPUT_6}make behave PYBEHAVE=path/to/file.feature${TPUT_END} -> test a specified file and all features"
@echo "\t${TPUT_6}make behave PYBEHAVE_OUTPUT=allure${TPUT_END} -> format and redirect the results using Allure"
@echo ""
# ==============================================================================
# ===== Available rules - use ## to show a description on help or jump line ====
# ==============================================================================
## create python virtual environment and install requirements
install: version
@test -d .venv || { \
if [ ${PYTHON_EXEC} ]; then \
echo "${TPUT_3}Creating local Python Virtual Environment using ${PYTHON_EXEC}...${TPUT_END}"; \
else \
test -s .python-version \
&& echo "${TPUT_3}Creating local Python Virtual Environment using pyenv...${TPUT_END}" \
|| { \
echo "${TPUT_BOLD}${TPUT_1}ERROR: File .python-version not found.${TPUT_END}"; \
echo "\tRun 'pyenv local <version>' to create the file."; \
echo "\tCheck availables version by running 'pyenv versions'."; \
echo "\tMake sure you have 'pyenv' installed."; \
echo ""; \
echo "\tAlternatively you can run 'make install PYTHON_EXEC=/path/to/python'."; \
echo ""; exit 1; \
} \
fi; \
echo "${PYTHON_EXEC_VENV} -m venv .venv"; \
${PYTHON_EXEC_VENV} -m venv .venv; \
}
@echo "\n${TPUT_3}Upgrading python pip...${TPUT_END}"
.venv/bin/python -m pip install --upgrade pip
ifeq (${PYPINSTALL_REQUIREMENTS}, yes)
@echo "\n${TPUT_3}Installing requirements.txt...${TPUT_END}"
.venv/bin/python -m pip install${TWINE_PIP_INDEX_URL} -r requirements.txt
endif
ifeq (${PYPINSTALL_REQUIREMENTS_DEV}, yes)
@echo "\n${TPUT_3}Installing requirements-dev.txt...${TPUT_END}"
.venv/bin/python -m pip install -r requirements-dev.txt
endif
ifeq (${PYPINSTALL_PYTEST}, yes)
@echo "\n${TPUT_3}Installing pytest...${TPUT_END}"
.venv/bin/python -m pip install pytest pytest-cov
endif
ifeq (${PYPINSTALL_BEHAVE}, yes)
@echo "\n${TPUT_3}Installing behave...${TPUT_END}"
.venv/bin/python -m pip install behave allure-behave
endif
ifeq (${PYPINSTALL_PRECOMMIT}, yes)
@echo "\n${TPUT_3}Installing pre-commit...${TPUT_END}"
.venv/bin/python -m pip install pre-commit
.venv/bin/pre-commit install
endif
@echo "\n${TPUT_BOLD}${TPUT_2}Local Python Virtual Environment:${TPUT_END}"
@echo "\tSOURCE -> .venv/bin/python"
@echo "\tVERSION -> $$(.venv/bin/python -VV)"
@echo ""
## run pre-commit for all files
check: version
@test -f .venv/bin/pre-commit || { echo "${TPUT_BOLD}${TPUT_1}Before, you have to run ${TPUT_6}make install${TPUT_END}\n"; exit 1; }
@echo "${TPUT_3}Running pre-commit for all files...${TPUT_END}"
.venv/bin/pre-commit run --all-files
## run pytest and coverage
test: version
@test -f .venv/bin/pytest || { echo "${TPUT_BOLD}${TPUT_1}Before, you have to run ${TPUT_6}make install${TPUT_END}\n"; exit 1; }
@echo "${TPUT_3}Running unit tests...${TPUT_END}"
.venv/bin/pytest ${PYTEST}
## connect to AWS using saml2aws
connect: version
@which aws > /dev/null || echo "${TPUT_BOLD}${TPUT_1}ERROR: AWS CLI not found.${TPUT_END}"
@which saml2aws > /dev/null || echo "${TPUT_BOLD}${TPUT_1}ERROR: saml2aws not found.${TPUT_END}"
@echo "${TPUT_3}Login to AWS using SAML2...${TPUT_END}"
saml2aws login --skip-prompt --disable-keychain
@echo "\n${TPUT_3}Checking profile gfdev...${TPUT_END}"
aws sts get-caller-identity --profile gfdev
@echo "\n${TPUT_BOLD}${TPUT_2}You are connected to AWS CLI.${TPUT_END}"
@echo
## run behaviour tests
behave: version
@test -f .venv/bin/behave || { echo "${TPUT_BOLD}${TPUT_1}Before, you have to run ${TPUT_6}make install${TPUT_END}\n"; exit 1; }
@echo "${TPUT_3}Removing Behavior Tests Results...${TPUT_END}"
rm -rf behave-results
@echo "\n${TPUT_3}Running behavior tests...${TPUT_END}"
ifeq (${PYBEHAVE_OUTPUT}, allure)
.venv/bin/behave -f allure_behave.formatter:AllureFormatter -o behave-results ${PYBEHAVE} --tags=@${ENV}
.venv/bin/allure generate behave-results --clean -o behave-report
else
.venv/bin/behave ${PYBEHAVE} --tags=@${ENV}
endif
## run performance and load tests
locust: version
@test -f .venv/bin/locust || { echo "${TPUT_BOLD}${TPUT_1}Before, you have to run ${TPUT_6}make install${TPUT_END}\n"; exit 1; }
@echo "\n${TPUT_3}Running performance tests...${TPUT_END}"
.venv/bin/locust
## remove python virtual environment and local temporary files
clean: version
@echo "${TPUT_3}Removing Local Python Virtual Environment...${TPUT_END}"
@rm -rf .venv
@echo "${TPUT_3}Removing PyTest Cache...${TPUT_END}"
@rm -rf .pytest_cache
@echo "${TPUT_3}Removing Behavior Tests Results...${TPUT_END}"
@rm -rf behave-results
@echo "${TPUT_3}Removing Python Cache...${TPUT_END}"
@find ./ -type d -name '__pycache__' -exec rm -rf {} +;
@echo "${TPUT_3}Removing PyTest Coverage Cache...${TPUT_END}"
@rm -f .coverage
@echo "${TPUT_3}Removing .makefail...${TPUT_END}"
@rm -f .makefail
@echo "\n${TPUT_BOLD}${TPUT_2}Clean!${TPUT_END}"
@if [ ${VIRTUAL_ENV} ]; then echo ${TPUT_BOLD}${TPUT_1}"\nMANDATORY:\n\tRun 'deactivate' to exit of virtual environment.${TPUT_END}"; fi
@echo ""
## delete AWS CLI Credentials file
disconnect: version
@echo "${TPUT_3}Deleting AWS CLI Credentials file...${TPUT_END}"
rm -f ~/.aws/credentials
@echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment