Skip to content

Instantly share code, notes, and snippets.

@lewtun
Last active August 13, 2020 20:17
Show Gist options
  • Save lewtun/3da515880549d1aa99d585f503183514 to your computer and use it in GitHub Desktop.
Save lewtun/3da515880549d1aa99d585f503183514 to your computer and use it in GitHub Desktop.
Makefile for nbdev with linting and code formatting
# Copyight 2016 drivendata
# Copyright 2019 fast.ai
# Copyright 2020 Lewis Tunstall
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This file combines and adapts the following two Makefiles:
# nbdev: https://github.com/fastai/nbdev/blob/master/Makefile
# cookiecutter-data-science: https://github.com/drivendata/cookiecutter-data-science
.PHONY: all my_awesome_library docs_serve docs test release pypi dist clean style precommit quality
#################################################################################
# GLOBALS #
#################################################################################
SRC = $(wildcard notebooks/*.ipynb)
#################################################################################
# COMMANDS #
#################################################################################
## Build library and docs
all: my_awesome_library docs
## Build library
my_awesome_library: $(SRC)
nbdev_build_lib
touch my_awesome_library
## Spin up docs server
docs_serve: docs
cd docs && bundle exec jekyll serve
## Build docs
docs: $(SRC)
nbdev_build_docs
touch docs
## Run tests
test:
nbdev_test_nbs
## Make PyPI release
release: pypi
nbdev_bump_version
## Upload to PyPI
pypi: dist
twine upload --repository pypi dist/*
## Make source distribution
dist: clean
python setup.py sdist bdist_wheel
## Remove source distribution
clean:
rm -rf dist
## Format source code
style:
black --line-length 119 my_awesome_library
## Build library, docs, and format source code and notebooks
precommit: my_awesome_library docs style
nbdev_clean_nbs
## Check code formatting, imports, and linting
quality: style
black --check --line-length 119 my_awesome_library
isort --check-only my_awesome_library
flake8 my_awesome_library
#################################################################################
# Self Documenting Commands #
#################################################################################
.DEFAULT_GOAL := help
# Inspired by <http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html>
# sed script explained:
# /^##/:
# * save line in hold space
# * purge line
# * Loop:
# * append newline + line to hold space
# * go to next line
# * if line starts with doc comment, strip comment character off and loop
# * remove target prerequisites
# * append hold space (+ newline) to line
# * replace newline plus comments by `---`
# * print line
# Separate expressions are necessary because labels cannot be delimited by
# semicolon; see <http://stackoverflow.com/a/11799865/1968>
.PHONY: help
help:
@echo "$$(tput bold)Available rules:$$(tput sgr0)"
@echo
@sed -n -e "/^## / { \
h; \
s/.*//; \
:doc" \
-e "H; \
n; \
s/^## //; \
t doc" \
-e "s/:.*//; \
G; \
s/\\n## /---/; \
s/\\n/ /g; \
p; \
}" ${MAKEFILE_LIST} \
| LC_ALL='C' sort --ignore-case \
| awk -F '---' \
-v ncol=$$(tput cols) \
-v indent=19 \
-v col_on="$$(tput setaf 6)" \
-v col_off="$$(tput sgr0)" \
'{ \
printf "%s%*s%s ", col_on, -indent, $$1, col_off; \
n = split($$2, words, " "); \
line_length = ncol - indent; \
for (i = 1; i <= n; i++) { \
line_length -= length(words[i]) + 1; \
if (line_length <= 0) { \
line_length = ncol - indent - length(words[i]) - 1; \
printf "\n%*s ", -indent, " "; \
} \
printf "%s ", words[i]; \
} \
printf "\n"; \
}' \
| more $(shell test $(shell uname) = Darwin && echo '--no-init --raw-control-chars')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment