Skip to content

Instantly share code, notes, and snippets.

@MarkWarneke
Created June 18, 2020 12:25
Show Gist options
  • Save MarkWarneke/2e26d7caef237042e9374ebf564517ad to your computer and use it in GitHub Desktop.
Save MarkWarneke/2e26d7caef237042e9374ebf564517ad to your computer and use it in GitHub Desktop.
Makefile for python code
# Makefile for python code
#
# > make help
#
# The following commands can be used.
#
# init: sets up environment and installs requirements
# install: Installs development requirments
# format: Formats the code with autopep8
# lint: Runs flake8 on src, exit if critical rules are broken
# clean: Remove build and cache files
# env: Source venv and environment files for testing
# leave: Cleanup and deactivate venv
# test: Run pytest
# run: Executes the logic
VENV_PATH='env/bin/activate'
ENVIRONMENT_VARIABLE_FILE='.env'
DOCKER_NAME=$DOCKER_NAME
DOCKER_TAG=$DOCKER_TAG
define find.functions
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
endef
help:
@echo 'The following commands can be used.'
@echo ''
$(call find.functions)
init: ## sets up environment and installs requirements
init:
pip install -r requirements.txt
install: ## Installs development requirments
install:
python -m pip install --upgrade pip
# Used for packaging and publishing
pip install setuptools wheel twine
# Used for linting
pip install flake8
# Used for testing
pip install pytest
lint: ## Runs flake8 on src, exit if critical rules are broken
lint:
# stop the build if there are Python syntax errors or undefined names
flake8 src --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 src --count --exit-zero --statistics
package: ## Create package in dist
package: clean
python3 setup/setup.py sdist bdist_wheel
upload-test: ## Create package and upload to test.pypi
upload-test: package
python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* --non-interactive --verbose
upload: ## Create package and upload to pypi
upload: package
python3 -m twine upload dist/* --non-interactive
clean: ## Remove build and cache files
clean:
rm -rf *.egg-info
rm -rf build
rm -rf dist
rm -rf .pytest_cache
# Remove all pycache
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf
env: ## Source venv and environment files for testing
env:
python3 -m venv env
source $(VENV_PATH)
source $(ENVIRONMENT_VARIABLE_FILE)
leave: ## Cleanup and deactivate venv
leave: clean
deactivate
test: ## Run pytest
test:
pytest . -p no:logging -p no:warnings
build: ## Build docker image
build:
docker build -t $(DOCKER_NAME):$(DOCKER_TAG) -f docker/Dockerfile .
create: ## Create docker image
create: build
docker create -it --name $(DOCKER_NAME) $(DOCKER_NAME):$(DOCKER_TAG)
start: ## Build and start docker image
start: build
docker start $(DOCKER_NAME)
run: ## build, start and run docker image
run: start
docker run -it $(DOCKER_NAME):$(DOCKER_TAG)
exec: ## build, start and exec into docker image
exec: start
docker exec -it $(DOCKER_NAME) python
@MestreLion
Copy link

You sure this works with GNU make? All sourcing in env are not inherited in other targets, and does not apply to when make ends and you return to your current shell either. Besides, no other target depends on env, so all pip install are not performed inside the virtual env

@MarkWarneke
Copy link
Author

You sure this works with GNU make?

Yes this works with GNU make.

All sourcing in env are not inherited in other targets, and does not apply to when make ends and you return to your current shell either. Besides, no other target depends on env, so all pip install are not performed inside the virtual env

Correct, this is for reference. I haven't figured out how to make it work as a prerequisite.

@esantix
Copy link

esantix commented Mar 28, 2024

Adding this command @export $(shell cat lenv | grep -v '^#' | xargs)

@MestreLion
Copy link

Correct, this is for reference. I haven't figured out how to make it work as a prerequisite.

This is as close as I could get:

https://github.com/MestreLion/hackmatch/blob/main/Makefile

It runs all commands in the venv, creating it if necessary, and install system package dependencies. It also improves a bit on your help implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment