Skip to content

Commit

Permalink
Initial migration to poetry, python12, removes pip-compile, updates m…
Browse files Browse the repository at this point in the history
…akefile, moves to standard pythong package subdirectories
  • Loading branch information
karlbrown-va committed Sep 18, 2024
1 parent ca1f770 commit 59ce1b9
Show file tree
Hide file tree
Showing 20 changed files with 670 additions and 370 deletions.
3 changes: 1 addition & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[run]
branch = True
source = .
source = wtf_bot
omit =
**/test_*
**/ENV/*

[html]
directory = results/coverage
Expand Down
20 changes: 11 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ jobs:
name: build and test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python $
uses: actions/setup-python@v4
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: '.python-version' # Read python version from a file
- name: Install dependencies
run: |
pip install --require-hashes -r requirements.txt -r dev-requirements.txt
- name: Test with pytest
run: |
make test

- name: Set up poetry
uses: Gr1N/setup-poetry@v9

- name: Runs tests and static analysis
shell: bash
run: make test
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ results/
vars.yml

# ide
.idea
.idea

.install.stamp
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.8.12
3.12.6
121 changes: 43 additions & 78 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,92 +1,57 @@
# Makefile for wtf-bot
# Tested with GNU Make 3.8.1
MAKEFLAGS += --warn-undefined-variables
SHELL := /usr/bin/env bash -e
CI_ARG := $(CI)

SHELL := /usr/bin/env bash -e -u -o pipefail
.DEFAULT_GOAL := help
INSTALL_STAMP := .install.stamp
POETRY := $(shell command -v poetry 2> /dev/null)

# cribbed from https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html and https://news.ycombinator.com/item?id=11195539
# cribbed from https://github.com/mozilla-services/telescope/blob/main/Makefile
.PHONY: help
help: ## Prints out documentation for available commands
@awk -F ':|##' \
'/^[^\t].+?:.*?##/ {\
printf "\033[36m%-30s\033[0m %s\n", $$1, $$NF \
}' $(MAKEFILE_LIST)

## Pip / Python

.PHONY: python-install
# python-install recipe all has to run in a single shell because it's running inside a virtualenv
python-install: requirements.txt dev-requirements.txt ## Sets up your python environment for the first time (only need to run once)
pip install virtualenv ;\
virtualenv ENV ;\
source ENV/bin/activate ;\
echo shell ENV activated ;\
pip install --require-hashes -r requirements.txt -r dev-requirements.txt ;\
echo Finished install ;\
echo Please activate the virtualenvironment with: ;\
echo source ENV/bin/activate

# Errors out if VIRTUAL_ENV is not defined and we aren't in a CI environment.
.PHONY: check-env
check-env:
ifndef VIRTUAL_ENV
ifneq ($(CI_ARG), true)
$(error VIRTUAL_ENV is undefined, meaning you aren't running in a virtual environment. Fix by running: 'source ENV/bin/activate')
endif
endif

requirements.txt: requirements.in
pip-compile --allow-unsafe --generate-hashes requirements.in --output-file $@

dev-requirements.txt: dev-requirements.in
pip-compile --allow-unsafe --generate-hashes dev-requirements.in --output-file $@

.PHONY: pip-upgrade
pip-upgrade: ## Upgrade all python dependencies
pip-compile --upgrade --allow-unsafe --generate-hashes requirements.in --output-file requirements.txt
pip-compile --upgrade --allow-unsafe --generate-hashes dev-requirements.in --output-file dev-requirements.txt

SITE_PACKAGES := $(shell pip show pip | grep '^Location' | cut -f2 -d ':')
$(SITE_PACKAGES): requirements.txt dev-requirements.txt check-env
ifeq ($(CI_ARG), true)
@echo "Do nothing; assume python dependencies were installed already"
else
pip-sync requirements.txt dev-requirements.txt
endif

.PHONY: pip-install
pip-install: $(SITE_PACKAGES)

## Test targets
@echo "Please use 'make <target>' where <target> is one of the following commands."
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo "Check the Makefile to know exactly what each target is doing."

install: $(INSTALL_STAMP) ## Install dependencies
$(INSTALL_STAMP): pyproject.toml poetry.lock
@if [[ -z "$(POETRY)" ]]; then echo "Poetry could not be found. See https://python-poetry.org/docs/"; exit 2; fi
@echo "=> Installing python dependencies"
"$(POETRY)" --version
"$(POETRY)" install
touch $(INSTALL_STAMP)

.PHONY: format-check
format-check: $(INSTALL_STAMP) ## runs code formatting
"$(POETRY)" run ruff format --check

.PHONY: format-fix
format-fix: $(INSTALL_STAMP) ## runs code formatting
"$(POETRY)" run ruff format

.PHONY: lint
lint: $(INSTALL_STAMP) ## runs code formatting checks
"$(POETRY)" run ruff check

.PHONY: lint-fix
lint-fix: $(INSTALL_STAMP) ## runs code formatting checks
"$(POETRY)" run ruff check --fix --exit-non-zero-on-fix

## Test
.PHONY: unit-test
unit-test: pip-install ## Run python unit tests
python -m pytest -v --cov --cov-report term --cov-report xml --cov-report html

.PHONY: flake8
flake8: pip-install ## Run Flake8 python static style checking and linting
@echo "flake8 comments:"
flake8 --statistics .
unit-test: $(INSTALL_STAMP) ## Run python unit tests
"$(POETRY)" run pytest -v --cov --cov-report term --cov-report xml --cov-report html

.PHONY: test
test: unit-test flake8 ## Run unit tests, static analysis
test: unit-test format-check lint ## Run unit tests, static analysis
@echo "All tests passed." # This should only be printed if all of the other targets succeed

.PHONY: install-action-lint
install-action-lint: ## Install actionlint
brew install actionlint

.PHONY: actionlint
actionlint: ## Run actionlint
actionlint

.PHONY: clean
clean: ## Delete any directories, files or logs that are auto-generated, except python packages
clean: ## Delete any directories, files or logs that are auto-generated
rm -rf results
rm -rf .pytest_cache
rm -f .coverage
find . -type d -name "__pycache__" | xargs rm -rf {};
rm -f .install.stamp .coverage .coverage.*

.PHONY: deepclean
deepclean: clean ## Delete python packages and virtualenv. You must run 'make python-install' after running this.
rm -rf ENV
@echo virtualenvironment was deleted. Type 'deactivate' to deactivate the shims.
deepclean: clean ## Runs cleans and deletes all poetry environments
"$(POETRY)" env remove --all -n
@echo Poetry environments deleted. Type 'exit' to exit the shell.
9 changes: 0 additions & 9 deletions dev-requirements.in

This file was deleted.

148 changes: 0 additions & 148 deletions dev-requirements.txt

This file was deleted.

Loading

0 comments on commit 59ce1b9

Please sign in to comment.